none
What's wrong in my code??? RRS feed

  • Question

  • 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.

    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

    Friday, January 9, 2015 2:19 PM

Answers

  • I resolved this error of my self.......

    Thanks & Regards RAJENDRAN M

    • Proposed as answer by Cor Ligthert Saturday, January 10, 2015 2:38 PM
    • Marked as answer by Caillen Tuesday, January 20, 2015 2:33 AM
    Saturday, January 10, 2015 10:26 AM

All replies

  • What error are you getting? Are you catching a SqlException? Tell us how you know that your code is failing.
    Friday, January 9, 2015 3:16 PM
  • You have this:

    byte[] img = null;
    FileStream fs = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    img = br.ReadBytes((int)fs.Length);

    But you are adding these images:

    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;

    I don't know where those "...pic" variables are defined, but you're not adding the "img" variable that you just created.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, January 9, 2015 3:35 PM
  • Start with using the parameter AddWithValue instead of that old 2003 way.

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx


    Success
    Cor

    Friday, January 9, 2015 5:27 PM
  • I've created 5 pictureboxes  for my understanding purpose I've given the names for that in properties,

    1.vehicel image picturbox =boxvehpic

    2.customer image picturbox=boxcuspic

    3.guarantor image picturbox=boxguapic

    4.documents image picturbox=boxdocpic

    5.Insurance image picturebox=boxinspic


    Thanks & Regards RAJENDRAN M

    Saturday, January 10, 2015 5:47 AM
  • I'm getting Invalid cast exception runtime error....

    Thanks & Regards RAJENDRAN M

    Saturday, January 10, 2015 6:38 AM
  • I resolved this error of my self.......

    Thanks & Regards RAJENDRAN M

    • Proposed as answer by Cor Ligthert Saturday, January 10, 2015 2:38 PM
    • Marked as answer by Caillen Tuesday, January 20, 2015 2:33 AM
    Saturday, January 10, 2015 10:26 AM
  • We appreciate that if you can share your solution here, it'll help others who have similar questions.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 20, 2015 2:34 AM
  • First create 1 byte[] method in that you convert the image to byte format and you just call those methods wherever you are trying to store the images...

    Thanks & Regards RAJENDRAN M

    Saturday, January 24, 2015 11:05 AM