protected void Button1_Click(object sender, EventArgs e)
{
if ( ! FileUpload1.HasFile)
{
Label1.Visible = true;
Label1.Text = "Please Select Image File"; //checking if file uploader has no file selected
}
else
{
int length = FileUpload1.PostedFile.ContentLength;
byte[ ] pic =new byte[length];
FileUpload1.PostedFile.InputStream.Read(pic, 0, length);
try
{
connection(); //calling connection method
//inserting uploaded image query
SqlCommand com = new SqlCommand("insert into ImageTotable "
+ "(myphoto,name) values (@photo, @name)", con);
com.Parameters.AddWithValue("@photo", pic);
com.Parameters.AddWithValue("@name", TextBox1.Text);
com.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text = "Image Uploaded Sucessfully"; //after Sucessfully uploaded image
}
finally
{
con.Close();
}
}