Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, January 24, 2014

LINQ to Untyped DataSet Samples


SAMPLE:1

VB.NET

Dim query = From em In ds.Tables("Employees").AsEnumerable() _
            Where em.Field(Of String)("FirstName") = "Bijo" _

            Select em

'To display the results
   For Each em In query
       Console.WriteLine("Name = {1} {2}", _
                em.Field(Of String)("FirstName"), _
                em.Field(Of String)("LastName"))

        Next


C#

var query = from em in ds.Tables("Employees").AsEnumerable() _
            where em.Field<string>("FirstName") == "Bijo"

            select em.Field<string>(“FirstName”);



C# METHOD-2

var query = ds.Tables("Employees").AsEnumerable()
    .Where(dr => dr.Field<string>("FirstName") == "Bijo")

    .Select(dr => dr.Field<string>("FirstName"));







Tuesday, October 15, 2013

Upload a file using Web Service

Upload a file using byte array :

Web service method:


  [WebMethod]
        public string UploadFile(byte[] f, string fileName)
        {
            // the byte array argument contains the content of the file and 
            //the string argument contains the name and extension
            try
            {
                MemoryStream ms = new MemoryStream(f);

                //open a file stream pointing the storage folder 'uFiles'

                FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
                            ("~/uFiles/") + fileName, FileMode.Create);

                // write the memory stream  to the filestream
                ms.WriteTo(fs);
               
                ms.Close();
                fs.Close();
                fs.Dispose();
                return "OK";
            }

            catch (Exception ex)
            {
                     return ex.Message.ToString();
            }
        }


Sample win application code which consumes the method.


 FileInfo fInfo = new FileInfo(<<path & filename>>);

 FileStream fStream = new FileStream(<<path & filename>>,FileMode.Open, FileAccess.Read);

 BinaryReader br = new BinaryReader(fStream);
                 
 // convert the file to a byte array
 byte[] data = br.ReadBytes((int) fInfo.Length);
 br.Close();

localhost.Service1 s = new localhost.Service1();

s.UploadFile(data, <<filename>>);

 fStream.Close();

fStream.Dispose();





Upload an image using web service


Using Base64String:


Soap Web Service Method:-

 [WebMethod]
        public string UploadImage(string imgBase64String, string fname)
        {
            try
            {
                //Converts the base64String to byte array
                byte[] imageBytes = Convert.FromBase64String(imgBase64String);

                //store it in a memory stream
                MemoryStream ms1 = new MemoryStream(imageBytes);

                //generate image from stream
                Image img = Image.FromStream(ms1);

                //Map the folder to store image
                string actFolder = Server.MapPath("~/uFiles/");

                //save the image
                img.Save(actFolder + fname);

                return "OK";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }



Sample Win application code which consumes above service

// don't forget to add the web reference before using this code. 
//here the reference name is localhost and service name is Service 1 

            string base64;
            using (MemoryStream ms = new MemoryStream())
            {

                Image ximg = Image.FromFile(<<filepath>>);
                ximg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                base64 = Convert.ToBase64String(ms.ToArray());
               
                localhost.Service1 S1 = new localhost.Service1();

               string r = S1.UploadImage(base64, "MyFile.Jpg");

               MessageBox.Show(r);

            }



Thursday, March 14, 2013

Update a ConnectionString Stored in App.Config File

        using System.Configuration;
        public void updateConnectionString()
        {
           //create a new connection string settings
            ConnectionStringSettings cSetting = new ConnectionStringSettings();

            //connection string
            cSetting.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";
            //connection settings name in conf. file
            cSetting.Name = "WindowsFormsApplication1.Properties.Settings.conStr";
            //Provider
            cSetting.ProviderName = "MySql.Data.MySqlClient";

            //get the application conf file
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
           
            //read the connection string section
            ConnectionStringsSection connectionSettings = (ConnectionStringsSection)config.GetSection("connectionStrings");

            //remove the current conf section and add new
            connectionSettings.ConnectionStrings.Remove(cSetting);
            connectionSettings.ConnectionStrings.Add(cSetting);

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

            //to read and view
            //string newString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.conStr"].ConnectionString;
            //System.Diagnostics.Debug.Print(newString);
        }