I'm a beginner in C# and I have a problem with image uploading.
Everything in my code inserting in my database except for the files that I uploaded, I didn't know why it doesn't save it in the database, it only saved when I remove the Response.Redirect("Personalinformation.aspx")
but I can't remove this response redirect because I need it.
protected void Button8_Click(object sender, EventArgs e) {
SqlConnection con = new SqlConnection(conString);
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
if (TextBox11.Text != "" && TextBox8.Text != "" && TextBox9.Text != "" )
{
if (FileUpload1.PostedFile != null)
{
if (FileUpload2.PostedFile != null)
{
string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string FileName1 = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
//Save files to disk
FileUpload1.SaveAs(Server.MapPath("/upload/" + FileName));
FileUpload2.SaveAs(Server.MapPath("/identification/" + FileName1));
String q = "Insert into dbo.requests(request_type,visit_date,reason,request_date,status_id,user_id,user_name,FileName,FilePath,End_vdate,FileName1,FilePath1,RequestNotes,VisitorType)values(N'" + DropDownList1.SelectedValue + "',N'" + TextBox8.Text + "',N'" + TextBox9.Text + "','" + DateTime.Now + "','4','" + Session["empNo"] + "',N'" + Session["ArbnName"] + "' , @FileName , @FilePath ,N'" + TextBox10.Text + "',@FileName1 , @FilePath1)";
SqlCommand cmd = new SqlCommand(q, con);
cmd.Parameters.AddWithValue("@FileName", FileName);
cmd.Parameters.AddWithValue("@FilePath", "/upload/" + FileName);
cmd.Parameters.AddWithValue("@FileName1", FileName1);
cmd.Parameters.AddWithValue("@FilePath1", "/identification/" + FileName1);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
else
{
String q = "Insert into dbo.requests(request_type,visit_date,reason,request_date,status_id,user_id,user_name,End_vdate,phone_ext)values(N'" + DropDownList1.SelectedValue + "',N'" + TextBox8.Text + "',N'" + TextBox9.Text + "','" + DateTime.Now + "','4','" + Session["empNo"] + "' ,N'" + Session["ArbnName"] + "',N'" + TextBox10.Text + "','" + TextBox11.Text.ToString() + "')";
SqlCommand cmd = new SqlCommand(q, con);
cmd.ExecuteNonQuery();
}
response.redirect("Personalinformation.aspx");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert(' هناك حقول مطلوبة فارغة ');</script>");
}
String c = "Insert into dbo.logFile(user_id,transaction_date,action_type) values ('" + Session["empNo"] + "', '" + DateTime.Now + "' ,N' طلب قبول زائر') ";
SqlCommand cmdc = new SqlCommand(c, con);
cmdc.ExecuteNonQuery();
con.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0) //index of long visit
{
FileUpload1.Visible = false;
FileUpload2.Visible = false;
Button6.Visible = false;
TextBox10.Visible = false;
Label2.Visible = false;
Label3.Visible = false;
}
else
{
FileUpload1.Visible = true;
FileUpload2.Visible = true;
Button6.Visible = true;
TextBox10.Visible = true;
Label2.Visible = true;
Label3.Visible = true;
}
}