(Failed to convert parameter value from a Bitmap to a Byte[]) Error while inserting i

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaliniraji
    New Member
    • Jan 2015
    • 15

    (Failed to convert parameter value from a Bitmap to a Byte[]) Error while inserting i

    Hi everyone,

    I've created 1 windows app in that app i've created Registration form including image fields.After filling all the details in that form when i click on save button i'm runtime error(Failed to convert parameter value from a Bitmap to a Byte[])..Can anyone tell me how to resolve this issue..I'm try to resolve this from since last 4days.I posted the question on so many forums but no use atleast i came to bytes forum..I hope I'll get the solution from this forum.


    This is the code which I've written under button method::

    Code:
      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[] photo = GetPhoto(imageloc);
    
                            cmd.CommandType = CommandType.StoredProcedure;
                            
                            SqlParameter p1 = new SqlParameter("@Hp_Number", SqlDbType.NVarChar, 50);
                            SqlParameter p2 = new SqlParameter("@Customer_Name", SqlDbType.VarChar, 50);
                            SqlParameter p3 = new SqlParameter("@Customer_Contact_Number", SqlDbType.NVarChar, 15);
                            SqlParameter p4 = new SqlParameter("@Guarantor_Name", SqlDbType.VarChar, 50);
                            SqlParameter p5 = new SqlParameter("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15);
                            SqlParameter p6 = new SqlParameter("@Hp_Date", SqlDbType.Date);
                            SqlParameter p7 = new SqlParameter("@Customer_Address", SqlDbType.NVarChar, Max);
                            SqlParameter p8 = new SqlParameter("@Vehicle", SqlDbType.VarChar, 50);
                            SqlParameter p9 = new SqlParameter("@Vehicle_Model", SqlDbType.VarChar, 50);
                            SqlParameter p10 = new SqlParameter("@Vehicle_Number", SqlDbType.NVarChar, 50);
                            SqlParameter p11 = new SqlParameter("@Chasis_Number", SqlDbType.NVarChar, 50);
                            SqlParameter p12 = new SqlParameter("@Engine_Number", SqlDbType.NVarChar, 50);
                            SqlParameter p13 = new SqlParameter("@FC_Date", SqlDbType.Date);
                            SqlParameter p14 = new SqlParameter("@Insurance_Date", SqlDbType.Date);
                            SqlParameter p15 = new SqlParameter("@Insurance_Amount", SqlDbType.Int);
                            SqlParameter p16 = new SqlParameter("@Paid_Amount", SqlDbType.Int);
                            SqlParameter p17 = new SqlParameter("@Vehicle_Pic",SqlDbType.Image,photo.Length);
                            SqlParameter p18 = new SqlParameter("@Customer_Pic ", SqlDbType.Image, photo.Length);
                            SqlParameter p19 = new SqlParameter("@Guarantor_Pic ", SqlDbType.Image, photo.Length);
                            SqlParameter p20 = new SqlParameter("@Documents_Pic", SqlDbType.Image, photo.Length);
                            SqlParameter p21 = new SqlParameter("@Insurance_Pic", SqlDbType.Image, photo.Length);
    
                            cmd.Parameters.Add(p1).Value = tbhpnum.Text; 
                            cmd.Parameters.Add(p2).Value = tbcusnam.Text; 
                            cmd.Parameters.Add(p3).Value=tbcusmblno.Text;
                            cmd.Parameters.Add(p4).Value = tbguanam.Text;
                            cmd.Parameters.Add(p5).Value = tbguamblno.Text;
                            cmd.Parameters.Add(p6).Value = DateTime.Parse(tbhpdat.Text);
                            cmd.Parameters.Add(p7).Value = tbcusadd.Text;
                            cmd.Parameters.Add(p8).Value = tbveh.SelectedItem.ToString();
                            cmd.Parameters.Add(p9).Value = tbvehmod.SelectedItem.ToString();
                            cmd.Parameters.Add(p10).Value = tbvehnum.Text;
                            cmd.Parameters.Add(p11).Value = tbchanum.Text;
                            cmd.Parameters.Add(p12).Value = tbengnum.Text;
                            cmd.Parameters.Add(p13).Value = DateTime.Parse(tbfcdat.Text);
                            cmd.Parameters.Add(p14).Value = DateTime.Parse(tbinsdat.Text);
                            cmd.Parameters.Add(p15).Value = Convert.ToInt32(tbinsamt.Text);
                            cmd.Parameters.Add(p16).Value = Convert.ToInt32(tbpaiamt.Text);
                            cmd.Parameters.Add(p17).Value = boxvehpic;
                            cmd.Parameters.Add(p18).Value = boxcuspic;
                            cmd.Parameters.Add(p19).Value = boxguapic;
                            cmd.Parameters.Add(p20).Value=boxdocpic;
                            cmd.Parameters.Add(p21).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();
                        }
                    }
                }
            }
    External Byte[] method code::
    Code:
     public static byte[] GetPhoto(string imageloc)
            {
                byte[] photo= null;
                FileStream stream = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(stream);
                photo= reader.ReadBytes((int)stream.Length);
    
                reader.Close();
                stream.Close();
    
                return photo;
            }
    Stored Procedure::
    Code:
    create PROC SP_Info
    (
      @Hp_Number nvarchar(50),
      @Customer_Name varchar(50),
      @Customer_Contact_Number nvarchar(15),
      @Guarantor_Name varchar(50),
      @Guarantor_Contact_Number nvarchar(15),
      @Hp_Date date,
      @Customer_Address nvarchar(MAX),
      @Vehicle varchar(50),
      @Vehicle_Model varchar(50),
      @Vehicle_Number nvarchar(50),
      @Chasis_Number nvarchar(50),
      @Engine_Number nvarchar(50),
      @FC_Date date,
      @Insurance_Date date,
      @Insurance_Amount int,
      @Paid_Amount int,
      @Vehicle_Pic image,
      @Customer_Pic image,
      @Guarantor_Pic  image,
      @Documents_Pic image,
      @Insurance_Pic image
    )
    AS
    BEGIN
     Insert into Info values(@Hp_Number,
      @Customer_Name,
      @Customer_Contact_Number,
      @Guarantor_Name,
      @Guarantor_Contact_Number,
      @Hp_Date,
      @Customer_Address,
      @Vehicle,
      @Vehicle_Model,
      @Vehicle_Number,
      @Chasis_Number,
      @Engine_Number,
      @FC_Date,
      @Insurance_Date,
      @Insurance_Amount,
      @Paid_Amount,
      @Vehicle_Pic,
      @Customer_Pic,
      @Guarantor_Pic,
      @Documents_Pic,
      @Insurance_Pic)
    END
    Last edited by Frinavale; Jan 8 '15, 05:24 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Lines 52-56:
    Code:
    cmd.Parameters.Add(p17).Value = boxvehpic;
    cmd.Parameters.Add(p18).Value = boxcuspic;
    cmd.Parameters.Add(p19).Value = boxguapic;
    cmd.Parameters.Add(p20).Value=boxdocpic;
    cmd.Parameters.Add(p21).Value = boxinspic;
    What are boxcuspic, boxguapic,boxdocpic and boxinspic?
    Last edited by Frinavale; Jan 9 '15, 08:36 PM.

    Comment

    • shaliniraji
      New Member
      • Jan 2015
      • 15

      #3
      In Registration form I used 5 picture boxes and for my understanding purpose I gave the names for that picture boxes..

      1.boxvehpic = vehicle picture box
      2.boxcuspic = customer picture box
      3.boxguapic= guarantor picture box
      4.boxdocpic= Document picture box
      5.boxinspic= Insurance doc picture box

      Comment

      • shaliniraji
        New Member
        • Jan 2015
        • 15

        #4
        I changed my code like this,Eventhough no use still same error is persisting.....

        Code:
        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();
                            }
                        }
                    }
                }
        Last edited by Rabbit; Jan 9 '15, 04:43 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data. Second Warning.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          A picture box control is not a byte array.

          You need to retrieve the picture from the picture box control and convert it into a byte array before you attempt to update the database with a byte array.

          This is why you are getting the error message:
          Failed to convert parameter value from a Bitmap to a Byte[]

          -Frinny

          Comment

          • shaliniraji
            New Member
            • Jan 2015
            • 15

            #6
            I resolved my error of my self...

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Could you please share what you did to resolve the problem so that other people facing the same problem can find a solution?

              Thanks
              -Frinny

              Comment

              Working...