Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a code that when I debug it I can login as a user and fill out the form. When I click submit the form, as I have it setup, will say "You Have Successfully Submitted the Information!!!"
When I check the database table there is no data in there. What did I do wrong? Can someone please look over my code and let me know what went wrong? Thanks.

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.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 insCmd = "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); Insert into TableGRADRATE (ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSDERS, YEAR, DATE) values (@ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSDERS, @YEAR, @DATE);";

        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text);
        insertUser.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text);
        insertUser.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text);
        insertUser.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text);
        insertUser.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text);
        insertUser.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text);
        insertUser.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text);
        insertUser.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text);
        insertUser.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text);
        insertUser.Parameters.AddWithValue("@FCPTBHC", TextBoxTNCPG.Text);
        insertUser.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text);
        insertUser.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
        insertUser.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
        insertUser.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
        insertUser.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
        insertUser.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
        insertUser.Parameters.AddWithValue("@BTRANSDERS", TextBoxTTOG.Text);
        insertUser.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
        insertUser.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("FormAPublic.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)
   {
       
   }
}


Ok. I had using System.Data; twice. I looked over and over I still do not see where my catch is the problem. I have almost the same code in another program on another computer and it works just fine. Catch is in the same place and everything. Any body else getting something different?
Posted
Updated 16-Jul-13 9:27am
v2
Comments
[no name] 16-Jul-13 12:17pm    
You have to debug it , on a side note, Why are you showing a successful message in catch statement ?
Computer Wiz99 16-Jul-13 15:37pm    
I just put it there for the user. Also when the user click submit, the page just redirects to the same form instead of the page I put into the redirect. Why does it do that?
[no name] 16-Jul-13 15:45pm    
because your code is not reaching that point..
Computer Wiz99 16-Jul-13 16:16pm    
At what point are you talking about? If my code is not reaching that point then why does it work on another computer in a different program?

Code is throwing exception before data get inserted into database and due to which catch block is getting executed and your getting "You Have Successfully Submitted the Information!!!".

I believe first you need to learn who to use try catch and finally. Please go through below links or you can search yourself in Google.

try-catch-finally (C# Reference)[^]
Try ... Catch in C# .NET[^]
 
Share this answer
 
After hours of testing and testing and rewriting my code I got it to work. Here is what I did.

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.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Printing;

public partial class FinancialProfileFormA : 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);
        


        SqlCommand cmd = new SqlCommand ("Insert into TableFIN (TOTAL_REVE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT) values (@TOTAL_REVE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT)",con);
        cmd.CommandType = CommandType.Text;
        
        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTROA.Text);
        cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
        cmd.Parameters.AddWithValue("@RESEARCH", TextBoxRes.Text);
        cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxPubS.Text);
        cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcad.Text);
        cmd.Parameters.AddWithValue("@STUDENT_SE", TextBoxStudS.Text);
        cmd.Parameters.AddWithValue("@INSTITUTIO", TextBoxInstiS.Text);
        cmd.Parameters.AddWithValue("@PHYSICAL_P", TextBoxOperM.Text);
        cmd.Parameters.AddWithValue("@SCHOLARSHI", TextBoxSFEDA.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
        cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOED.Text);
        cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
        cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
        cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.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);
        con.Open();
        cmd.ExecuteNonQuery();
        }

   public class PCPrint : System.Drawing.Printing.PrintDocument
    {
        
    }
   protected void ButtonPrint_Click(object sender, EventArgs e)
   {
       
   }
}


I changed the insCmd to just cmd and changed insertUser to cmd and I add CommandType.text
 
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