Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.47/5 (3 votes)
See more:
I have a web form that has textboxes and validators on it. When the user clicks on the Save button there should be a message to display and then redirects user back to their home page. The message and the redirect doesn't work. What did I do wrong?

Save Button Properties:
ASP.NET
<asp:Button ID="ButtonSave" runat="server" onclick="ButtonSave_Click" 
                    style="font-weight: 700" Text="Save" Width="100px" 
                    CausesValidation="False" />


Code Behind:
C#
protected void ButtonSave_Click(object sender, EventArgs e)
    {
        SqlConnection con7 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con7.Open();

        SqlCommand cmd = new SqlCommand("Insert into Table345 (INST_ID, FT_UNDERGR, DATE, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, UnderG12, Postb9, Total123b4b, FTEYR, THCAS) values (@INST_ID, @FT_UNDERGR, @DATE, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTPBHC, @NCHC, @UnderG12, @Postb9, @Total123b4b, @FTEYR, @THCAS);", con7);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCPTPBHC", TextBoxTNCPG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FTEYR", lblYEAR1.Text);
        cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        cmd.Parameters.AddWithValue("@UnderG12", TextBoxTHUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@Postb9", TextBoxTHG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@Total123b4b", TextBoxT1234.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@THCAS", TextBoxTHCAS.Text.Replace(",", ""));

        cmd.ExecuteNonQuery();

        if (true)
        {
            SqlConnection con88 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con88.Open();
            SqlCommand level = new SqlCommand("select accessLevel, INST_ID from Tablepass where INST_ID = @INST_ID AND accessLevel = @accessLevel", con88);
            level.Parameters.Add(new SqlParameter("INST_ID", TextBoxINST_ID.Text));
            level.Parameters.Add(new SqlParameter("accessLevel", TextBoxaccessLevel.Text));

            SqlDataReader reader = level.ExecuteReader();
            DataTable dt1 = new DataTable();
            dt1.Load(reader);

            foreach (DataRow dr1 in dt1.Rows)
            {
                int returnedLevel = Convert.ToInt32(dr1[0].ToString());
                int accessLevel = Convert.ToInt32(dr1[1].ToString());
                Session["accessLevel"] = accessLevel;

                if (returnedLevel == 1)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You Have Successfully Saved the Enrollment Profile. You may come back to finish entering your data later. You will now be redirect to the Menu Page');location.href='FormAPublic.aspx';", true);
                }
                else if (returnedLevel == 11)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You Have Successfully Saved the Enrollment Profile. You may come back to finish entering your data later. You will now be redirect to the Menu Page');location.href='FormAPublicL.aspx';", true);
                }
                else if (returnedLevel == 7)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You Have Successfully Saved the Enrollment Profile. You may come back to finish entering your data later. You will now be redirect to the Menu Page');location.href='CEOPage.aspx';", true);
                }
                else if (returnedLevel == 3)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You Have Successfully Saved the Enrollment Profile. You may come back to finish entering your data later. You will now be redirect to the Menu Page');location.href='FormDPrivateFor.aspx';", true);
                }
                else if (returnedLevel == 31)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You Have Successfully Saved the Enrollment Profile. You may come back to finish entering your data later. You will now be redirect to the Menu Page');location.href='FormDPrivateForL.aspx';", true);
                }
                else if (returnedLevel == 2)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You Have Successfully Saved the Enrollment Profile. You may come back to finish entering your data later. You will now be redirect to the Menu Page');location.href='FormCPrivateNon.aspx';", true);
                }
                else if (returnedLevel == 21)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You Have Successfully Saved the Enrollment Profile. You may come back to finish entering your data later. You will now be redirect to the Menu Page');location.href='FormCPrivateNonL.aspx';", true);
                }
                con88.Close();
            }
        }
    }
}
Posted
Comments
Kornfeld Eliyahu Peter 23-Oct-14 16:15pm    
The validation part seems to be easy - you set CausesValidation to false for that button...
About the message - did you debugged your code to see if any of those RegisterStartupScript calls are actually executes?
(Note: You run RegisterStartupScript in a loop and that is a bit strange as you always pass in the same type and key and that means that only the last JavaScript code will be registered anyway...)
Computer Wiz99 24-Oct-14 4:42am    
Yes. I use the same code on other projects. Why has it stopped now.
Badger Roj 24-Oct-14 5:13am    
Have you run the code through a debugger (e.g. visual studio)?

My first guess is that returnedlevel is not acquiring a value for which you are testing (i.e. the value of dr1[0]). Try adding an 'else' statement to catch anything else. Even better use a 'switch->case' statement with a 'default' to catch anything else.
Computer Wiz99 24-Oct-14 5:22am    
I am using an if else statement in the code. It works on other projects I have done.

1 solution

I have it working now. One little small thing in my code. I had this:

C#
if (true)
        {
            SqlConnection con88 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con88.Open();


I changed it to this:
C#
if (Page.IsValid)
        {
            SqlConnection con88 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con88.Open();
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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