I am really confused .I have table with 3 columns called id,title,images .What I want to do is to store images in the db.But I couldnt .I am getting exception at this line
int id = Convert.ToInt32(cmd.ExecuteScalar());
Connnection state seems open.What else do i need to check?
SqlConnection connection = null;
try
{
Byte[] imgByte = null;
if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
{
HttpPostedFile File = FileUpload1.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
connection = new SqlConnection("server=.\\sqlexpress;database=Northwind;UID=sa;Password=1234");
connection.Open();
string sql = "INSERT INTO imgtable (title,images) VALUES(@theTitle, @theImage) SELECT @@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@theTitle", txtTitle.Text);
cmd.Parameters.AddWithValue("@theImage", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());