Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
OleDbConnection h5 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\users\\IJC Database.accdb");
            OleDbCommand cmd5;
            h5.Open();
            cmd5 = new OleDbCommand("select MAX(ID) from ijc"+";",h5);
            cmd5.ExecuteNonQuery(); 


how can i take the result from cmd5 command and give this result to variable.
Posted
Updated 24-Jun-13 8:02am
v2

ExecuteNonQuery returns the number of rows involved in the command. Its probably running your command, but returning 0 since no rows were affected. ExecuteNonQuery should be used for Updates, Inserts, and Deletes, not Selects.

Instead of using cmd5.ExecuteNonQuery(), do this:

C#
int maxID = (int)cmd5.ExecuteScalar();


Then maxID will hold the MaxID value from the query.
 
Share this answer
 
Comments
loai_maane 24-Jun-13 14:08pm    
thnx Ron :))
Ron Beyer 24-Jun-13 14:16pm    
If one of these (or both really) was the answer, please mark them as answer so people can reference them in the future. Thanks!
loai_maane 24-Jun-13 14:29pm    
Ron you know in access database, the id field i want to sort it again .
this the code .
OleDbConnection h5 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\IJC Database.accdb");
OleDbConnection h6 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\users\\IJC Database.accdb");
OleDbCommand cmd5;
OleDbCommand cmd6;
h5.Open();
cmd5 = new OleDbCommand("select count(ID) from ijc"+";",h5);
int countID = (int)cmd5.ExecuteScalar();
h5.Close();
h6.Open();
for (int i = 1; i <= countID; i++)
{
cmd6 = new OleDbCommand("update ijc set ID='"+i+"';",h6);
cmd6.ExecuteNonQuery();

}
h6.Close();

but how can i move to the next id... you understand me ??
Use ExecuteScalar instead of ExecuteNonQuery.

See DbCommand.ExecuteScalar Method
 
Share this answer
 
v4
Comments
loai_maane 24-Jun-13 14:08pm    
thnx:))

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