I am getting error
String or binary data would be truncated when trying to insert data in SQL Server Database. What I think is, it might be because of the image length.
Database
CREATE TABLE [dbo].[tblImg](
[ImgID] [nvarchar](50) NULL,
[Img_UserID] [int] NOT NULL,
[ImgName] [nvarchar](50) NULL,
[img] [varbinary](max) NULL,
[img_desc] [nvarchar](50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
What I have tried:
Here is the coding that I currently use to insert data and image into database :
Private Sub getImg()
Dim len As Integer = selectedFile.PostedFile.ContentLength
Dim pic As Byte() = New Byte(len - 1) {}
selectedFile.PostedFile.InputStream.Read(pic, 0, len)
Dim fileName As String = selectedFile.PostedFile.FileName
Using lCnn As New SqlConnection(gsConnString)
lCnn.Open()
Using lCmd As New SqlCommand
Dim lsCmd As String
lCmd.Connection = lCnn
lsCmd = "INSERT INTO tblImg"
lsCmd &= " (ImgID, Img_UserID, ImgName, img, img_desc)"
lsCmd &= " VALUES(" & SQLQUOTE(gsImgID)
lsCmd &= ", " & SQLQUOTE(gsUserID)
lsCmd &= ", @imgName, @img, " & SQLQUOTE(Trim(imgDescTxt.Text)) & ")"
lCmd.Parameters.AddWithValue("@imgName", fileName)
lCmd.Parameters.AddWithValue("@img", pic)
lCmd.CommandText = lsCmd
lCmd.ExecuteNonQuery()
End Using
lCnn.Close()
End Using
End Sub
Somebody please help me with this error. Thank you.