Click here to Skip to main content
15,917,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how ti solve this error?

i m getting error at cm.ExecuteNonQuery()that : Invalidcastexceptionwasunhealded

VB
Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
       Dim sampleImage As Bitmap = New Bitmap(100, 100)
       Dim mStream As New System.IO.MemoryStream
       Dim ImageBytes As Byte()

       sampleImage.Save(mStream, Imaging.ImageFormat.Png)
       ImageBytes = mStream.ToArray
       cn.Open()
       Dim cm As SqlCommand = New SqlCommand("insert into Empdetails(EmpName,EmpMobileNo,EmpEmail,EmpUsername,EmpPassword,EmpImage) values(@p1,@p2,@p3,@p4,@p5,@p6)", cn)
       cm.Parameters.Add("@p1", SqlDbType.VarChar).Value = txtname.Text
       cm.Parameters.Add("@p2", SqlDbType.VarChar).Value = txtmobile.Text
       cm.Parameters.Add("@p3", SqlDbType.VarChar).Value = txtemail.Text
       cm.Parameters.Add("@p4", SqlDbType.VarChar).Value = txtusername.Text
       cm.Parameters.Add("@p5", SqlDbType.VarChar).Value = txtpassword.Text
       cm.Parameters.Add("@p6", SqlDbType.Image).Value = pbimage.Image

       cm.ExecuteNonQuery()
       lblmsg.Text = "you have succesfully registered"
       cn.Close()
   End Sub
Posted
Comments
raki1111 21-Mar-12 1:14am    
Hi,
Instead of assigning the image directly , u have to assign the array of bytes .

1 solution

Hi,
try the following-
it uses Picturebox to get the Image.

VB
Try
                    con.ConnectionString = str
                    con.Open()
                    Dim cmd As SqlCommand = New SqlCommand("insert into Empdetails(EmpName,EmpMobileNo,EmpEmail,EmpUsername,EmpPassword,EmpImage) values(@p1,@p2,@p3,@p4,@p5,@p6)", cn)
                    ' Add code for other Parameter
                    ' Code for Image Parameter
                    Dim param As New SqlParameter("@ImageData", SqlDbType.VarBinary)
                    Dim ImageData As Byte() = IO.File.ReadAllBytes(PictureBox1.ImageLocation)
                    param.Value = ImageData
                    cmd.Parameters.Add(param)
                    cmd.ExecuteNonQuery()
                    MsgBox("You are Registered!")
                Catch ex As Exception
                    MsgBox(ex.Message)
                Finally
                    con.Close()
                End Try


If any problem persists let me know.

Thanks,
 
Share this answer
 
v4

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