Re: primary key problem
- From: "Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us>
- Date: Tue, 26 Jul 2005 13:42:32 -0400
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();
>
>
.
- Follow-Ups:
- Re: primary key problem
- From: Newbie
- Re: primary key problem
- References:
- primary key problem
- From: Newbie
- primary key problem
- Prev by Date: RE: Type Casting
- Next by Date: Re: C# Retain PlaceHolder ViewState
- Previous by thread: primary key problem
- Next by thread: Re: primary key problem
- Index(es):
Relevant Pages
|