Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
An unhandled exception occurred during the execution of the current form. Please review the stack trace for more information about the error and where it originated in the code
Please suggest me.

What I have tried:

I tried with below code.

C#
private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtAdmissionNo.Text == "")
                {
                    MessageBox.Show("Please Select Student's  AdmissionNo", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtAdmissionNo.Focus();
                    return;
                }
                Calculate1();
                String ConnectionString = ConfigurationManager.ConnectionStrings["WinLogconn"].ConnectionString.ToString();
                SqlConnection con = new SqlConnection(ConnectionString);
                
                string cb = @"INSERT INTO tbl_CourseFeePayment(FeePayementID,AdmissionNo,StudentName,Class,Section,SessionYear,PreviousDue,PaymentDate,ModeOfPayment,Fees,PaymentModeDetails,Fine,TotalPaid,CurrentDue)VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,d14)";
                cmd = new SqlCommand(cb);
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@FeePayementID", FeePaymentID.Text);
                cmd.Parameters.AddWithValue("@AdmissionNo", txtAdmissionNo.Text);
                cmd.Parameters.AddWithValue("@StudentName", txtStudentName.Text);
                cmd.Parameters.AddWithValue("@Class", txtclass.Text);
                cmd.Parameters.AddWithValue("@Section", txtSection.Text);
                cmd.Parameters.AddWithValue("@SessionYear", txtSession.Text);
                cmd.Parameters.AddWithValue("@PreviousDue", txtPrevDue.Text);
                cmd.Parameters.AddWithValue("@PaymentDate", dtpPaymentDate.Text);
                cmd.Parameters.AddWithValue("@ModeOfPayment", txtModeOfPayment.Text);
                cmd.Parameters.AddWithValue("@Fees", txtFee);
                cmd.Parameters.AddWithValue("@PaymentModeDetails", txtPaymentModeDetails.Text);
                cmd.Parameters.AddWithValue("@Fees", txtFine.Text);
                cmd.Parameters.AddWithValue("@TotalPaid", txtTotalPaid.Text);
                cmd.Parameters.AddWithValue("@CurrentDue", txtCurentDueFees.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                st1 = lblUser.Text;
                st2 = " ClassFee Payment Is Done admissionNo='" + txtAdmissionNo.Text + "' having PaymentID ='" + FeePaymentID.Text + "'";
                btnSave.Enabled = false;
                btnPrint.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Posted
Updated 25-Feb-23 7:52am
v2
Comments
CHill60 24-May-16 6:27am    
What information did the stack trace give?
CHill60 24-May-16 6:40am    
Also watch out for txtFee ... if that is a TextBox then you need to use txtFee.Text

1 solution

Try:
C#
cmd.Parameters.AddWithValue("@PaymentDate", dtpPaymentDate.Value);

And tie your parameter names to the names you give in the SQL.
These don't match:
SQL
VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,d14)
They should be
C#
VALUES (@FeePayementID, @AdmissionNo, @StudentName, @Class, ...
To match the parameters:
C#
cmd.Parameters.AddWithValue("@FeePayementID", FeePaymentID.Text);
cmd.Parameters.AddWithValue("@AdmissionNo", txtAdmissionNo.Text);
cmd.Parameters.AddWithValue("@StudentName", txtStudentName.Text);
cmd.Parameters.AddWithValue("@Class", txtclass.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