Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I have a form I made and I have the code I wrote. When I fill in the data and click submit the it says that You Have Successfully Submitted the Information!!! but when I go into the database and select the table it went to there is nothing there. What did I do wrong? Here is my code:

C#
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Printing;

public partial class FTEEnrollmentInformation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

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

         
        
        string insCmd1 = "Insert into TableFTE (FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTBHC, NCHC) values (@FT_UNDERGR, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTBHC, @NCHC)";


        string insCmd2 = "Insert into TableGRADRATE (ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSDERS, YEAR, DATE) values (@ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSDERS, @YEAR, @DATE)";

        SqlCommand insertUser1 = new SqlCommand(insCmd1, con);
        insertUser1.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text);
        insertUser1.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text);
        insertUser1.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text);
        insertUser1.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text);
        insertUser1.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text);
        insertUser1.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text);
        insertUser1.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text);
        insertUser1.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text);
        insertUser1.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text);
        insertUser1.Parameters.AddWithValue("@FCPTBHC", TextBoxTNCPG.Text);
        insertUser1.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text);

        SqlCommand insertUser2 = new SqlCommand(insCmd2, con);
        insertUser2.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
        insertUser2.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
        insertUser2.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
        insertUser2.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
        insertUser2.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
        insertUser2.Parameters.AddWithValue("@BTRANSDERS", TextBoxTTOG.Text);
        insertUser2.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
        insertUser2.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        try
        {
            insertUser1.ExecuteNonQuery();
            insertUser2.ExecuteNonQuery();
            con.Close();
            Response.Redirect("FinancialProfileFormA.aspx");
        }
        catch (Exception er)
        {
            Response.Write("You Have Successfully Submitted the Information!!!");
        }
        finally
        {

        }
    }
   public class PCPrint : System.Drawing.Printing.PrintDocument
    {
        
    }
   protected void ButtonPrint_Click(object sender, EventArgs e)
   {
       
   }
}
Posted
Comments
[no name] 8-Jul-13 16:27pm    
You are getting that message not because you were successful, but because you are throwing an exception. The data is not in the database for the same reason.
joshrduncan2012 8-Jul-13 16:59pm    
ThePhantom is correct. Your code looks good. You need to put your success message at the end of the try block and the exception that something failed goes in the catch block.
Asp_Learner 8-Jul-13 19:16pm    
catch (Exception er)
{
Response.Write(er.ToString());
}

try this in your catch block ,then improve your question

1 solution

Hey, Add the entire codeblock in a try catch statement. You are executing the NonQuery outside your insert code.
VB
try
        {
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
        con.Open();
 
         
        
        string insCmd1 = "Insert into TableFTE (FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTBHC, NCHC) values (@FT_UNDERGR, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTBHC, @NCHC)";
 

        string insCmd2 = "Insert into TableGRADRATE (ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSDERS, YEAR, DATE) values (@ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSDERS, @YEAR, @DATE)";
 
        SqlCommand insertUser1 = new SqlCommand(insCmd1, con);
        insertUser1.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text);
        insertUser1.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text);
        insertUser1.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text);
        insertUser1.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text);
        insertUser1.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text);
        insertUser1.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text);
        insertUser1.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text);
        insertUser1.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text);
        insertUser1.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text);
        insertUser1.Parameters.AddWithValue("@FCPTBHC", TextBoxTNCPG.Text);
        insertUser1.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text);
 
        SqlCommand insertUser2 = new SqlCommand(insCmd2, con);
        insertUser2.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
        insertUser2.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
        insertUser2.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
        insertUser2.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
        insertUser2.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
        insertUser2.Parameters.AddWithValue("@BTRANSDERS", TextBoxTTOG.Text);
        insertUser2.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
        insertUser2.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);

            insertUser1.ExecuteNonQuery();
            insertUser2.ExecuteNonQuery();
            con.Close();
            Response.Redirect("FinancialProfileFormA.aspx");
        }
        catch (Exception er)
        {
            Response.Write("You Have Successfully Submitted the Information!!!");
        }
        finally
        {
 
        }
 
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