G
Guest
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();
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();