Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my code :-
my sql date formate is YYYY-mm-dd
ed = 1;
            con = new SqlConnection(conn);
            UID = Login.UID;
            DateTime dt = DateTime.Now;
            if (txtTFee.Text != "" || txtpending.Text != "" || txtinst .Text != "" || txtamt .Text != "" || txtbalance .Text != "" || txtcourse .Text != "" || txtdpay .Text != "")
            {
                cmd = new SqlCommand("insert into Fee values(@date,@Regno,@CID,@Feepaid,@tfee,@pend,1,@uid,@crdate,@upuid,@update,1)", con);
                con.Open();
                cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(txtdate.Text));
                cmd.Parameters.AddWithValue("@Regno", comboBox1.SelectedValue.ToString());
                cmd.Parameters.AddWithValue("@CID", Convert.ToInt32(lblcid.Text));
                cmd.Parameters.AddWithValue("@Feepaid", Convert.ToDouble(txtamt.Text));
                cmd.Parameters.AddWithValue("@tfee", Convert.ToDouble(txtfeepaid.Text));

                cmd.Parameters.AddWithValue("@pend", Convert.ToDouble(txtpending.Text));
                // cmd.Parameters.AddWithValue("@ps", 1);

                cmd.Parameters.AddWithValue("@uid", UID);
                cmd.Parameters.AddWithValue("@crdate", dt);
                cmd.Parameters.AddWithValue("@upuid", UID);
                cmd.Parameters.AddWithValue("@update", dt);
                cmd.ExecuteNonQuery();
                con.Close();


What I have tried:

ed = 1;
            con = new SqlConnection(conn);
            UID = Login.UID;
            DateTime dt = DateTime.Now;
            if (txtTFee.Text != "" || txtpending.Text != "" || txtinst .Text != "" || txtamt .Text != "" || txtbalance .Text != "" || txtcourse .Text != "" || txtdpay .Text != "")
            {
                cmd = new SqlCommand("insert into Fee values(@date,@Regno,@CID,@Feepaid,@tfee,@pend,1,@uid,@crdate,@upuid,@update,1)", con);
                con.Open();
                cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(txtdate.Text));
                cmd.Parameters.AddWithValue("@Regno", comboBox1.SelectedValue.ToString());
                cmd.Parameters.AddWithValue("@CID", Convert.ToInt32(lblcid.Text));
                cmd.Parameters.AddWithValue("@Feepaid", Convert.ToDouble(txtamt.Text));
                cmd.Parameters.AddWithValue("@tfee", Convert.ToDouble(txtfeepaid.Text));

                cmd.Parameters.AddWithValue("@pend", Convert.ToDouble(txtpending.Text));
                // cmd.Parameters.AddWithValue("@ps", 1);

                cmd.Parameters.AddWithValue("@uid", UID);
                cmd.Parameters.AddWithValue("@crdate", dt);
                cmd.Parameters.AddWithValue("@upuid", UID);
                cmd.Parameters.AddWithValue("@update", dt);
                cmd.ExecuteNonQuery();
                con.Close();
Posted
Updated 29-Sep-17 1:01am
Comments
Jochen Arndt 29-Sep-17 7:07am    
Check that the order of your values corresponds to the order of the table columns and that the last column is of type int (where you pass '1').

1 solution

Two problems here, one of which you have noticed.
I'll deal with the other one first.
Never use Convert.ToDateTime, or Convert.ToDouble to handle user input - it's a recipe for your app to crash if they user miskeys - and with that many inputs on one form, the chances are high. And it would annoy the heck out of me if I filled in all that and the app crashed because I mistyped the date in the first box...
Always use DateTime.TryParse etc instead, and report problems to users then return from the method to give them a chance to fix it.

Now the problem you are having.
When you don't list the column names you are inserting into, SQL inserts in a fixed order - the same as it lists them in the "Columns" view in SSMS. It doesn't attempt to do any "matching up" by type, or skip any columns such as an IDENTITY column that it handles anyway.
Probably, you do have an INT ID column, which is set as IDENTITY, and SQL is trying to insert a DateTime value to that column: hence the error. It's good practice to always list the columns you want to SELECT or affect, it also makes your code more resistant to database changes in the future, and reduces the amount of data you SELECT uneccesarily, improving application performance.
C#
cmd = new SqlCommand("INSERT INTO Fee (MyDate, RegNo, CID, FeePaid, TFee, PEND, UID, CRDate, UPUID, UPDate) VALUES (@date, @Regno, @CID, @Feepaid, @tfee, @pend, 1, @uid, @crdate, @upuid, @update, 1)", con);
 
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