Forums - C# Corner

Forum guidelines
Brian

Brian

  • 1.8k
  • 2
  • 36

Success message for form submit with fileupload ASP.NET SQL?

May 13 2019 1:40 AM

This code works very well. But I don't get Saved message when the file upload code is there. I've tried submitting a simple text form and I get success message, but not when fileupload code is there.

 

protected void Button1_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contenttype = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string query = "INSERT INTO LeaveReqTable (Name, EPFNo, FileName, ContentType, Data) VALUES (@Name, @EPFNo, @FileName, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
cmd.Parameters.AddWithValue("@EPFNo", TextBox2.Text);
cmd.Parameters.AddWithValue("@FileName", filename);
cmd.Parameters.AddWithValue("@ContentType", contenttype);
cmd.Parameters.AddWithValue("@Data", bytes);
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
Label1.Text = "Record Inserted Succesfully into the Database";
Label1.ForeColor = System.Drawing.Color.CornflowerBlue;
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
}
con.Close();
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
}
}
 

Answers (1)