I have created a stored procedure of insert command for employee details in SQL Server 2005 in which one of the parameters is an image for which I have used varbinary as the datatype in the table..
But when I am adding that parameter in the stored procedure I am getting the following error-
Implicit conversion from data type varchar to varbinary is not
allowed. Use the CONVERT function to run this query.
Stored procedure:
(
@Employee_ID nvarchar(10)='',
@Password nvarchar(10)='',
@Security_Question nvarchar(50)='',
@Answer nvarchar(50)='',
@First_Name nvarchar(20)='',
@Middle_Name nvarchar(20)='',
@Last_Name nvarchar(20)='',
@Employee_Type nvarchar(15)='',
@Department nvarchar(15)='',
@Photo varbinary(50)=''
)
insert into Registration
(
Employee_ID,
Password,
Security_Question,
Answer,
First_Name,
Middle_Name,
Last_Name,
Employee_Type,
Department,
Photo
)
values
(
@Employee_ID,
@Password,
@Security_Question,
@Answer,
@First_Name,
@Middle_Name,
@Last_Name,
@Employee_Type,
@Department,
@Photo
)
Table structure:
Column Name Data Type Allow Nulls
Employee_ID nvarchar(10) Unchecked
Password nvarchar(10) Checked
Security_Question nvarchar(50) Checked
Answer nvarchar(50) Checked
First_Name nvarchar(20) Checked
Middle_Name nvarchar(20) Checked
Last_Name nvarchar(20) Checked
Employee_Type nvarchar(15) Checked
Department nvarchar(15) Checked
Photo varbinary(50) Checked
Code in vb.net for calling stored procedure->
Public Function Submit(ByVal obj As UserData, ByVal opt As String) As Boolean
Using cnn As New SqlConnection(conn)
Using cmd As New SqlCommand
cmd.Connection = cnn
If opt = "Submit" Then
cmd.CommandText = "sp_emp_Registration"
End If
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@Employee_ID", obj.EmpID))
cmd.Parameters.Add(New SqlParameter("@Password", obj.Pwd))
cmd.Parameters.Add(New SqlParameter("@Security_Question", obj.SecQues))
cmd.Parameters.Add(New SqlParameter("@Answer", obj.Ans))
cmd.Parameters.Add(New SqlParameter("@First_Name", obj.Firstname))
cmd.Parameters.Add(New SqlParameter("@Middle_Name", obj.Middlename))
cmd.Parameters.Add(New SqlParameter("@Last_Name", obj.Lastname))
cmd.Parameters.Add(New SqlParameter("@Employee_Type", obj.EmpType))
cmd.Parameters.Add(New SqlParameter("@Department", obj.dept))
cmd.Parameters.Add(New SqlParameter("@Photo", obj.photo))
cnn.Open()
Try
If (cmd.ExecuteNonQuery() > 0) Then
cnn.Close()
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Using
End Using
End Function
I am not getting what to do..can anyone give me some suggestion or solution?
Thanks in advance.
You assign a string to a varbinary as default value. This operation don’t perform a implicit cast. To avoid error:
Change line:
by:
If you don’t have Photo value for some rows you should alter table column to allow nulls.