Wednesday, October 12, 2011

Transaction in c#


SqlConnection con = new SqlConnection("Connection String");

     SqlCommand command1 = new SqlCommand(); //properly initiliased command with sql text or procedure name
            SqlCommand command2 = new SqlCommand(); //properly initiliased command with sql text or procedure name

            SqlTransaction trx = conn.BeginTransaction();
            command1.Transaction = trx;
            command2.Transaction = trx;

            try
            {
                command1.ExecuteNonQuery();
                command2.ExecuteNonQuery();
                trx.Commit();
            }
            catch (SqlException ex)
            {
                trx.Rollback();
            }
            finally
            {
                //clean up resources
            }

Tuesday, October 11, 2011

How to Split String


string mystring = "abc,124,cambridge";
string[] arr_String = mystring.Split(new char[] { ',' });//store string in an array
textbox1.Text = arr_String[0].ToString();//display string in textbox of index 0
textbox2.Text = arr_String[1].ToString();
textbox3.Text = arr_String[2].ToString();


Saturday, October 8, 2011

How to pass value one form to another

1. write code for form2


public string value1
        {
            get { return value1; }
            set { value1= value; }
        }
public string value2
        {
            get { return value2; }
            set { value2 = value; }
        }
private void form2_Load(object sender, EventArgs e)
        {
         }


2. then write code below to form1


private void btnPass_Click(object sender, EventArgs e)
        {

         Form2 frm2 = new Form2();
         frm2.value1=textbox1.text;
         frm2.value2=textbox2.text;
         frm2.show();
         
        }

3. write the code below to form_load event of form2

private void form2_Load(object sender, EventArgs e)
        {

         textbox1.text=this.value1;
         textbox2.text=this.value2;
         
         }

Save image from picture box to database sqlserver



1. create table in database

CREATE TABLE [dbo].[tbl_Image_Data] (



        [ID] [int] NOT NULL ,

        [Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

        [Picture] [image] NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]



2. add picturebox and name it pb and two buttons "browse" and "save"



3. First of all browse image from computer to pictureBox

private void btnBrowse_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
           
                OpenFileDialog openFileDialog = new OpenFileDialog();
               // openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                openFileDialog.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
                if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        if ((myStream = openFileDialog.OpenFile()) != null)
                        {
                            string FileName = openFileDialog.FileName;
                            txtImagepath.Text = FileName;
                            if (myStream.Length > 512000)
                            {
                                MessageBox.Show("File size limit exceed");
                            }
                            else
                            {
                                //pb is my pictureBox name load image in pb
                                pb.Load(FileName);
                             
                                //stretch image to fit in picturebox
                                pb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                            }
                            //MessageBox.Show(myStream.Length.ToString());
                        }
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show("Error: Could not read file from disk. error: " + ex.Message);

                    }
                }
             

               

        }
2.save this to sql server


private void btnSave_Click(object sender, EventArgs e)
 {
    MemoryStream stream = new MemoryStream();
                        pb.Image.Save(stream, ImageFormat.Jpeg);

                        byte[] pic = stream.ToArray();
                      byte_Img=pic;
                }

            try
            {
                this.sqlConnection1.Open();
                if (sqlCommand1.Parameters.Count == 0)
                {
                    this.sqlCommand1.CommandText = "INSERT INTO tbl_Image_Data(ID," +
                                   " Name,Picture) values(@ID,@Name,@Picture)";
                    this.sqlCommand1.Parameters.Add("@ID",
                                     System.Data.SqlDbType.Int, 4);
                    this.sqlCommand1.Parameters.Add("@Name",
                                     System.Data.SqlDbType.VarChar, 50);
                    this.sqlCommand1.Parameters.Add("@Picture",
                                     System.Data.SqlDbType.Image);
                }

                this.sqlCommand1.Parameters["@ID"].Value = this.ID.Text;
                this.sqlCommand1.Parameters["@Name"].Value = this.Name.Text;
                this.sqlCommand1.Parameters["@Picture"].Value = this.byte_Img;

                int result = this.sqlCommand1.ExecuteNonQuery();
                MessageBox.Show(Convert.ToString(result));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.sqlConnection1.Close();
            }

  }