fahad sheikhji

fahad sheikhji

  • NA
  • 53
  • 55.5k

instance failure while saving image to database?

Jul 22 2012 6:48 AM
hi am getting error while saving images to database..

here is my code

private void updatedata()
        {

            try
            {


                if (imagename != "")
                {
                    FileStream fs;
                    fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
                    byte[] picbyte = new byte[fs.Length];
                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                    string connstr = @"Data Source=FAHADSHEIKHJI\\FAHADSHEIKHJI;Initial Catalog=UAEExchangeShiroor;Integrated Security=True;";

                    SqlConnection conn = new SqlConnection(connstr);
                    conn.Open();
                    string query;
                    query = "insert into tblCustomerDetails(cdCustomerName,cdCustomerSex,cdCustomerPhNo,cdCustomerDOB,cdCustomerJoinDate,cdCustomerAge,cdCustomerIDProofNo,cdCustomerPhoto) values(" + tbName.Text + "," + tbSex.Text + "," + tbMobile.Text + "," + tbDateOfBirth.Text + "," + tbJoinDate.Text + "," + tbAge.Text + "," + tbIDProof.Text + "," + "@pic)";

                    SqlParameter picparameter = new SqlParameter();
                    picparameter.SqlDbType = SqlDbType.Image;
                    picparameter.ParameterName = "pic";
                    picparameter.Value = picbyte;
                    SqlCommand cmd = new SqlCommand(query, conn);
                    cmd.Parameters.Add(picparameter);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Record Added Successfully");
                    cmd.Dispose();
                    conn.Close();
                    conn.Dispose();
                    Connection();
                }
            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
        }

        private void Connection()
        {

            try
            {
                string connstr = @"Data Source=FAHADSHEIKHJI\\FAHADSHEIKHJI;Initial Catalog=UAEExchangeShiroor;Integrated Security=True;";
           
                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();
                empadap1 = new SqlDataAdapter();
                empadap1.SelectCommand = new SqlCommand("SELECT * FROM tblCustomerDetails", conn);
                dset = new DataSet("dset");
                empadap1.Fill(dset);
               DataTable dtable;
                dtable = dset.Tables[0];
                comboBox1.Items.Clear();
                foreach (DataRow drow in dtable.Rows)
                {
                    comboBox1.Items.Add(drow[0].ToString());
                  comboBox1.SelectedIndex = 0;

                }

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }

        }
//this to retrieve records from databse and binding id to comobox
        private void button4_Click(object sender, EventArgs e)
        {
            DataTable dataTable = dset.Tables[0];

        if (pcPhoto.Image != null)

        {
            pcPhoto.Image.Dispose();
        }
        FileStream FS1 = new FileStream("image.jpg", FileMode.Create);
        foreach (DataRow dataRow in dataTable.Rows)

        {
            if (dataRow[0].ToString() == comboBox1.SelectedItem.ToString())

            {

                byte[] blob = (byte[])dataRow[1];
                FS1.Write(blob, 0, blob.Length);
                FS1.Close();
                FS1 = null;
                pictureBox1.Image = Image.FromFile("image.jpg");
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Refresh();
            }
        }
        }

Update class am calling in btnsave click to save data in database



please help me to solve this error..
or provide me some easy code tat i can save image in sql server and retrieve image in datagrid ??

thank you


Answers (1)