locked
C# inserting with Primarykey output RRS feed

  • Question

  • User-577390650 posted

    hi

    i try use the SCOPE_IDENTITY()  but i got always error ou no value

    anyone can explain me step by step how can i configure the sqldatasource to get the primary key when inserting something

     

    thanks

    Thursday, January 5, 2012 4:13 PM

Answers

  • User-1226263862 posted

    Your SqlDataSource needs to have a InsertParameter specified for each of those 60+ parameters and the @NewId parameter also needs to be specified in your InsertParameters with the Direction property set to Output.  Also no need to perform a DataBind on the SqlDataSource on the Datasource.Inserted event.  And no need to specify the NewId parameter in code behind as it will be defined in the aspx page.

    Suggest reading this article on using SqlDataSource to perform Insert, Update and Deletes. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 5, 2012 9:02 PM

All replies

  • User-1226263862 posted

    Check out this post.

    Thursday, January 5, 2012 4:18 PM
  • User-577390650 posted

    i already check

    but i dont know why dont work with me.

    Thursday, January 5, 2012 6:03 PM
  • User-1226263862 posted
    Show your code.
    Thursday, January 5, 2012 6:07 PM
  • User-577390650 posted

    hi,

    this is my code

     

    asp. inset comand in sqldatasource

    InsertCommand="INSERT INTO dbo_Artigos(af, sq, ar, hy, az, eu, be, bg, ca, zhHant, zhHans, hr, cs, da, dv, nl, en, et, fo, fa, fi, fr, gl, ka, de, el, gu, he, hi, hu, [is], id, it, ja, kn, kk, kok, ko, kyKG, lv, lt, mk, ms, mr, mn, no, pl, pt, pa, ro, ru, sa, srCyrlCS, sk, sl, es, sw, sv, syr, ta, tt, te, th, tr, uk, ur, uz, vi, ArtigosPK) VALUES (@af, @sq, @ar, @hy, @az, @eu, @be, @bg, @ca, @zhHant, @zhHans, @hr, @cs, @da, @dv, @nl, @en, @et, @fo, @fa, @fi, @fr, @gl, @ka, @de, @el, @gu, @he, @hi, @hu, @is , @id, @it, @ja, @kn, @kk, @kok, @ko, @kyKG, @lv, @lt, @mk, @ms, @mr, @mn, @no, @pl, @pt, @pa, @ro, @ru, @sa, @srCyrlCS, @sk, @sl, @es, @sw, @sv, @syr, @ta, @tt, @te, @th, @tr, @uk, @ur, @uz, @vi,); SELECT @NewID = SCOPE_IDENTITY()"

    behind code

           protected void NovoArtigoSqlDataSource_Inserted(object sender, SqlDataSourceStatusEventArgs e)

            {

                NovoArtigoSqlDataSource.DataBind();

               int lastID = (int)e.Command.Parameters["@NewID"].Value;

                UltimoArtigoAdicionadoLabel.Text = lastID.ToString();

            }

           protected void NovoArtigoSqlDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e)

            {

                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter();

                p.DbType = System.Data.DbType.Int32;

                p.Direction = System.Data.ParameterDirection.Output;

                p.ParameterName ="NewID";

                e.Command.Parameters.Add(p);

            }

    can you help?

    Thursday, January 5, 2012 8:15 PM
  • User-1226263862 posted

    Your SqlDataSource needs to have a InsertParameter specified for each of those 60+ parameters and the @NewId parameter also needs to be specified in your InsertParameters with the Direction property set to Output.  Also no need to perform a DataBind on the SqlDataSource on the Datasource.Inserted event.  And no need to specify the NewId parameter in code behind as it will be defined in the aspx page.

    Suggest reading this article on using SqlDataSource to perform Insert, Update and Deletes. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 5, 2012 9:02 PM
  • User3866881 posted

    SCOPE_IDENTITY() 

    This will return you only an identitied primary key,So plz make sure that your primary key is identitied flag。

    Best reguards!

    Friday, January 6, 2012 9:29 PM