C# Database @@identity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Micheal

    #1

    C# Database @@identity

    How do I get the @@identity value without using stored procedures in C#?

    I have the a table named 'sports' that has an @@identity value for the
    primary key.

    What I want to do is after I insert into into the 'sports' table, I want to
    get the @@identity value (primary key) for what I just inserted.

    I have to following code if this helps, thanks in advance:

    ---------------------------------------------------------

    string sql = "insert into sports (name.......";

    conn.Open();

    // insert record into the database
    SqlCommand cmd = new SqlCommand(sql, conn);
    SqlDataReader result = cmd.ExecuteRead er();
    result.Close();

    // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
    SqlCommand ident = new SqlCommand("sel ect @@identity from sports", conn);
    SqlDataReader ident_result = ident.ExecuteRe ader();

    int sport_id = -1;
    if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }

    ident_result.Cl ose();
    conn.Dispose();


  • Swanand Mokashi

    #2
    Re: C# Database @@identity

    rather use :
    string sql = "insert into sports (name.......);S ELECT @@IDENTITY;"
    Now use your SqlDataReader result = to getvalue of the first column which is
    the new identity

    --
    Swanand Mokashi
    Microsoft Certified Professional
    The personal homepage of Swanand Mokashi — engineer, founder of Malaika Consultants, and a curious tinkerer writing about technology, music, and life.

    Home of the Stock Quotes, Quote of the day and Horoscope web services
    "Micheal" <frooyo@sbcglob al.net> wrote in message
    news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > How do I get the @@identity value without using stored procedures in C#?
    >
    > I have the a table named 'sports' that has an @@identity value for the
    > primary key.
    >
    > What I want to do is after I insert into into the 'sports' table, I want[/color]
    to[color=blue]
    > get the @@identity value (primary key) for what I just inserted.
    >
    > I have to following code if this helps, thanks in advance:
    >
    > ---------------------------------------------------------
    >
    > string sql = "insert into sports (name.......";
    >
    > conn.Open();
    >
    > // insert record into the database
    > SqlCommand cmd = new SqlCommand(sql, conn);
    > SqlDataReader result = cmd.ExecuteRead er();
    > result.Close();
    >
    > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
    > SqlCommand ident = new SqlCommand("sel ect @@identity from sports", conn);
    > SqlDataReader ident_result = ident.ExecuteRe ader();
    >
    > int sport_id = -1;
    > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
    >
    > ident_result.Cl ose();
    > conn.Dispose();
    >
    >[/color]


    Comment

    • S. Justin Gengo

      #3
      Re: C# Database @@identity

      Micheal,

      It will work if you just do the select @@identity within your insert
      statement.

      Then use ExecuteScalar to get it out.

      Like this:

      strSQL = "INSERT INTO sports (name) VALUES (@name);SELECT @@Identity"
      SQLCommand.Comm andText = strSQL
      IdReturned = SQLCommand.Exec uteScalar

      I hope this helps.
      --
      S. Justin Gengo, MCP
      Web Developer

      Free code library at:
      www.aboutfortunate.com

      "Out of chaos comes order."
      Nietzche
      "Micheal" <frooyo@sbcglob al.net> wrote in message
      news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > How do I get the @@identity value without using stored procedures in C#?
      >
      > I have the a table named 'sports' that has an @@identity value for the
      > primary key.
      >
      > What I want to do is after I insert into into the 'sports' table, I want[/color]
      to[color=blue]
      > get the @@identity value (primary key) for what I just inserted.
      >
      > I have to following code if this helps, thanks in advance:
      >
      > ---------------------------------------------------------
      >
      > string sql = "insert into sports (name.......";
      >
      > conn.Open();
      >
      > // insert record into the database
      > SqlCommand cmd = new SqlCommand(sql, conn);
      > SqlDataReader result = cmd.ExecuteRead er();
      > result.Close();
      >
      > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
      > SqlCommand ident = new SqlCommand("sel ect @@identity from sports", conn);
      > SqlDataReader ident_result = ident.ExecuteRe ader();
      >
      > int sport_id = -1;
      > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
      >
      > ident_result.Cl ose();
      > conn.Dispose();
      >
      >[/color]


      Comment

      • Swanand Mokashi

        #4
        Re: C# Database @@identity

        yes executescalar is a even better option

        --
        Swanand Mokashi
        Microsoft Certified Professional
        The personal homepage of Swanand Mokashi — engineer, founder of Malaika Consultants, and a curious tinkerer writing about technology, music, and life.

        Home of the Stock Quotes, Quote of the day and Horoscope web services
        "S. Justin Gengo" <sjgengo@aboutf ortunate.com> wrote in message
        news:uMe3HEuUDH A.736@TK2MSFTNG P09.phx.gbl...[color=blue]
        > Micheal,
        >
        > It will work if you just do the select @@identity within your insert
        > statement.
        >
        > Then use ExecuteScalar to get it out.
        >
        > Like this:
        >
        > strSQL = "INSERT INTO sports (name) VALUES (@name);SELECT @@Identity"
        > SQLCommand.Comm andText = strSQL
        > IdReturned = SQLCommand.Exec uteScalar
        >
        > I hope this helps.
        > --
        > S. Justin Gengo, MCP
        > Web Developer
        >
        > Free code library at:
        > www.aboutfortunate.com
        >
        > "Out of chaos comes order."
        > Nietzche
        > "Micheal" <frooyo@sbcglob al.net> wrote in message
        > news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=green]
        > > How do I get the @@identity value without using stored procedures in C#?
        > >
        > > I have the a table named 'sports' that has an @@identity value for the
        > > primary key.
        > >
        > > What I want to do is after I insert into into the 'sports' table, I want[/color]
        > to[color=green]
        > > get the @@identity value (primary key) for what I just inserted.
        > >
        > > I have to following code if this helps, thanks in advance:
        > >
        > > ---------------------------------------------------------
        > >
        > > string sql = "insert into sports (name.......";
        > >
        > > conn.Open();
        > >
        > > // insert record into the database
        > > SqlCommand cmd = new SqlCommand(sql, conn);
        > > SqlDataReader result = cmd.ExecuteRead er();
        > > result.Close();
        > >
        > > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
        > > SqlCommand ident = new SqlCommand("sel ect @@identity from sports",[/color][/color]
        conn);[color=blue][color=green]
        > > SqlDataReader ident_result = ident.ExecuteRe ader();
        > >
        > > int sport_id = -1;
        > > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
        > >
        > > ident_result.Cl ose();
        > > conn.Dispose();
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Micheal

          #5
          Re: C# Database @@identity

          Hmm....

          I am doing the following (see below) and it does __not__ work? It error's
          at cmd.ExecuteScal ar() stating:
          "cannot implicity convert type 'object' to SqlDataReader"

          Any ideas?

          -------------------------------------------------
          string sql = "insert into sports (name) value ('test'); select @@identity";

          conn.Open();

          // insert record into the database
          SqlCommand cmd = new SqlCommand(sql, conn);
          SqlDataReader result = cmd.ExecuteScal ar();

          int sport_id = -1;
          if (result.Read()) { sport_id = result.GetDecim al(0); }

          result.Close();
          conn.Dispose();
          --------------------------------------------------------






          "Micheal" <frooyo@sbcglob al.net> wrote in message
          news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          > How do I get the @@identity value without using stored procedures in C#?
          >
          > I have the a table named 'sports' that has an @@identity value for the
          > primary key.
          >
          > What I want to do is after I insert into into the 'sports' table, I want[/color]
          to[color=blue]
          > get the @@identity value (primary key) for what I just inserted.
          >
          > I have to following code if this helps, thanks in advance:
          >
          > ---------------------------------------------------------
          >
          > string sql = "insert into sports (name.......";
          >
          > conn.Open();
          >
          > // insert record into the database
          > SqlCommand cmd = new SqlCommand(sql, conn);
          > SqlDataReader result = cmd.ExecuteRead er();
          > result.Close();
          >
          > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
          > SqlCommand ident = new SqlCommand("sel ect @@identity from sports", conn);
          > SqlDataReader ident_result = ident.ExecuteRe ader();
          >
          > int sport_id = -1;
          > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
          >
          > ident_result.Cl ose();
          > conn.Dispose();
          >
          >[/color]


          Comment

          • S. Justin Gengo

            #6
            Re: C# Database @@identity

            Micheal,

            The ExecuteScalar method returns a single value. Instead of a datareader it
            is returning the id itself you just need to declare an integer variable and
            set it equal to the execute scalar line like I showed in my example.

            It returns as a type object so you'll have to cast it as int.


            --
            S. Justin Gengo, MCP
            Web Developer

            Free code library at:
            www.aboutfortunate.com

            "Out of chaos comes order."
            Nietzche
            "Micheal" <frooyo@sbcglob al.net> wrote in message
            news:%23ZC9YZuU DHA.1956@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > Hmm....
            >
            > I am doing the following (see below) and it does __not__ work? It error's
            > at cmd.ExecuteScal ar() stating:
            > "cannot implicity convert type 'object' to SqlDataReader"
            >
            > Any ideas?
            >
            > -------------------------------------------------
            > string sql = "insert into sports (name) value ('test'); select[/color]
            @@identity";[color=blue]
            >
            > conn.Open();
            >
            > // insert record into the database
            > SqlCommand cmd = new SqlCommand(sql, conn);
            > SqlDataReader result = cmd.ExecuteScal ar();
            >
            > int sport_id = -1;
            > if (result.Read()) { sport_id = result.GetDecim al(0); }
            >
            > result.Close();
            > conn.Dispose();
            > --------------------------------------------------------
            >
            >
            >
            >
            >
            >
            > "Micheal" <frooyo@sbcglob al.net> wrote in message
            > news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=green]
            > > How do I get the @@identity value without using stored procedures in C#?
            > >
            > > I have the a table named 'sports' that has an @@identity value for the
            > > primary key.
            > >
            > > What I want to do is after I insert into into the 'sports' table, I want[/color]
            > to[color=green]
            > > get the @@identity value (primary key) for what I just inserted.
            > >
            > > I have to following code if this helps, thanks in advance:
            > >
            > > ---------------------------------------------------------
            > >
            > > string sql = "insert into sports (name.......";
            > >
            > > conn.Open();
            > >
            > > // insert record into the database
            > > SqlCommand cmd = new SqlCommand(sql, conn);
            > > SqlDataReader result = cmd.ExecuteRead er();
            > > result.Close();
            > >
            > > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
            > > SqlCommand ident = new SqlCommand("sel ect @@identity from sports",[/color][/color]
            conn);[color=blue][color=green]
            > > SqlDataReader ident_result = ident.ExecuteRe ader();
            > >
            > > int sport_id = -1;
            > > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
            > >
            > > ident_result.Cl ose();
            > > conn.Dispose();
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Swanand Mokashi

              #7
              Re: C# Database @@identity

              ExecuteScalar returns the 1st column of the 1st row of your query result.
              This is by default returned as type object
              use :
              int sport_id = (int) cmd.ExecuteScal ar();

              --
              Swanand Mokashi
              Microsoft Certified Professional
              The personal homepage of Swanand Mokashi — engineer, founder of Malaika Consultants, and a curious tinkerer writing about technology, music, and life.

              Home of the Stock Quotes, Quote of the day and Horoscope web services
              "Micheal" <frooyo@sbcglob al.net> wrote in message
              news:%23ZC9YZuU DHA.1956@TK2MSF TNGP10.phx.gbl. ..[color=blue]
              > Hmm....
              >
              > I am doing the following (see below) and it does __not__ work? It error's
              > at cmd.ExecuteScal ar() stating:
              > "cannot implicity convert type 'object' to SqlDataReader"
              >
              > Any ideas?
              >
              > -------------------------------------------------
              > string sql = "insert into sports (name) value ('test'); select[/color]
              @@identity";[color=blue]
              >
              > conn.Open();
              >
              > // insert record into the database
              > SqlCommand cmd = new SqlCommand(sql, conn);
              > SqlDataReader result = cmd.ExecuteScal ar();
              >
              > int sport_id = -1;
              > if (result.Read()) { sport_id = result.GetDecim al(0); }
              >
              > result.Close();
              > conn.Dispose();
              > --------------------------------------------------------
              >
              >
              >
              >
              >
              >
              > "Micheal" <frooyo@sbcglob al.net> wrote in message
              > news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=green]
              > > How do I get the @@identity value without using stored procedures in C#?
              > >
              > > I have the a table named 'sports' that has an @@identity value for the
              > > primary key.
              > >
              > > What I want to do is after I insert into into the 'sports' table, I want[/color]
              > to[color=green]
              > > get the @@identity value (primary key) for what I just inserted.
              > >
              > > I have to following code if this helps, thanks in advance:
              > >
              > > ---------------------------------------------------------
              > >
              > > string sql = "insert into sports (name.......";
              > >
              > > conn.Open();
              > >
              > > // insert record into the database
              > > SqlCommand cmd = new SqlCommand(sql, conn);
              > > SqlDataReader result = cmd.ExecuteRead er();
              > > result.Close();
              > >
              > > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
              > > SqlCommand ident = new SqlCommand("sel ect @@identity from sports",[/color][/color]
              conn);[color=blue][color=green]
              > > SqlDataReader ident_result = ident.ExecuteRe ader();
              > >
              > > int sport_id = -1;
              > > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
              > >
              > > ident_result.Cl ose();
              > > conn.Dispose();
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Neil Ramsbottom

                #8
                Re: C# Database @@identity


                Just for the record (I know that people have pointed you in the right
                direction), but the code you posted wouldn't have worked because the
                @@identity would only be in scope for that transaction.

                Given that no other statements were executed prior to the select (in
                particular, nothing that affects an identity field) - @@identity would'nt
                return a value... actually it might return 0, but I could be wrong.



                Neil Ramsbottom


                "Micheal" <frooyo@sbcglob al.net> wrote in message
                news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                > How do I get the @@identity value without using stored procedures in C#?
                >
                > I have the a table named 'sports' that has an @@identity value for the
                > primary key.
                >
                > What I want to do is after I insert into into the 'sports' table, I want[/color]
                to[color=blue]
                > get the @@identity value (primary key) for what I just inserted.
                >
                > I have to following code if this helps, thanks in advance:
                >
                > ---------------------------------------------------------
                >
                > string sql = "insert into sports (name.......";
                >
                > conn.Open();
                >
                > // insert record into the database
                > SqlCommand cmd = new SqlCommand(sql, conn);
                > SqlDataReader result = cmd.ExecuteRead er();
                > result.Close();
                >
                > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
                > SqlCommand ident = new SqlCommand("sel ect @@identity from sports", conn);
                > SqlDataReader ident_result = ident.ExecuteRe ader();
                >
                > int sport_id = -1;
                > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
                >
                > ident_result.Cl ose();
                > conn.Dispose();
                >
                >[/color]


                Comment

                • Micheal

                  #9
                  Re: C# Database @@identity

                  The below does _not_ work! The datatype for the autoincrement value is INT
                  in SQL Server 2000. The strange thing is that if I use a 'decimal' it works
                  (using .NET framework 1.1)

                  ---------------------------------------------------------
                  string sql = "insert into sports (name) value ('test'); select
                  @@identity";

                  conn.Open();

                  // insert record into the database
                  SqlCommand cmd = new SqlCommand(sql, conn);
                  int result = (int)cmd.Execut eScalar();
                  ----------------------------------------------------------





                  "S. Justin Gengo" <sjgengo@aboutf ortunate.com> wrote in message
                  news:OPUtF5uUDH A.2004@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > Micheal,
                  >
                  > The ExecuteScalar method returns a single value. Instead of a datareader[/color]
                  it[color=blue]
                  > is returning the id itself you just need to declare an integer variable[/color]
                  and[color=blue]
                  > set it equal to the execute scalar line like I showed in my example.
                  >
                  > It returns as a type object so you'll have to cast it as int.
                  >
                  >
                  > --
                  > S. Justin Gengo, MCP
                  > Web Developer
                  >
                  > Free code library at:
                  > www.aboutfortunate.com
                  >
                  > "Out of chaos comes order."
                  > Nietzche
                  > "Micheal" <frooyo@sbcglob al.net> wrote in message
                  > news:%23ZC9YZuU DHA.1956@TK2MSF TNGP10.phx.gbl. ..[color=green]
                  > > Hmm....
                  > >
                  > > I am doing the following (see below) and it does __not__ work? It[/color][/color]
                  error's[color=blue][color=green]
                  > > at cmd.ExecuteScal ar() stating:
                  > > "cannot implicity convert type 'object' to SqlDataReader"
                  > >
                  > > Any ideas?
                  > >
                  > > -------------------------------------------------
                  > > string sql = "insert into sports (name) value ('test'); select[/color]
                  > @@identity";[color=green]
                  > >
                  > > conn.Open();
                  > >
                  > > // insert record into the database
                  > > SqlCommand cmd = new SqlCommand(sql, conn);
                  > > SqlDataReader result = cmd.ExecuteScal ar();
                  > >
                  > > int sport_id = -1;
                  > > if (result.Read()) { sport_id = result.GetDecim al(0); }
                  > >
                  > > result.Close();
                  > > conn.Dispose();
                  > > --------------------------------------------------------
                  > >
                  > >
                  > >
                  > >
                  > >
                  > >
                  > > "Micheal" <frooyo@sbcglob al.net> wrote in message
                  > > news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
                  > > > How do I get the @@identity value without using stored procedures in[/color][/color][/color]
                  C#?[color=blue][color=green][color=darkred]
                  > > >
                  > > > I have the a table named 'sports' that has an @@identity value for the
                  > > > primary key.
                  > > >
                  > > > What I want to do is after I insert into into the 'sports' table, I[/color][/color][/color]
                  want[color=blue][color=green]
                  > > to[color=darkred]
                  > > > get the @@identity value (primary key) for what I just inserted.
                  > > >
                  > > > I have to following code if this helps, thanks in advance:
                  > > >
                  > > > ---------------------------------------------------------
                  > > >
                  > > > string sql = "insert into sports (name.......";
                  > > >
                  > > > conn.Open();
                  > > >
                  > > > // insert record into the database
                  > > > SqlCommand cmd = new SqlCommand(sql, conn);
                  > > > SqlDataReader result = cmd.ExecuteRead er();
                  > > > result.Close();
                  > > >
                  > > > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
                  > > > SqlCommand ident = new SqlCommand("sel ect @@identity from sports",[/color][/color]
                  > conn);[color=green][color=darkred]
                  > > > SqlDataReader ident_result = ident.ExecuteRe ader();
                  > > >
                  > > > int sport_id = -1;
                  > > > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
                  > > >
                  > > > ident_result.Cl ose();
                  > > > conn.Dispose();
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • S. Justin Gengo

                    #10
                    Re: C# Database @@identity

                    Interesting!

                    Well, I mostly program in vb.net and only a little in C#. It's good to know
                    about this difference.

                    --
                    S. Justin Gengo, MCP
                    Web Developer

                    Free code library at:
                    www.aboutfortunate.com

                    "Out of chaos comes order."
                    Nietzche
                    "Micheal" <frooyo@sbcglob al.net> wrote in message
                    news:OA4qRJrVDH A.3404@tk2msftn gp13.phx.gbl...[color=blue]
                    > The below does _not_ work! The datatype for the autoincrement value is[/color]
                    INT[color=blue]
                    > in SQL Server 2000. The strange thing is that if I use a 'decimal' it[/color]
                    works[color=blue]
                    > (using .NET framework 1.1)
                    >
                    > ---------------------------------------------------------
                    > string sql = "insert into sports (name) value ('test'); select
                    > @@identity";
                    >
                    > conn.Open();
                    >
                    > // insert record into the database
                    > SqlCommand cmd = new SqlCommand(sql, conn);
                    > int result = (int)cmd.Execut eScalar();
                    > ----------------------------------------------------------
                    >
                    >
                    >
                    >
                    >
                    > "S. Justin Gengo" <sjgengo@aboutf ortunate.com> wrote in message
                    > news:OPUtF5uUDH A.2004@TK2MSFTN GP11.phx.gbl...[color=green]
                    > > Micheal,
                    > >
                    > > The ExecuteScalar method returns a single value. Instead of a datareader[/color]
                    > it[color=green]
                    > > is returning the id itself you just need to declare an integer variable[/color]
                    > and[color=green]
                    > > set it equal to the execute scalar line like I showed in my example.
                    > >
                    > > It returns as a type object so you'll have to cast it as int.
                    > >
                    > >
                    > > --
                    > > S. Justin Gengo, MCP
                    > > Web Developer
                    > >
                    > > Free code library at:
                    > > www.aboutfortunate.com
                    > >
                    > > "Out of chaos comes order."
                    > > Nietzche
                    > > "Micheal" <frooyo@sbcglob al.net> wrote in message
                    > > news:%23ZC9YZuU DHA.1956@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
                    > > > Hmm....
                    > > >
                    > > > I am doing the following (see below) and it does __not__ work? It[/color][/color]
                    > error's[color=green][color=darkred]
                    > > > at cmd.ExecuteScal ar() stating:
                    > > > "cannot implicity convert type 'object' to SqlDataReader"
                    > > >
                    > > > Any ideas?
                    > > >
                    > > > -------------------------------------------------
                    > > > string sql = "insert into sports (name) value ('test'); select[/color]
                    > > @@identity";[color=darkred]
                    > > >
                    > > > conn.Open();
                    > > >
                    > > > // insert record into the database
                    > > > SqlCommand cmd = new SqlCommand(sql, conn);
                    > > > SqlDataReader result = cmd.ExecuteScal ar();
                    > > >
                    > > > int sport_id = -1;
                    > > > if (result.Read()) { sport_id = result.GetDecim al(0); }
                    > > >
                    > > > result.Close();
                    > > > conn.Dispose();
                    > > > --------------------------------------------------------
                    > > >
                    > > >
                    > > >
                    > > >
                    > > >
                    > > >
                    > > > "Micheal" <frooyo@sbcglob al.net> wrote in message
                    > > > news:%23KbRy7tU DHA.2184@TK2MSF TNGP10.phx.gbl. ..
                    > > > > How do I get the @@identity value without using stored procedures in[/color][/color]
                    > C#?[color=green][color=darkred]
                    > > > >
                    > > > > I have the a table named 'sports' that has an @@identity value for[/color][/color][/color]
                    the[color=blue][color=green][color=darkred]
                    > > > > primary key.
                    > > > >
                    > > > > What I want to do is after I insert into into the 'sports' table, I[/color][/color]
                    > want[color=green][color=darkred]
                    > > > to
                    > > > > get the @@identity value (primary key) for what I just inserted.
                    > > > >
                    > > > > I have to following code if this helps, thanks in advance:
                    > > > >
                    > > > > ---------------------------------------------------------
                    > > > >
                    > > > > string sql = "insert into sports (name.......";
                    > > > >
                    > > > > conn.Open();
                    > > > >
                    > > > > // insert record into the database
                    > > > > SqlCommand cmd = new SqlCommand(sql, conn);
                    > > > > SqlDataReader result = cmd.ExecuteRead er();
                    > > > > result.Close();
                    > > > >
                    > > > > // ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
                    > > > > SqlCommand ident = new SqlCommand("sel ect @@identity from sports",[/color]
                    > > conn);[color=darkred]
                    > > > > SqlDataReader ident_result = ident.ExecuteRe ader();
                    > > > >
                    > > > > int sport_id = -1;
                    > > > > if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
                    > > > >
                    > > > > ident_result.Cl ose();
                    > > > > conn.Dispose();
                    > > > >
                    > > > >
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    Working...