Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code for a messagebox to show when the user clicks on the submit button but it does not show. What did I do wrong? Is the code right?

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);

           try
           {
               cmd.ExecuteNonQuery();
               con.Close();
               Response.Redirect("Gradrate.aspx");

           }
           catch (Exception er)
           {
               ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted Your Cohort')", true);
           }
           finally
           {
           }
       }
Posted
Updated 18-Nov-13 5:00am
v2
Comments
ZurdoDev 18-Nov-13 10:04am    
This is all wrong. You have a successful message in a catch block. However, just put a breakpoint to see what is happening.
Computer Wiz99 18-Nov-13 10:06am    
Ok, I will do that.
Computer Wiz99 18-Nov-13 10:11am    
Nothing happened. I put the breakpoint on the alerMessage and it just sat there. No message displayed.
ZurdoDev 18-Nov-13 10:18am    
You need to understand how web programming works. C# code executes on the server and then when it is all done it sends html to the client and if you have called any javascript (such as in your catch block) it will then process that on the client so you won't see it happen in the catch block but after the page is rendered.
Computer Wiz99 18-Nov-13 10:20am    
Ok. I have this same code on a registration form and it works but I use a lable.

1 solution

You need to use RegisterStartupScript. You just registered the block but nothing is calling that code. If you register it as a startup that means it will run when the page loads. However, I like the idea of a label much better. I am not a fan of calling JS from C#, personally.
 
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