Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that a user will fill out and submit into the database. I have a message that will tell the user that the data was saved into the database then it redirects to another page. I had to fix my redirect code and now that works but the message is not displaying before the redirect. What did I do wrong?

C#
protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();

        SqlCommand cmd = new SqlCommand("Insert into Table50 (INST_ID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, ACADEMIC_S, NET_AID, AUXILIARY_, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT) values (@INST_ID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @ACADEMIC_S, @NET_AID, @AUXILIARY_, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT)Insert into Table55 (INST_ID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, ACADEMIC_S, NET_AID, AUXILIARY_, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT) values (@INST_ID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @ACADEMIC_S, @NET_AID, @AUXILIARY_, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT)", con);

        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTRR.Text);
        cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
        cmd.Parameters.AddWithValue("@RESEARCH", TextBoxResPs.Text);
        cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcadSSSIS.Text);
        cmd.Parameters.AddWithValue("@NET_AID", TextBoxNGAS.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxAOE.Text);
        cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
        cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
        cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
        cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text);
        cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
        cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
        cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
        cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
        cmd.Parameters.AddWithValue("@LONGTERMDEBT", TextBoxLTD.Text);
        cmd.Parameters.AddWithValue("@FINYR", lblYEAR.Text);
        cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);

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

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

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

        if (true)
        {
            SqlCommand level = new SqlCommand("select accessLevel, INST_ID from Table22 where INST_ID = @INST_ID AND accessLevel = @accessLevel", con4);
            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)
                {
                    Response.Redirect("FormAPublic.aspx");
                }
                else if (returnedLevel == 2)
                {
                    Response.Redirect("FormCPrivateNon.aspx");
                }
                else if (returnedLevel == 3)
                {
                    Response.Redirect("FormDPrivateFor.aspx");
                }
                else if (returnedLevel == 11)
                {
                    Response.Redirect("FormAPublicL.aspx");
                }
                else if (returnedLevel == 21)
                {
                    Response.Redirect("FormCPrivateNonL.aspx");
                }
                else if (returnedLevel == 31)
                {
                    Response.Redirect("FormDPrivateForL.aspx");
                }
                else if (returnedLevel == 7)
                {
                    Response.Redirect("CEOPage.aspx");
                }

            }

            TextBoxTRR.Text = "0";
            TextBoxInstr.Text = "0";
            TextBoxResPs.Text = "0";
            TextBoxAcadSSSIS.Text = "0";
            TextBoxNGAS.Text = "0";
            TextBoxAuxE.Text = "0";
            TextBoxAOE.Text = "0";
            TextBoxTA.Text = "0";
            TextBoxTL.Text = "0";
            TextBoxNPRNA.Text = "0";
            TextBoxETRNA.Text = "0";
            TextBoxTUNA.Text = "0";
            TextBoxTR.Text = "0";
            TextBoxTFN.Text = "0";
            TextBoxCD.Text = "0";
            TextBoxLTD.Text = "0";
            TextBoxFullN.Text = "John Doe";
            TextBoxTitle.Text = "Dr.,Mr., Mrs., Ms., Miss";
            TextBoxMA.Text = "123 Address Road";
            TextBoxTN.Text = "000-000-0000";
            TextBoxFN.Text = "000-000-0000";
            TextBoxEA.Text = "@email.com";
            TextBoxTNA.Text = "0";
            TextBoxTNA2.Text = "0";

        {
Posted

You cant show an alert just before a Page redirect. Need to handle the redirection also in Javascript.
 
Share this answer
 
Comments
Computer Wiz99 8-Jan-14 23:10pm    
Ok. I don't know Javascript. Can you give me an example please?
JoCodes 8-Jan-14 23:12pm    
http://stackoverflow.com/questions/12209822/javascript-alert-before-redirecting-in-asp-net
http://stackoverflow.com/questions/11993048/showing-clientscript-alerts-before-redirecting-to-another-page-in-asp-net-c
Computer Wiz99 8-Jan-14 23:14pm    
Ok, thanks. I will take a look at this.
JoCodes 8-Jan-14 23:17pm    
Welcome. try and if facing any issues let me know
Showing Alert before Response.Redirect is not possible.
Instead you have to redirect using JavaScript.

Something like below should work...
C#
ScriptManager.RegisterStartupScript(this,this.GetType(),"Redit","window.location='" + Request.ApplicationPath + "Default.aspx';",true);
 
Share this answer
 
Comments
Computer Wiz99 9-Jan-14 9:16am    
Tadit Dash, your answer, "Showing Alert before Response.Redirect is not possible." is wrong. I have coded to where it does. Let me show you. Look at the solution that says old message.
Are you sure? According to your code, the alert is showing inside if clause and redirection is done inside else.
How both will be executed at the same time?
Computer Wiz99 9-Jan-14 11:21am    
Try it out. See if it works for you. :)
I am out of office and not having Lappy right now.

And no need to try because think logically and try to debug the code. Either it will go inside if clause or else. Do you know how if else clause works? They can't be executed at the same time.

I guess you trying something else. Please explain me the exact thing.
I had a project in which i had same situation.Try using this

C#
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Response Added Successfully');", true);
           string ToRedirectURL = "homepage.aspx";
           Response.AppendHeader("REFRESH", "2;URL=" + ToRedirectURL);


You can change homepage.aspx to whereevr you want to redirect.And the digit 2 after refresh in the last line tells after how many seconds the page will change.You can set it accordingly
 
Share this answer
 
Comments
Computer Wiz99 9-Jan-14 12:19pm    
Karn Singh, I see what you code is showing but as you can see my code goes through a little more steps. Is there a way to do my code and show the message?
Old Message for "Showing Alert before Response.Redirect is not possible." but it is.

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 Table88 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE, TIME) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE, @TIME)Insert into Table89 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE, TIME) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE, @TIME)", con);

            cmd.CommandType = CommandType.Text;
            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.Parameters.AddWithValue("@TIME", lblTime.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"); 
            }

            TextBoxUNITID.Text = "0";
            TextBoxTNUGSC.Text = "0";
            TextBoxTNUGSCD.Text = "0";
            TextBoxTTOUG.Text = "0";
            TextBoxTNGSC.Text = "0";
            TextBoxTNGSCD.Text = "0";
            TextBoxTTOG.Text = "0";
        }
 
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