Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi Everyone..

Can any one tell me whats wrong in my code...I would like to store images into my database, but I'm not able to do that.

C#
private void btnaddinfo_Click(object sender, EventArgs e)
        {
            string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(scn))
                {
                using (SqlCommand cmd = new SqlCommand("SP_Info", cn))
                {
                    try
                    { 
                        byte[] img = null;
                        FileStream fs = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
                        BinaryReader br = new BinaryReader(fs);
                        img = br.ReadBytes((int)fs.Length);     

                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.Add("@Hp_Number", SqlDbType.NVarChar, 50).Value = tbhpnum.Text; ;
                        cmd.Parameters.Add("@Customer_Name", SqlDbType.VarChar, 50).Value = tbcusnam.Text;
                        cmd.Parameters.Add("@Customer_Contact_Number", SqlDbType.NVarChar, 15).Value = tbcusmblno.Text;
                        cmd.Parameters.Add("@Guarantor_Name", SqlDbType.VarChar, 50).Value = tbguanam.Text;
                        cmd.Parameters.Add("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15).Value = tbguamblno.Text;
                        cmd.Parameters.Add("@Hp_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbhpdat.Text);
                        cmd.Parameters.Add("@Customer_Address", SqlDbType.NVarChar, Max).Value = tbcusadd.Text;
                        cmd.Parameters.Add("@Vehicle", SqlDbType.VarChar, 50).Value = tbveh.SelectedItem.ToString();
                        cmd.Parameters.Add("@Vehicle_Model", SqlDbType.VarChar, 50).Value = tbvehmod.SelectedItem.ToString();
                        cmd.Parameters.Add("@Vehicle_Number", SqlDbType.NVarChar, 50).Value = tbvehnum.Text;
                        cmd.Parameters.Add("@Chasis_Number", SqlDbType.NVarChar, 50).Value = tbchanum.Text;
                        cmd.Parameters.Add("@Engine_Number", SqlDbType.NVarChar, 50).Value = tbengnum.Text;
                        cmd.Parameters.Add("@FC_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbfcdat.Text);
                        cmd.Parameters.Add("@Insurance_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbinsdat.Text);
                        cmd.Parameters.Add("@Insurance_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbinsamt.Text);
                        cmd.Parameters.Add("@Paid_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbpaiamt.Text);
                        cmd.Parameters.Add("@Vehicle_Pic", SqlDbType.Image).Value = boxvehpic;
                        cmd.Parameters.Add("@Customer_Pic", SqlDbType.Image).Value = boxcuspic;
                        cmd.Parameters.Add("@Guarantor_Pic", SqlDbType.Image).Value = boxguapic;
                        cmd.Parameters.Add("@Documents_Pic", SqlDbType.Image).Value = boxdocpic;
                        cmd.Parameters.Add("@Insurance_Pic", SqlDbType.Image).Value = boxinspic;

                        if (cn.State != ConnectionState.Open)
                            cn.Open();
                        int count = cmd.ExecuteNonQuery();
                        if (count == 1)
                        {
                            MessageBox.Show(count.ToString() + "Record(s) Saved.");
                        }
                        else
                        {
                            MessageBox.Show("Please correct the error which you have entered and then click on addinformation button");
                        }
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.ToString());

                    }
                    finally
                    {
                        if (cn.State == ConnectionState.Open)
                            cn.Close();
                    }
                }
            }
        }


Thanks & Regards RAJENDRAN M
Posted
Updated 9-Jan-15 3:47am
v2
Comments
Afzaal Ahmad Zeeshan 9-Jan-15 9:50am    
Would you like to share what kind of error are you getting?
PIEBALDconsult 9-Jan-15 9:55am    
Not part of ther problem, but change all the parameter statements from
cmd.Parameters.Add("@Hp_Number", SqlDbType.NVarChar, 50).Value = tbhpnum.Text;
to
cmd.Parameters.AddWithValue("@Hp_Number", tbhpnum.Text0 ) ;
there is no need to set the type.

1 solution

As I suggested yesterday when you asked this...perhaps it would be better to use teh image data instead of a random value.

boxvehpic, boxcuspic, boxguapic, etc. aren't set in your method, so gawd knows what you are posting as images to your DB.
Yesterday, it was the length of the image data that you tried to store as an image.

Why don't you try the actual image data? You load it at the top of the method and then ignore it completely this time!
Please, start to think about what the code does instead of just grabbing code from the internet and making random modifications... As the man said: "in software, hope and pray is not a viable strategy" (Luc Pattyn)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900