Wednesday, 15 May 2013

c# - Why don't I have a definition for 'ExecuteScalar'? -



c# - Why don't I have a definition for 'ExecuteScalar'? -

i have searched reply find there typo in someone's code.

i have not found why code not have definition executescalar(). might need capture customer_id when add together row sql database because auto increment.

here code had problem:

if (customer_idtextbox == null) { sqlconnection.open(); string sql = "select max(customer_id) customer"; int maxid = convert.toint32(sql.executescalar()); sqlconnection.close(); }

you need create instance of sqlcommand, assign connection , query it.

try code instead. (a couple bits of advice. i've surrounded connection , command in using statements there's no need close anything... they'll disposed of. also, seek create connections , commands close possible point you're going need them.)

int maxid = -1; if (customer_idtextbox == null) { using (var sqlconnection = new sqlconnection(/* connection string */)) { sqlconnection.open(); string query = "select max(customer_id) customer"; using (var sqlcommand = new sqlcommand(query, sqlconnection)) { maxid = convert.toint32(sqlcommand.executescalar()); } } }

c# .net sqlconnection

No comments:

Post a Comment