Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was updating the data at the time I am getting this error. Please help me, where I did make a mistake ?

This is the Error: Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

VB
Private Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Submit.Click
Dim fileName As String = Path.GetFileName(AttachUpload.PostedFile.FileName)
                    Dim str As Stream = AttachUpload.PostedFile.InputStream
                    Dim br As New BinaryReader(str)
                    Dim FileType As String = AttachUpload.PostedFile.ContentType
                    Dim FileData As [Byte]() = br.ReadBytes(CInt(str.Length))
                    Using con As New SqlConnection(strConnString)
                        Using cmd As New SqlCommand()
                            cmd.CommandText = "Update BUSON_CALLLOG set UserId='@UserId',RpDateTime='@RpDateTime',CallSubject='@CallSubject', CallDescription='@CallDescription', Priority='@Priority', CallType='@CallType', [Status]='@Status', FeedBack='@FeedBack', FileName='@fileName',FileType='@FileType',FileData='@FileData' where DocEntry='" + CallId.Text + "'"
                            cmd.Parameters.AddWithValue("@UserId", lbluid.Text)
                            cmd.Parameters.AddWithValue("@RpDateTime", Date.Today)
                            cmd.Parameters.AddWithValue("@CallSubject", CallSubject.Text)
                            cmd.Parameters.AddWithValue("@CallDescription", CallDesc.Text)
                            cmd.Parameters.AddWithValue("@Priority", Priority.SelectedValue)
                            cmd.Parameters.AddWithValue("@CallType", CallType.SelectedValue)
                            cmd.Parameters.AddWithValue("@Status", "Open")
                            cmd.Parameters.AddWithValue("@FeedBack", Feedback.Text)
                            cmd.Parameters.AddWithValue("@fileName", fileName)
                            cmd.Parameters.AddWithValue("@FileType", FileType)
                            cmd.Parameters.AddWithValue("@FileData", FileData)
                            cmd.Connection = con
                            con.Open()
                            cmd.ExecuteNonQuery()
                            con.Close()
                        End Using
                    End Using
                    Errlbl0.Text = ""
                    'Sucs.Text = "Your Call Booked Successfully!! Our Consultant will respond you as soon as possible!!"
                    Dim body As String = Me.PopulateBody("Sir", _
                "Please Click hear and View the Quires", _
                "http://localhost:3482/consultantViewCalllog.aspx?UserId=" + CallId.Text & _
                "", _
                ("" & _
                " " & _
                ""))
                    Me.SendHtmlFormattedEmail("recipient@gmail.com", "Reg Serives Call!", body)
                    Sucs.Text = "Your Call has been Booked Successfully!! Our Consultant will respond you as soon as possible!!"
                    Response.Redirect("WorkSpace.aspx")
Posted
Updated 4-Jul-13 22:04pm
v2
Comments
Johnny J. 5-Jul-13 4:12am    
Which of the fields in the BUSON_CALLLOG table are of the varbinary type? Filedata?
DhananjayanP 5-Jul-13 4:13am    
Yes Filedata datatype is Varbinary(max)

1 solution

Take out the quotes!
C#
cmd.CommandText = "Update BUSON_CALLLOG set UserId='@UserId',RpDateTime='@RpDateTime',CallSubject='@CallSubject', CallDescription='@CallDescription', Priority='@Priority', CallType='@CallType', [Status]='@Status', FeedBack='@FeedBack', FileName='@fileName',FileType='@FileType',FileData='@FileData' where DocEntry='" + CallId.Text + "'"
Should be:
C#
cmd.CommandText = "Update BUSON_CALLLOG set UserId=@UserId,RpDateTime=@RpDateTime,CallSubject=@CallSubject, CallDescription=@CallDescription, Priority=@Priority, CallType=@CallType, [Status]=@Status, FeedBack=@FeedBack, FileName=@fileName,FileType=@FileType,FileData=@FileData where DocEntry='" + CallId.Text + "'"
Otherwise, they aren't parameters, they are text strings!

And parametrize that DocEntry/CallId bit as well - or SQL Injection is still possible on this query!
 
Share this answer
 
Comments
akshay walkhade 8-Jun-16 3:01am    
It worked. Thank you.
Member 13692796 14-Mar-18 4:48am    
Please help me on below as iam getting Implicit conversion from data type varchar to varbinary(max) is not allowed error

string strConn = string.Empty;
strConn = ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConn);
con.Close();
if (imageupload.HasFile)
{
string strname = imageupload.FileName.ToString();
imageupload.PostedFile.SaveAs(Server.MapPath("~/Images/") + strname);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into Publications values('" + txtPublicationName.Text + "','" + strname + "')";

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
lbltext.Visible = true;
lbltext.Text = "Image Uploaded successfully";
lbltext.Text = "";
}
else
{
lbltext.Visible = true;
lbltext.Text = "Plz upload the image!!!!";
}
con.Close();
OriginalGriff 14-Mar-18 5:15am    
This is a new question: post it as such.

https://www.codeproject.com/Questions/ask.aspx

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