C
cjheath
Hi
I am having some problems retrieving the last inserted id from the DB,
I really want to chuck this into a variable so I can use it anywhere.
The following is what I have got so far :-
string connStr =
ConfigurationManager.ConnectionStrings["dataConn"].ConnectionString;
SqlConnection objConn = new SqlConnection(connStr);
SqlCommand objCmd;
objConn.Open();
objCmd = new SqlCommand("rab_sp_err_addError", objConn);
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.AddWithValue("@err_ErrorType", strErrorType);
objCmd.Parameters.AddWithValue("@err_BrowserType", strBrowserType);
objCmd.Parameters.AddWithValue("@err_Page", strPage);
objCmd.Parameters.AddWithValue("@sitename", strSiteName);
objCmd.Parameters.AddWithValue("@sitestatus", strSiteStatus);
objCmd.ExecuteNonQuery();
objConn.Close();
SqlCommand cmd = new SqlCommand("SELECT @@IDENTITY AS id", objConn);
objConn.Open();
object result = cmd.ExecuteScalar();
if (!(result == null))
{
Label1.Text += String.Format("{0:d}", result);
Label1.Text += "result!!<br />";
}
else
{
Label1.Text = "no match";
}
The first block of text runs fine and the record is put into the DB
but the second block where I try to get the indentity field brings
back nothing or if I play around with this it says something about an
incorrect cast.
Am I doing this completely the wrong way or is there something I have
missed since I am new to dot net.
Thanks
Chris
I am having some problems retrieving the last inserted id from the DB,
I really want to chuck this into a variable so I can use it anywhere.
The following is what I have got so far :-
string connStr =
ConfigurationManager.ConnectionStrings["dataConn"].ConnectionString;
SqlConnection objConn = new SqlConnection(connStr);
SqlCommand objCmd;
objConn.Open();
objCmd = new SqlCommand("rab_sp_err_addError", objConn);
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.AddWithValue("@err_ErrorType", strErrorType);
objCmd.Parameters.AddWithValue("@err_BrowserType", strBrowserType);
objCmd.Parameters.AddWithValue("@err_Page", strPage);
objCmd.Parameters.AddWithValue("@sitename", strSiteName);
objCmd.Parameters.AddWithValue("@sitestatus", strSiteStatus);
objCmd.ExecuteNonQuery();
objConn.Close();
SqlCommand cmd = new SqlCommand("SELECT @@IDENTITY AS id", objConn);
objConn.Open();
object result = cmd.ExecuteScalar();
if (!(result == null))
{
Label1.Text += String.Format("{0:d}", result);
Label1.Text += "result!!<br />";
}
else
{
Label1.Text = "no match";
}
The first block of text runs fine and the record is put into the DB
but the second block where I try to get the indentity field brings
back nothing or if I play around with this it says something about an
incorrect cast.
Am I doing this completely the wrong way or is there something I have
missed since I am new to dot net.
Thanks
Chris