Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Can this command update the database and work?
SQL
SqlCommand cmdupdate = new SqlCommand();
            cmdupdate.Connection = cn;
            cmdupdate.CommandType = CommandType.Text;
            cmdupdate.CommandText = "UPDATE Orders2 " +
                " SET Monthname= DATENAME (MONTH, OrderDate)," +
                " Weekname = DATENAME (WEEKDAY, OrderDate) +' '+ 'of' +' '+ DATENAME (MONTH, OrderDate)," +
                " Yearname = DATENAME (YEAR, OrderDate)," +
                " MonthYear = DATENAME (MONTH, OrderDate) + ' '+ DATENAME(YEAR, OrderDate)," +
                " QuarterNum = 'Quarter' + ' ' + DATENAME (QUARTER, OrderDate) +' ' + 'of' + ' ' + DATENAME (YEAR, OrderDate)," +
                " Weeknum = 'Week' + ' ' + DATENAME (WK, OrderDate) +' '+ 'of' +' '+ DATENAME (YEAR, OrderDate)," + ";";
Posted
Updated 12-Dec-10 23:39pm
v5
Comments
Henry Minute 12-Dec-10 8:29am    
Correct it so that it does what? Do you want it corrected so that it will launch a nuclear missile, or what? You haven't told us how it is currently incorrect.
Dave Kreskowiak 12-Dec-10 13:48pm    
Correct what? How do you expect anyone to be able to fix this to expectations that you haven't mentioned anything about??
Toli Cuturicu 12-Dec-10 14:34pm    
"How do you correct this?" - I don't.
Sandeep Mewara 13-Dec-10 4:03am    
What's wrong?
wolfsor 13-Dec-10 5:36am    
I have just updated the question.

1 solution

wolfsor wrote:
Can this command update the database and work?

Nope, it'll fail. You're concatenating a comma just before terminating the Sql statement. Take a look at the last piece of the code;
SQL
+ DATENAME (YEAR, OrderDate)," + ";";

That would result in this;
SQL
+ DATENAME (YEAR, OrderDate),;

Remove that last comma, and it would work.

Some additional remarks, as you asked how to 'correct' the statement;

  • A SqlCommand implements the IDisposable interface. You'd best encapsulate it in a using directive.
  • The SqlConnection class has a factory-method called "CreateCommand" that you can use to create a new command for that particular connection
  • You're concatenating too much, at the expense of readability. Example;
    +' ' + 'of' + ' ' 

    would be more readable in this form;
    +' of '
 
Share this answer
 
v2
Comments
saeedazam786 7-Jan-13 4:05am    
i want to ask a question that in update your are changing the values but how can we Subtract the values in update.for example a stock column contain 20,we give input as 5,then how can we subtract 5 from stock column and then stock column updated to 15.
Thanks in advance
Eddy Vluggen 7-Jan-13 5:30am    
UPDATE [MyTableName]
SET CurrentStock = CurrentStock - 5
WHERE Id = @yourKeyValue
saeedazam786 7-Jan-13 23:34pm    
Thanks for your answer.Here is my code


DataTable dt = new DataTable();
con = new SqlConnection();
ds1 = new DataSet();
cb = new SqlCommandBuilder(da);

con.ConnectionString = "Data Source=SAEED\\SQLEXPRESS;Initial Catalog=FYPDB;Integrated Security=True";
con.Open();
dt = ds1.Tables["Bus"];
string sql = "Update Bus SET MaleSeat=MaleSeat-"+textBox2.Text + ", FemaleSeat=FemaleSeat-"+textBox3.Text+"Where BusNo='"+textBox1.Text+"'";
da = new SqlDataAdapter(sql, con);
da.Fill(ds1, "Bus");
da.Update(ds1,"Bus");
con.Close();
MessageBox.Show("Data updated");

i am getting this error:

Update unable to find TableMapping['Bus'] or DataTable 'Bus

Plz tell me where i am going wrong.
Eddy Vluggen 8-Jan-13 9:14am    
I'd use an IDbCommand, not an adapter.

Google for SqlCommand.ExecuteNonQuery :)

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