Hi always try to use the Try Catch Final block to execute this queries.In Catch block capture the Error and Show to the User.
Do like this
SqlConnection connection = null;
Byte[] imgByte = null;
if (imgUpload.PostedFile != null && imgUpload.PostedFile.FileName != "" && imgUpload.HasFile)
{
//To create a Posted File
HttpPostedFile file = imgUpload.PostedFile;
//Create byte Array with File Length
imgByte =
new Byte[file.ContentLength];
//Force the control to load the data in Array
file.InputStream.Read(imgByte, 0, file.ContentLength);
}
//Insert the Employee name and Image into DB
string con = ConfigurationManager.ConnectionStrings["TESTDB"].ConnectionString;
connection =
new SqlConnection(con);
connection.Open();
try
{
string Insert_Query = "insert into empdetails(empname,empimg) values(@en,@eimg) select @@identity";
SqlCommand cmd = new SqlCommand(Insert_Query, connection);
cmd.Parameters.Add(
new SqlParameter("@en", txtEName.Text));
cmd.Parameters.Add(
new SqlParameter("@eimg", imgByte));
id =
Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text =
String.Format("Employee ID is {0}", id);
}
catch (Exception exp)
{
lblResult.Text = exp.Message;
}