insert into sql and get scope identity

i need to get the scope identity once i insert a new row into sql. I'm not sure how to get the id of the newly created row. see my code below.
protected void loadCRSQL()
    {      
        string tempCRSON = lblDocN.Text.Remove(0, 2);
        int crSON = Convert.ToInt32(tempCRSON);
        string crBLN = txtbxBLog.Text.ToString();
        string crCC = txtbxCC.Text.ToString();    

        SqlConnection dbConnection = new SqlConnection(connectionString);
        
        string insertCRString = "Insert into cR ([crSON],[crBLN], ";
        insertCRString += "[crCC]) ";
        insertCRString += "VALUES (@crSON,@crBLN,@crCC) ";
	insertCRString += "select @id = SCOPE_IDENTITY()"; 
        //sql command
        SqlCommand dbCommand = new SqlCommand();
        dbCommand.CommandText = insertCRString;
        dbCommand.Connection = dbConnection;
        //
        dbCommand.Parameters.Add(new SqlParameter("@crSON", SqlDbType.Int));
        dbCommand.Parameters["@crSON"].Value = crSON;
        dbCommand.Parameters.Add(new SqlParameter("@crBLN", SqlDbType.VarChar, 25));
        dbCommand.Parameters["@crBLN"].Value = crBLN;
        dbCommand.Parameters.Add(new SqlParameter("@crCC", SqlDbType.VarChar, 25));
        dbCommand.Parameters["@crCC"].Value = crCC;
        
        dbCommand.CommandTimeout = 60;

        //
        try
        {
            dbConnection.Open();
            dbCommand.ExecuteNonQuery();
            prepCRDSQL(int iIdentity); //how do I get the scope_identity to pass it to another sql insert?
        }
        catch (SqlException ex)
        {
            writeError(ex.Message.ToString());
            sendErrorEmail(ex.Message.ToString());
            Response.Redirect("error.aspx");
        }
        finally
        {
            //close db conn
            dbConnection.Close();
        }
    }

Open in new window

vicominAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HugoHiaslCommented:
if you ude ms sql you could query

select @@identity

with a command after the insert.
0
Dirk HaestProject managerCommented:
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Alfred A.Commented:
0
Om PrakashCommented:


dbCommand.Parameters.Add(new SqlParameter("@crSON", SqlDbType.Int)); 
dbCommand.Parameters["@crSON"].Value = crSON; 
dbCommand.Parameters.Add(new SqlParameter("@crBLN", SqlDbType.VarChar, 25)); 
dbCommand.Parameters["@crBLN"].Value = crBLN; 
dbCommand.Parameters.Add(new SqlParameter("@crCC", SqlDbType.VarChar, 25)); 
dbCommand.Parameters["@crCC"].Value = crCC; 

SqlParameter id = new SqlParameter("@id", SqlDbType.Int);            
id.Direction = ParameterDirection.Output;            
dbCommand.Parameters.Add(id);            

dbConnection.Open(); 
dbCommand.ExecuteNonQuery(); 
id = (int)id.Value;

Open in new window

0
RoySharpCommented:
As you haven't said which db you use here's the answer for MySQL

select last_insert_id() from tableName;

This is per-connection so you get the id for your last insert statement.
0
vicominAuthor Commented:
using sql server 2005. so is the code below a good solution?
protected void loadCRSQL()
    {      
        string tempCRSON = lblDocN.Text.Remove(0, 2);
        int crSON = Convert.ToInt32(tempCRSON);
        string crBLN = txtbxBLog.Text.ToString();
        string crCC = txtbxCC.Text.ToString();    

            int iScopeIdentity = 0;

        SqlConnection dbConnection = new SqlConnection(connectionString);
        
        string insertCRString = "Insert into cR ([crSON],[crBLN], ";
        insertCRString += "[crCC]) ";
        insertCRString += "VALUES (@crSON,@crBLN,@crCC) ";
	insertCRString += "SET @crScopeId = SCOPE_IDENTITY()"; 
        //sql command
        SqlCommand dbCommand = new SqlCommand();
        dbCommand.CommandText = insertCRString;
        dbCommand.Connection = dbConnection;
        //
        dbCommand.Parameters.Add(new SqlParameter("@crSON", SqlDbType.Int));
        dbCommand.Parameters["@crSON"].Value = crSON;
        dbCommand.Parameters.Add(new SqlParameter("@crBLN", SqlDbType.VarChar, 25));
        dbCommand.Parameters["@crBLN"].Value = crBLN;
        dbCommand.Parameters.Add(new SqlParameter("@crCC", SqlDbType.VarChar, 25));
        dbCommand.Parameters["@crCC"].Value = crCC;

        SqlParameter scopeIdParameter = new SqlParameter("@crScopeId", SqlDbType.Int);
        scopeIdParameter.Direction = ParameterDirection.Output;
        dbCommand.Parameters.Add(scopeIdParameter);

        
        dbCommand.CommandTimeout = 60;

        //
        try
        {
            dbConnection.Open();
            dbCommand.ExecuteNonQuery();
	    iScopeIdentity = (int)scopeIdParameter.Value;
            prepCRDSQL(iScopeIdentity); //how do I get the scope_identity to pass it to another sql insert?
        }
        catch (SqlException ex)
        {
            writeError(ex.Message.ToString());
            sendErrorEmail(ex.Message.ToString());
            Response.Redirect("error.aspx");
        }
        finally
        {
            //close db conn
            dbConnection.Close();
        }
    }

Open in new window

0
Alfred A.Commented:
Hi,

Did you tried to run it?  That was my approach in one of my app before and it worked and I just followed the instructions in the very first link I provided.
0
Alfred A.Commented:
Hi,

Oh.  You assigned the points already.  I didn't noticed that.  Thank you. :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.