Re: primary key problem

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,

You can't know in advance what the new value will be it's only after you
added it that you know which was assigned.

Imagine what happens if two person execute at the same time the code below?
both will get the same "id" but only one ( at the most ) will really get it
after all.


what is what you want to do?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




"Newbie" <Newbie@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B7E00AC9-B3CE-4087-89F3-A5F16339CD07@xxxxxxxxxxxxxxxx
> hello!
>
> i'm new to sql server and having some problem getting the primary key or
> index (Reference column). opening up the design table, the primary key or
> index column has an identity seed number that may vary in time. the
> identity
> increment is 1. in my code, i'm trying to get the next value and showing
> it
> in a textbox (txtReference). but i'm getting the wrong value.
>
> example: if the last row in the table TaskOrder has a value of 150 in the
> Reference column, i'm getting the value of 151. but bec. of the identity
> seed
> of 200, when the row is actually added, the value for the Reference column
> is
> 201 or higher.
>
> how do i get the actual value that the database will use? i need to change
> my select statement.
>
> my code:
>
> sReference = "Select max(Reference) From TaskOrder";
> sqlConn = new
> SqlConnection(ConfigurationSettings.AppSettings["sql2000"]);
> sqlCMD = new SqlCommand(sReference);
> sqlCMD.Connection = sqlConn;
> sqlConn.Open();
> SqlDataAdapter adapter = new SqlDataAdapter(sqlCMD);
> DataSet ds = new DataSet();
> adapter.Fill(ds);
> int tableRowCount = ds.Tables[0].Rows.Count;
> if (tableRowCount == 1)
> {
> sReference = ds.Tables[0].Rows[0].ItemArray[0].ToString();
> nReference = int.Parse(sReference) + 1;
> sReference = nReference.ToString();
> Session["sReference"] = sReference;
> txtReference.Text = sReference;
> }
> else
> {
> txtReference.Text = "1";
> }
> sqlConn.Close();
>
>


.



Relevant Pages

  • Re: primary key problem
    ... i need to show the value in a textbox. ... >> i'm new to sql server and having some problem getting the primary key or ... >> index column has an identity seed number that may vary in time. ... >> Reference column, i'm getting the value of 151. ...
    (microsoft.public.dotnet.languages.csharp)
  • primary key problem
    ... i'm new to sql server and having some problem getting the primary key or ... opening up the design table, ... Reference column, i'm getting the value of 151. ... sReference = nReference.ToString; ...
    (microsoft.public.dotnet.languages.csharp)