1
OleDbCommand computerStatus = new OleDbCommand("update Computer SET Status= 'Occupied' where PcNumber='" + cboComputerNo.Text + "'", con);
computerStatus.ExecuteNonQuery();

this is my code. pcNumber is autonumber i am getting an error it wants me to change the data type to string but i need it to be autonumber.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

0

AutoNumber is auto number. Since it is a numerical value, you don't need to use single quotes with it.

But more important, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

Also use using statement to dispose your OleDbConnection and OleDbCommand.

using(OleDbConnection con = new OleDbConnection(conString))
using(OleDbCommand computerStatus = con.CreateCommand())
{
    computerStatus.CommandText = "update Computer SET Status= ? where PcNumber = ?";
    computerStatus.Parameters.AddWithValue("@status", "Occupied");
    computerStatus.Parameters.AddWithValue("@number", cboComputerNo.Text);
    computerStatus.ExecuteNonQuery();
}

As LarsTech pointed, you may wanna check your cboComputerNo.Text string is a valid integer with using Int.TryParse method.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • why do i need to use using? why not make it smaller in code? – Allan Patrick Caldito Oct 12 '14 at 12:31
  • @AllanPatrickCaldito Using statement dispose unmanagement resources. Check these questions: http://stackoverflow.com/questions/10057334/when-should-i-use-the-using-statement and http://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it – Soner Gönül Oct 12 '14 at 12:51