I am trying to insert image to the table [student] I have created in my Microsoft SQL Server.
I am using the FileUpload to upload the image, but I have problem with the code because my code always executes the catch block. Please help
try
{
if (FileUpload1.HasFile)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=DESKTOP-H7KQUT1;Initial Catalog=SAOS;Integrated Security=True";
string strname = FileUpload1.FileName.ToString();
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/upload/") + strname);
con.Open();
string insertQuery = "insert into student (SID, Email, Contact, FName, LName, HomeAddress, Gender, DOB, Image) values (@SID, @Email, @Contact, @FName, @LName, @HomeAddress, @Gender, @DOB, @Image)";
SqlCommand cmd = new SqlCommand(insertQuery, con);
cmd.Parameters.AddWithValue("@SID", TextBoxID.Text);
cmd.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
cmd.Parameters.AddWithValue("@Contact", TextBoxContact.Text);
cmd.Parameters.AddWithValue("@FName", TextBoxFName.Text);
cmd.Parameters.AddWithValue("@LName", TextBoxLName.Text);
cmd.Parameters.AddWithValue("@HomeAddress", TextBoxHome.Text);
cmd.Parameters.AddWithValue("@Gender", DropDownListGender.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@DOB", TextBoxDOB.Text);
cmd.Parameters.AddWithValue("@Image", FileUpload1.ToString());
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Image Uploaded successfully";
}
else
{
Label1.Visible = true;
Label1.Text = "Plz upload the image!!!!";
}
}
catch (Exception ex)
{
MessageBox.Show("ex.Message");
}
catchblock to beMessageBox.Show(ex.Message);(no quotes) and include the full exception details in your question.