473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

(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::

Expand|Select|Wrap|Line Numbers
  1.   private void btnaddinfo_Click(object sender, EventArgs e)
  2.         {
  3.             string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
  4.             using (SqlConnection cn = new SqlConnection(scn))
  5.             {
  6.                 using (SqlCommand cmd = new SqlCommand("SP_Info", cn))
  7.                 {
  8.                     try
  9.                     {
  10.                         byte[] photo = GetPhoto(imageloc);
  11.  
  12.                         cmd.CommandType = CommandType.StoredProcedure;
  13.  
  14.                         SqlParameter p1 = new SqlParameter("@Hp_Number", SqlDbType.NVarChar, 50);
  15.                         SqlParameter p2 = new SqlParameter("@Customer_Name", SqlDbType.VarChar, 50);
  16.                         SqlParameter p3 = new SqlParameter("@Customer_Contact_Number", SqlDbType.NVarChar, 15);
  17.                         SqlParameter p4 = new SqlParameter("@Guarantor_Name", SqlDbType.VarChar, 50);
  18.                         SqlParameter p5 = new SqlParameter("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15);
  19.                         SqlParameter p6 = new SqlParameter("@Hp_Date", SqlDbType.Date);
  20.                         SqlParameter p7 = new SqlParameter("@Customer_Address", SqlDbType.NVarChar, Max);
  21.                         SqlParameter p8 = new SqlParameter("@Vehicle", SqlDbType.VarChar, 50);
  22.                         SqlParameter p9 = new SqlParameter("@Vehicle_Model", SqlDbType.VarChar, 50);
  23.                         SqlParameter p10 = new SqlParameter("@Vehicle_Number", SqlDbType.NVarChar, 50);
  24.                         SqlParameter p11 = new SqlParameter("@Chasis_Number", SqlDbType.NVarChar, 50);
  25.                         SqlParameter p12 = new SqlParameter("@Engine_Number", SqlDbType.NVarChar, 50);
  26.                         SqlParameter p13 = new SqlParameter("@FC_Date", SqlDbType.Date);
  27.                         SqlParameter p14 = new SqlParameter("@Insurance_Date", SqlDbType.Date);
  28.                         SqlParameter p15 = new SqlParameter("@Insurance_Amount", SqlDbType.Int);
  29.                         SqlParameter p16 = new SqlParameter("@Paid_Amount", SqlDbType.Int);
  30.                         SqlParameter p17 = new SqlParameter("@Vehicle_Pic",SqlDbType.Image,photo.Length);
  31.                         SqlParameter p18 = new SqlParameter("@Customer_Pic ", SqlDbType.Image, photo.Length);
  32.                         SqlParameter p19 = new SqlParameter("@Guarantor_Pic ", SqlDbType.Image, photo.Length);
  33.                         SqlParameter p20 = new SqlParameter("@Documents_Pic", SqlDbType.Image, photo.Length);
  34.                         SqlParameter p21 = new SqlParameter("@Insurance_Pic", SqlDbType.Image, photo.Length);
  35.  
  36.                         cmd.Parameters.Add(p1).Value = tbhpnum.Text; 
  37.                         cmd.Parameters.Add(p2).Value = tbcusnam.Text; 
  38.                         cmd.Parameters.Add(p3).Value=tbcusmblno.Text;
  39.                         cmd.Parameters.Add(p4).Value = tbguanam.Text;
  40.                         cmd.Parameters.Add(p5).Value = tbguamblno.Text;
  41.                         cmd.Parameters.Add(p6).Value = DateTime.Parse(tbhpdat.Text);
  42.                         cmd.Parameters.Add(p7).Value = tbcusadd.Text;
  43.                         cmd.Parameters.Add(p8).Value = tbveh.SelectedItem.ToString();
  44.                         cmd.Parameters.Add(p9).Value = tbvehmod.SelectedItem.ToString();
  45.                         cmd.Parameters.Add(p10).Value = tbvehnum.Text;
  46.                         cmd.Parameters.Add(p11).Value = tbchanum.Text;
  47.                         cmd.Parameters.Add(p12).Value = tbengnum.Text;
  48.                         cmd.Parameters.Add(p13).Value = DateTime.Parse(tbfcdat.Text);
  49.                         cmd.Parameters.Add(p14).Value = DateTime.Parse(tbinsdat.Text);
  50.                         cmd.Parameters.Add(p15).Value = Convert.ToInt32(tbinsamt.Text);
  51.                         cmd.Parameters.Add(p16).Value = Convert.ToInt32(tbpaiamt.Text);
  52.                         cmd.Parameters.Add(p17).Value = boxvehpic;
  53.                         cmd.Parameters.Add(p18).Value = boxcuspic;
  54.                         cmd.Parameters.Add(p19).Value = boxguapic;
  55.                         cmd.Parameters.Add(p20).Value=boxdocpic;
  56.                         cmd.Parameters.Add(p21).Value = boxinspic;
  57.  
  58.                         if (cn.State != ConnectionState.Open)
  59.                             cn.Open();
  60.                         int count = cmd.ExecuteNonQuery();
  61.                         if (count == 1)
  62.                         {
  63.                             MessageBox.Show(count.ToString() + "Record(s) Saved.");
  64.                         }
  65.                         else
  66.                         {
  67.                             MessageBox.Show("Please correct the error which you have entered and then click on addinformation button");
  68.                         }
  69.                     }
  70.                     catch (SqlException ex)
  71.                     {
  72.                         MessageBox.Show(ex.ToString());
  73.  
  74.                     }
  75.                     finally
  76.                     {
  77.                         if (cn.State == ConnectionState.Open)
  78.                             cn.Close();
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.  
  85.  
External Byte[] method code::
Expand|Select|Wrap|Line Numbers
  1.  public static byte[] GetPhoto(string imageloc)
  2.         {
  3.             byte[] photo= null;
  4.             FileStream stream = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
  5.             BinaryReader reader = new BinaryReader(stream);
  6.             photo= reader.ReadBytes((int)stream.Length);
  7.  
  8.             reader.Close();
  9.             stream.Close();
  10.  
  11.             return photo;
  12.         }
  13.  
  14.  
Stored Procedure::
Expand|Select|Wrap|Line Numbers
  1. create PROC SP_Info
  2. (
  3.   @Hp_Number nvarchar(50),
  4.   @Customer_Name varchar(50),
  5.   @Customer_Contact_Number nvarchar(15),
  6.   @Guarantor_Name varchar(50),
  7.   @Guarantor_Contact_Number nvarchar(15),
  8.   @Hp_Date date,
  9.   @Customer_Address nvarchar(MAX),
  10.   @Vehicle varchar(50),
  11.   @Vehicle_Model varchar(50),
  12.   @Vehicle_Number nvarchar(50),
  13.   @Chasis_Number nvarchar(50),
  14.   @Engine_Number nvarchar(50),
  15.   @FC_Date date,
  16.   @Insurance_Date date,
  17.   @Insurance_Amount int,
  18.   @Paid_Amount int,
  19.   @Vehicle_Pic image,
  20.   @Customer_Pic image,
  21.   @Guarantor_Pic  image,
  22.   @Documents_Pic image,
  23.   @Insurance_Pic image
  24. )
  25. AS
  26. BEGIN
  27.  Insert into Info values(@Hp_Number,
  28.   @Customer_Name,
  29.   @Customer_Contact_Number,
  30.   @Guarantor_Name,
  31.   @Guarantor_Contact_Number,
  32.   @Hp_Date,
  33.   @Customer_Address,
  34.   @Vehicle,
  35.   @Vehicle_Model,
  36.   @Vehicle_Number,
  37.   @Chasis_Number,
  38.   @Engine_Number,
  39.   @FC_Date,
  40.   @Insurance_Date,
  41.   @Insurance_Amount,
  42.   @Paid_Amount,
  43.   @Vehicle_Pic,
  44.   @Customer_Pic,
  45.   @Guarantor_Pic,
  46.   @Documents_Pic,
  47.   @Insurance_Pic)
  48. END
Jan 8 '15 #1
6 3432
Frinavale
9,735 Expert Mod 8TB
Lines 52-56:
Expand|Select|Wrap|Line Numbers
  1. cmd.Parameters.Add(p17).Value = boxvehpic;
  2. cmd.Parameters.Add(p18).Value = boxcuspic;
  3. cmd.Parameters.Add(p19).Value = boxguapic;
  4. cmd.Parameters.Add(p20).Value=boxdocpic;
  5. cmd.Parameters.Add(p21).Value = boxinspic;
What are boxcuspic, boxguapic,boxdocpic and boxinspic?
Jan 8 '15 #2
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
Jan 9 '15 #3
I changed my code like this,Eventhough no use still same error is persisting.....

Expand|Select|Wrap|Line Numbers
  1. private void btnaddinfo_Click(object sender, EventArgs e)
  2.         {
  3.             string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
  4.             using (SqlConnection cn = new SqlConnection(scn))
  5.                 {
  6.                 using (SqlCommand cmd = new SqlCommand("SP_Info", cn))
  7.                 {
  8.                     try
  9.                     { 
  10.                         byte[] img = null;
  11.                         FileStream fs = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
  12.                         BinaryReader br = new BinaryReader(fs);
  13.                         img = br.ReadBytes((int)fs.Length);     
  14.  
  15.                         cmd.CommandType = CommandType.StoredProcedure;
  16.  
  17.                         cmd.Parameters.Add("@Hp_Number", SqlDbType.NVarChar, 50).Value = tbhpnum.Text; ;
  18.                         cmd.Parameters.Add("@Customer_Name", SqlDbType.VarChar, 50).Value = tbcusnam.Text;
  19.                         cmd.Parameters.Add("@Customer_Contact_Number", SqlDbType.NVarChar, 15).Value = tbcusmblno.Text;
  20.                         cmd.Parameters.Add("@Guarantor_Name", SqlDbType.VarChar, 50).Value = tbguanam.Text;
  21.                         cmd.Parameters.Add("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15).Value = tbguamblno.Text;
  22.                         cmd.Parameters.Add("@Hp_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbhpdat.Text);
  23.                         cmd.Parameters.Add("@Customer_Address", SqlDbType.NVarChar, Max).Value = tbcusadd.Text;
  24.                         cmd.Parameters.Add("@Vehicle", SqlDbType.VarChar, 50).Value = tbveh.SelectedItem.ToString();
  25.                         cmd.Parameters.Add("@Vehicle_Model", SqlDbType.VarChar, 50).Value = tbvehmod.SelectedItem.ToString();
  26.                         cmd.Parameters.Add("@Vehicle_Number", SqlDbType.NVarChar, 50).Value = tbvehnum.Text;
  27.                         cmd.Parameters.Add("@Chasis_Number", SqlDbType.NVarChar, 50).Value = tbchanum.Text;
  28.                         cmd.Parameters.Add("@Engine_Number", SqlDbType.NVarChar, 50).Value = tbengnum.Text;
  29.                         cmd.Parameters.Add("@FC_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbfcdat.Text);
  30.                         cmd.Parameters.Add("@Insurance_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbinsdat.Text);
  31.                         cmd.Parameters.Add("@Insurance_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbinsamt.Text);
  32.                         cmd.Parameters.Add("@Paid_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbpaiamt.Text);
  33.                         cmd.Parameters.Add("@Vehicle_Pic", SqlDbType.Image).Value = boxvehpic;
  34.                         cmd.Parameters.Add("@Customer_Pic", SqlDbType.Image).Value = boxcuspic;
  35.                         cmd.Parameters.Add("@Guarantor_Pic", SqlDbType.Image).Value = boxguapic;
  36.                         cmd.Parameters.Add("@Documents_Pic", SqlDbType.Image).Value = boxdocpic;
  37.                         cmd.Parameters.Add("@Insurance_Pic", SqlDbType.Image).Value = boxinspic;
  38.  
  39.                         if (cn.State != ConnectionState.Open)
  40.                             cn.Open();
  41.                         int count = cmd.ExecuteNonQuery();
  42.                         if (count == 1)
  43.                         {
  44.                             MessageBox.Show(count.ToString() + "Record(s) Saved.");
  45.                         }
  46.                         else
  47.                         {
  48.                             MessageBox.Show("Please correct the error which you have entered and then click on addinformation button");
  49.                         }
  50.                     }
  51.                     catch (SqlException ex)
  52.                     {
  53.                         MessageBox.Show(ex.ToString());
  54.  
  55.                     }
  56.                     finally
  57.                     {
  58.                         if (cn.State == ConnectionState.Open)
  59.                             cn.Close();
  60.                     }
  61.                 }
  62.             }
  63.         }
Jan 9 '15 #4
Frinavale
9,735 Expert Mod 8TB
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
Jan 9 '15 #5
I resolved my error of my self...
Jan 10 '15 #6
Frinavale
9,735 Expert Mod 8TB
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
Jan 11 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Igor Okulist | last post by:
int func(void**); { short* p = NULL; func(&p); //<<< here } Could somebody remind me why is this not allowed ? error message: "cannot convert parameter from 'short **' to 'void **'"
0
by: Chris Crowe [MVP 1997 -> 2006] | last post by:
I am having major problems using GetStoredProcCommand and using Parameters in the same function call in the latest builds of the Enterprise Library Data. The error I get is : Failed to...
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
2
by: mikejacobz | last post by:
Hi, I am using the Enterprise Application Blocks (Data Access Application Block 2005) and all I want to do is pass in a date as a parameter to a Stored procedure. e.g private void...
0
by: weird0 | last post by:
Here is the code for executing stored procedure ExecuteSP() , and object that sets all its parameters in other functions and the stored procedure. I think it has something to do with db datatypes...
4
by: sweatha | last post by:
Hi In sql server 2005 I have a column as GraduationYear Decimal(4,0). In asp.net I used the coding for that as SqlParameter GraduationYear = new SqlParameter("@GraduationYear",...
3
by: balajius | last post by:
I am using VS2008 with asp.net 3.5 and enterprise library 4.1. I am getting the following error when I try to connect to database. "Failed to convert parameter value from a SqlParameter to a String"...
0
by: androm | last post by:
when i run my code i get this error message "Failed to convert parameter value from a SqlParameter to a DateTime." & i couldn't find what is wrong..... here's my code protected void...
4
by: dougancil | last post by:
I'm trying to pass values in a SessionState from one page to another and then post them to a SQL database. When I click on the submit button on my submit page, I get this error: Failed to...
1
by: Mickle | last post by:
here is my code, I was trying to convert arraybyte to bitmap after placing some value in array and convert it back to bitmap, actually i'm working with bilinear interpolation. i really appreciate...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.