I am new to this have been thrown into the Lions den with only a class and a book. So I need a little help with how to update the DB from the fields that are entered on the screen. I actually provide the value with the code nad it works :
cmd.CommandText =
"UPDATE BM_DISPUTES SET DIS_LASTNAME = 'DOE', DIS_FIRSTNAME = 'JOHN'" +
"WHERE DIS_NO = 17";
So if someone could help me out with a simple example so I can get started I certainly would appreciate it. I will need for Update, Add and Delete...
Thanks,
Ben
PS; I use VS2010 C# and this is a Windows application...
Loading
theLizardPosted Feb 22, 2011, 5:43 PM
"UPDATE BM_DISPUTES SET " + "DIS_LASTNAME = '" + textBox2.Text + "', " +"DIS_FIRSTNAME = '" + textBox3.Text + "', " +"WHERE " + "DIS_NO =" + textBox1.Text;
In this case you are missing the single quote around "DIS_NO =" + textBox1.Text it should be "DIS_NO = ' " + textBox1.Text + " ' " as Sam said..
int numberOfAffectedRows = cmd.ExecuteNonQuery(); --> What will you do with the result?
Sam HobbsPosted Feb 22, 2011, 3:24 PM
The following is the result of that:
See the problem? There are no delimters around the text at the end. That should be very easy to figure out. Now the next thing to do is to look at the code and simplify it; you are putting together text literals using "+" when you can put the text together in one literal. This makes the code easier for you and us to read and it is easier for the computer. So the following will work better; just replace t1, t2 and t3 with the corresponding variables that you need.
One more thing. You really should use a parameterized SQL statement. For one reason, your update I think will fail if you have a last name with an apostrophe, such as "O'Heaney". If you were to use a TableAdapter, then the SQL will be created for you.
Ben meadePosted Feb 22, 2011, 2:55 PM
sqlConnection1.Close();
sqlConnection1.Open();
int COUNT = (int)cmd.ExecuteNonQuery();//UPDATE A SPECIFIC RECORD...cmd.CommandText =
cmd.ExecuteNonQuery();
sqlConnection1.Close();
"UPDATE BM_DISPUTES SET " + "DIS_LASTNAME = '" + textBox2.Text + "', " +"DIS_FIRSTNAME = '" + textBox3.Text + "', " +"WHERE " + "DIS_NO =" + textBox1.Text;int numberOfAffectedRows = cmd.ExecuteNonQuery();
ERROR: System.Data.SqlClient.SqlException was unhandled
Message=Incorrect syntax near the keyword 'WHERE'.
Textbox1 = the record I want to update. When I took the Where clause out it update every record with the same last and first name.
Thnaks Again,
Ben
Ben meadePosted Feb 22, 2011, 7:15 AM
Ben
theLizardPosted Feb 21, 2011, 3:47 PM
If we take your example
"UPDATE BM_DISPUTES SET DIS_LASTNAME = 'DOE', DIS_FIRSTNAME = 'JOHN'" +
"WHERE DIS_NO = 17";
then if DIS_LASTNAME is a text box on your screen you would do something like "UPDATE BM_DISPUTES SET DIS_LASTNAME = ' + TextBox1.Text + "', DIS_FIRSTNAME = '" + TextBox2.Text + "'"
Is this what you want to know?
Sam HobbsPosted Feb 21, 2011, 2:50 PM
Mike GoldPosted Feb 21, 2011, 11:47 AM
If you need to go back and get the values, you can query the table with a dataadapter which will put the results in a dataset. The other option is to use a data reader.
http://msdn.microsoft.com/en-us/library/haa3afyz.aspx
In your case:
SqlCommand command = new SqlCommand(
"SELECT DIS_LASTNAME, DIS_FIRSTNAME FROM BM_DISPUTES WHERE DIS_NO=17;",
connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
Ben meadePosted Feb 21, 2011, 9:27 AM
Thanks for the help and this was a good example. But I noticed on the example that the program is actually providing values to be updated, inserted or deleted. Am I thinking wrong? What I need to do is actually pull values that I enter and take those values and store then to the DB. I am using VS2010 C# and using a Windows application. Below is the code that I am using and it updates the DB, but with values I give. I am not certain of how to get the values from the windows screen that I entered to be used. My code is below:
cmd.CommandText =
cmd.ExecuteNonQuery();
"UPDATE BM_DISPUTES SET DIS_LASTNAME = 'DOE', DIS_FIRSTNAME = 'JOHN'" + "WHERE DIS_NO = 17";cmd.ExecuteNonQuery();
I would like to know how to update DIS_LASTNAME, which is the DB name with the value I enter on the screen. I am really new to this C# but not to programming. I understand logic, it's just what syntax to use to get what I want..
Thanks for you help.!!
Ben
Sam HobbsPosted Feb 17, 2011, 9:15 PM
Suthish NairPosted Feb 17, 2011, 2:57 PM
DataGridView - Insert, Update, Delete and Retrieve records using stored procedure
Mike GoldPosted Feb 16, 2011, 11:51 PM
1) Create a SqlConnection using the connection string (You can figure out the connection string by connecting through Server Explorer)
2) Create a SqlCommand passing it the SqlConnection and set the command text to your update as you did above.
3) Open the connection.
2) use the command method ExecuteNonQuery
Below is a good example of this:
http://www.java2s.com/Code/CSharp/Database-ADO.net/UsetheExecuteNonQuerymethodtorunINSERTUPDATEandDELETEstatements.htm