Reputation: 745
Hi I am submitting a form to access but want to get the access assigned auto number to display in a textbox after I submit. Below it was i have, any suggestions would be great!
string cmdstr = "Insert into TaskPerformed(TaskType,OtherType,Analyst,DateCompleted)Values(@b,@c,@d,@e)";
string query2 = "Select @@IDENTITY";
OleDbConnection con1 = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con1);
OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY", con1);//
con1.Open();
cmd.CommandText = query2;
com.ExecuteNonQuery();
con1.Close();
label16.Text = cmdNewID.ToString();
Upvotes: 0
Views: 551
Reputation: 16685
It looks like the problem you're having is because you're not executing the second command... and you're closing the connection before using it
using(OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY", con1))
{
con1.Open();
cmd.CommandText = query2;
com.ExecuteNonQuery();
label16.Text = cmdNewID.ExecuteScalar();
}
Upvotes: 3