We Help You To Renovate Your Blogger Blog With Awesomeness!

Monday, September 29, 2014

  • Datetime format Issue String was not recognized as a valid DateTime C# ASP .Net


    If this issue happens after the website is uploaded to server and no problem at your local computer, the cause of error is the date format difference of your system and your server

    Mostly the error happens for month

    consider a date "29/09/2014 20:20:00"

    Usually this error occurs at Convert.ToDateTime(DateTimeInStringFormat);

    My local computer consider '29' as day and my server consider '29' as a month.

    ie. at my local server date format is 'dd/MM/yyyy HH:mm:ss'

    and at server it is 'MM/dd/yyyy HH:mm:ss'

    There is lot of working solutions i found in a search. I am listing few of them

    Sol Using Parse Exact


    DateTime dt = DateTime.ParseExact(dr["dateCreated"].ToString().Trim(), 
     "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
     
    //dr["dateCreated"] returns  date from database
     
     

    Sol Using TryParse

     
    DateTime date4;
    string dateString = @"20/05/2012";
    bool result = DateTime.TryParse(dateString,out date4); 
    
    
    the parsing fails, but it will not throw error, rather it returns  
    false indicating that the parsing failed.
     
     
    
    

    Sol Using appropriate culture (I got this working)

    string dateString = @"20/05/2012";
    DateTime date2 = Convert.ToDateTime(dateString,
     System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);

     

    
    
     
    
    
  • Sunday, September 21, 2014

  • Setting up connection string in ASP.NET to SQL SERVER WEB.config Global

     
    In Web.config under <configuration> Tag
     
     
     
    <connectionStrings>
        <add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
      </connectionStrings>
     
     
     

    in C# code usage

     
      SqlCommand command;
            public static string connectionstring = ConfigurationManager.ConnectionStrings["ConnStringDb1"].ToString();
            SqlConnection conn = new SqlConnection(connectionstring); 
    
    
    Dont Forget to add "using System.Configuration;"
     


  • How to play a Sound in Android Java Eclipse



    MediaPlayer class can be used to control playback of audio/video files and streams.

     keep the intro.mp3 file in raw folder in res



     MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.intro);
        mp.start();
     
     
    mp.pause();// To pause / Stop music
     
     
     
     
  • C# Load Image from URL ASP

    Loading an image from a URL in C# is possible without much code. Downloading images off the internet can be done directly to memory without having to save them as a file. The image in memory can be written to disk later if necessary.



     The Logic Behind   is to wrap the raw data as a Stream. The System.IO namespace in C# has a useful class called MemoryStream. The MemoryStream C# class can be loaded with raw data that will be read like any other "file" stream, except the bytes are in memory.


    byte[] imageData = DownloadData(Url); //Url is image link
    MemoryStream stream = new MemoryStream(imageData);
    Image img = Image.FromStream(stream);
    stream.Close();
     string saveImagePath = System.Web.HttpContext.Current.Server.MapPath("../media/full/")  + "image.jpg"; 
     img.Save(saveImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);


  • Saturday, September 20, 2014

  • How to set read write permission to directory in C#.NET

    While working with files and directory we need read write permission on files and folders, this can be done by manually giving access permission to directory as well as programmatically.


    private static void SetPermissions(string dirPath)
    {
        DirectoryInfo info = new DirectoryInfo(dirPath);
        WindowsIdentity self = System.Security.Principal.WindowsIdentity.GetCurrent();
        DirectorySecurity ds = info.GetAccessControl();
        ds.AddAccessRule(new FileSystemAccessRule(self.Name,
        FileSystemRights.FullControl,
        InheritanceFlags.ObjectInherit |
        InheritanceFlags.ContainerInherit,
        PropagationFlags.None,
        AccessControlType.Allow));
        info.SetAccessControl(ds);
    }

     read-only on access on file
    FileInfo myFile = new FileInfo(files.FullName);
    myFile.IsReadOnly = false;
      File with Read Write permission

    FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);

  • Sunday, September 14, 2014

  • Convert Image to Base64 String and Base64 String to Image C#

    Image to Base64 String

    public string ImageToBase64(Image image, 
      System.Drawing.Imaging.ImageFormat format)
    {
      using (MemoryStream ms = new MemoryStream())
      {
        // Convert Image to byte[]
        image.Save(ms, format);
        byte[] imageBytes = ms.ToArray();
    
        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);
        return base64String;
      }
    }
     
     
     
     

    Base64 String to Image

     

    public Image Base64ToImage(string base64String)
    {
      // Convert Base64 String to byte[]
      byte[] imageBytes = Convert.FromBase64String(base64String);
      MemoryStream ms = new MemoryStream(imageBytes, 0, 
        imageBytes.Length);
    
      // Convert byte[] to Image
      ms.Write(imageBytes, 0, imageBytes.Length);
      Image image = Image.FromStream(ms, true);
      return image;
    }

     

     

     

  • Copyright @ 2013 Code Snippets.