Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that a user can fill out. When the user clicks the submit button the form pops up a success message and redirects back to the same form but the data is not cleared out. What did I do wrong?

C#
protected void Submit_Click(object sender, EventArgs e)
       {

           SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
           con.Open();

           SqlCommand cmd = new SqlCommand("Insert into Table2013 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE)Insert into Table2014 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE)", con);

           cmd.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
           cmd.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
           cmd.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
           cmd.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
           cmd.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
           cmd.Parameters.AddWithValue("@BTRANSFERS", TextBoxTTOG.Text);
           cmd.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
           cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
           cmd.Parameters.AddWithValue("@UNITID", TextBoxUNITID.Text);
           cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);

           cmd.ExecuteNonQuery();
           con.Close();

           if (Page.IsValid)
           {
               ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
           }
           else
           {
               Response.Redirect("Gradrate.aspx");
           }


       }
Posted

You redirect only if page invalid...

Your code:
C#
if (Page.IsValid)
{
  ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
}
else
{
  Response.Redirect("Gradrate.aspx");
}


You should write:
C#
if (Page.IsValid)
{
  ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);

  Response.Redirect("Gradrate.aspx");
}

or
C#
if (Page.IsValid)
{
  ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
}

Response.Redirect("Gradrate.aspx");
 
Share this answer
 
Comments
Computer Wiz99 19-Nov-13 8:34am    
Kornfeld Eliyahu Peter, The only thing wrong with it is that the pop up message does not display.
Kornfeld Eliyahu Peter 19-Nov-13 8:55am    
Redirect cut's your page flow and goes to the new target. If you want to display first a message you have to catch the confirm of that message and then do the redirect...
Computer Wiz99 19-Nov-13 9:04am    
Yeah, I did the try, catch and finally but it still did the same thing. No message display. But i have it now. Thanks.
I have added this to the code.

C#
if (Page.IsValid)
           {
               ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
           }
           else
           {
               Response.Redirect("Gradrate.aspx");
           }

           TextBoxUNITID.Text = string.Empty;
           TextBoxTNUGSC.Text = string.Empty;
           TextBoxTNUGSCD.Text = string.Empty;
           TextBoxTTOUG.Text = string.Empty;
           TextBoxTNGSC.Text = string.Empty;
           TextBoxTNGSCD.Text = string.Empty;
           TextBoxTTOG.Text = string.Empty;

       }
 
Share this answer
 

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