Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I did 2 tables in database using one button. Now why my data is not saving in database ?

What I have tried:

C#
SqlDataAdapter da2 = new SqlDataAdapter("SELECT isnull(max(cast(ID as int)),0)+1 FROM LAB_SH_LINE", con);
                DataTable dt2 = new DataTable();
                da2.Fill(dt2);
                string sid2 = Prog + DateTime.Now.ToString("yyMMdd") + '-' + ((int)dt.Rows[0][0]).ToString("000");

                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
                con.Open();
                SqlCommand cmd2 = new SqlCommand("Insert into LAB_SH_LINE values(@sid,@Value)", con);
                cmd2.Parameters.AddWithValue("sid", sid2);
                cmd2.Parameters.AddWithValue("Value", TextBox4.Text);
                cmd2.Parameters.AddWithValue("Value", TextBox5.Text);
                cmd2.Parameters.AddWithValue("Value", TextBox6.Text);
                cmd2.Parameters.AddWithValue("Value", TextBox7.Text);
                cmd2.Parameters.AddWithValue("Value", TextBox8.Text);
                cmd2.Parameters.AddWithValue("Value", TextBox9.Text);
                cmd2.Parameters.AddWithValue("Value", TextBox10.Text);
                cmd.Parameters.AddWithValue("Water_Level", DropDownList5.SelectedValue);
                cmd.ExecuteNonQuery();
Posted
Updated 23-Sep-20 20:13pm
v2

1 solution

Do yourself a favour: go back to your previous question and read what I said there yesterday: How do I save these data in db[^]
With the exception of the "trailing comma" you have ignored all of it.

So a summary of the points you have already been given:
1) Never do "anonymous inserts"
2) You almost certainly shouldn't be inserting ID values
3) You need to specify a value for each of the parameters in your INSERT statement
4) The parameter names must match up in the AddWithValue call and the INSERT statement

You need to fix these things!

And now the continuation of "how not to do INSERT". :sigh:

What on earth do you expect SQL to do with "Water_Level" as a parameter? You don't even try to refer to it in the INSERT statement so it has no idea at all that it should even look for a parameter of that name.

Where are the textbox values supposed to go? You're giving them all the same "parameter name"!
If you want to add multiple textboxes to your row, you need to list them as different parameter names, and refer to them as such in your INSERT statement.

Stop guessing. Start thinking, and stop ignoring what we say in the hope that you know better so it it'll all work out in the end if you just guess some more! It won't...
 
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