Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
            }
        }


What I have tried:

I've tried submitting a simple text form and I get success message, but not when fileupload code is there.
Posted
Updated 12-May-19 20:45pm

1 solution

Well no, you won't.
You add an alert message immediately after doing a Response.Redirect - and the later sends an HTTP request to the browser which sends that request to the web server which responds with a new page - anything you add or added to the "old" page (such as your alert) is ignored because it has been "superseded" by the new page request.
 
Share this answer
 
Comments
Member 13870499 13-May-19 2:52am    
oh yes my bad. it was commented. But the alert message after ExecuteNonQuery works when there's no file upload part. Could you tell me where exactly I should put success message or fix this code to work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900