I want to store the image(any type of image) into the database by using varbinary(MAX).
My Database:
CREATE TABLE [dbo].[Pic]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[picture] VARBINARY(MAX) NULL,
[origin] NVARCHAR(100) NULL
)
My code:
protected void Button1_Click(object sender, EventArgs e)
{
byte[] imagebyte = File.ReadAllBytes(Server.MapPath("~/") + imageUpload1);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Pic values('"+ imagebyte +"','"+ lblOrigin.Text +"')";
cmd.ExecuteNonQuery();
}
when I run my code, I get this error:
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query. at cmd.ExecuteNonQuery();
How can I solve this problem?