I want to execute a simple UPDATE command!

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

I am pretty new to .NET, so can someone tell me why this doesn't work?
It does not throw any exception but it doesn't update the record in the
table either. All variables are populated correctly and the record
DOES exist!

Thanks in advance!

SqlConnection loConnection = new SqlConnection(<connection string
here>);
SqlCommand loCommand = loConnection.CreateCommand();

loCommand.CommandText = "UPDATE NewUser SET Name = '" + lsName + "'
WHERE NewUserID = " + liNewUserID;
loConnection.Open();

SqlDataReader loDataReader = loCommand.ExecuteReader();
loDataReader.Read();
loDataReader.Close();
loConnection.Close();
 
Hi,

If you execute Update (also Insert and Delete), use
command.ExecuteNonQuery() rather than ExecuteReader().

HTH,

Elton Wang
 
Neither ExecuteNonQuery() nor ExecuteScalar() worked. Still doesn't
throw an error, but doesn't update the database either.
 
Let's see the code.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
SqlConnection loConnection = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Data_Connection_String"]);
SqlCommand loCommand = loConnection.CreateCommand();
loCommand.CommandText = "UPDATE tbad_newuser SET ad_newuser_expiredate
= '" + ldExpirationDate + "' WHERE ad_newuser_id = " + liNewUserID;
loConnection.Open();
loCommand.ExecuteScalar();
loConnection.Close();
 
Back
Top