473,388 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

primary key problem

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();
Nov 17 '05 #1
4 1782
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" <Ne****@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
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();

Nov 17 '05 #2
i need to show the value in a textbox (software requirement). i also need to
add a new row in another table wherer the value is stored in column 1. in
retrieving the data later, i need to use the value to fetch the right row in
both tables.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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" <Ne****@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
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();


Nov 17 '05 #3
Hi,

Well, you would have to change that requirement :) , I had a similar
situation so what I did was show this in the Textbox:
"N/A *"

Below in little red font:

"* The ID will be assigned after the data is inserted in the DB"

Remember you will have the ID only AFTER you insert it in the DB.

Cheers,

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

"Newbie" <Ne****@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
i need to show the value in a textbox (software requirement). i also need
to
add a new row in another table wherer the value is stored in column 1. in
retrieving the data later, i need to use the value to fetch the right row
in
both tables.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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" <Ne****@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
> 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();
>
>


Nov 17 '05 #4
> i need to show the value in a textbox (software requirement). i also
need to
add a new row in another table wherer the value is stored in column 1. in
retrieving the data later, i need to use the value to fetch the right
row in
both tables.

You could add the row right away. This will give you a new ID.

When the client enters the other data, you update the existing row instead
of inserting it.

Obviously the row will be mostly empty at first. The row could be marked
as "WaitingForData" by some field, and if it has been in that state for
more than a day, you can clean it up.

Greetings,
Wessel
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ghulam Farid | last post by:
Hi i have a table with primary key defined on col1 and col2. now i want to have col3 also included in primary key. when i alter the table it gives me error for duplicate rows. there is an option...
1
by: dev | last post by:
Hej There. We have a big problem. We have now for 4th years had a SQL Server without problems. But sutnely some of the primary keys are deleted. The subdata to the primary keys are not deleted....
9
by: 101 | last post by:
Taking a course on SQL. They are saying you can get better performance by having multiple files for a group. They then graphically show an example of "Primary" with multiple data files. I have...
7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
7
by: Philip | last post by:
Hey all, (Access 2000) I've been having a horror story with this design problem. My Database is Structured like This: AUTHORS, BOOKS, PAGES. Those are the Tables and each Item in each table...
2
by: stranger | last post by:
My database is set up so people can input parts orders. Sometimes they order the same parts on a monthly basis. I want to be able to duplicate past parts orders and have it pasted in with a new...
1
by: | last post by:
Hi, I am getting the following error when I run my Visual Basic application: "Cannot add primary key constraint since primary key is already set for the table" I am using datasets with...
3
by: Hugh O | last post by:
Hi, I am not sure if this type of question should be raised in this Newsgroup. If not please direct me. I am new to using RDO.Net data access but I thought I understood it. The 6 lines of...
8
by: shumaker | last post by:
I'm wondering if adding an autonumber primary key will improve the performance of a multiuser access database on a network share. I have a website that lists many tips for improving performance of...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.