473,758 Members | 5,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C onfigurationSet tings.AppSettin gs["sql2000"]);
sqlCMD = new SqlCommand(sRef erence);
sqlCMD.Connecti on = 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(sRefe rence) + 1;
sReference = nReference.ToSt ring();
Session["sReference "] = sReference;
txtReference.Te xt = sReference;
}
else
{
txtReference.Te xt = "1";
}
sqlConn.Close() ;
Nov 17 '05 #1
4 1795
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****@discuss ions.microsoft. com> wrote in message
news:B7******** *************** ***********@mic rosoft.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(C onfigurationSet tings.AppSettin gs["sql2000"]);
sqlCMD = new SqlCommand(sRef erence);
sqlCMD.Connecti on = 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(sRefe rence) + 1;
sReference = nReference.ToSt ring();
Session["sReference "] = sReference;
txtReference.Te xt = sReference;
}
else
{
txtReference.Te xt = "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****@discuss ions.microsoft. com> wrote in message
news:B7******** *************** ***********@mic rosoft.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(C onfigurationSet tings.AppSettin gs["sql2000"]);
sqlCMD = new SqlCommand(sRef erence);
sqlCMD.Connecti on = 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(sRefe rence) + 1;
sReference = nReference.ToSt ring();
Session["sReference "] = sReference;
txtReference.Te xt = sReference;
}
else
{
txtReference.Te xt = "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****@discuss ions.microsoft. com> wrote in message
news:1D******** *************** ***********@mic rosoft.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****@discuss ions.microsoft. com> wrote in message
news:B7******** *************** ***********@mic rosoft.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(C onfigurationSet tings.AppSettin gs["sql2000"]);
> sqlCMD = new SqlCommand(sRef erence);
> sqlCMD.Connecti on = 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(sRefe rence) + 1;
> sReference = nReference.ToSt ring();
> Session["sReference "] = sReference;
> txtReference.Te xt = sReference;
> }
> else
> {
> txtReference.Te xt = "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
3713
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 for 'with nocheck' but it only works with check or foreign key constraint. is there any option in sql server like in oracle 'no validate' which doesnt validate the existing data and force the data validation from new records. thanx Farid
1
1212
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. This is a big problem because This is a billing system. Recently there was over 300 primay keys deleted. Good that we have backup but still... NOOOT good Can anyone help me to solve this problem!!! PLEASE
9
6865
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 tried altering PRIMARY to have multiple data files and I get and error. I have tried creating a new database with multiple PRIMARY files and get an error. I can ALTER and CREATE secondary files with multiple data files with no
7
2956
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 trying to insert. I therefore do not think that I can use the sql "Insert Into" command. Here is a simplified illustration of my tables: tblFather NaturalKey1 NatuarlKey2
7
5351
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 needs a unique ID# based on its context. Primary Keys AUTHORS = AuthorID - NO Duplicates
2
3233
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 primary key. My first attempt seemed to work(minus a few Microsoft glitches). I used a "duplicate" command button at first. This made a copy of the record but failed to bump up the primary key by 1 number(so it seemed). Actually, the number did...
1
3828
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 primary keys which are filled from an SQL Server database. These datasets were automatically
3
3226
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 code below is simply trying to load an empty table that has only three columns. In the code I set the Name and Type fields. The third column is a unique primary key set to Integer. I have set the Primary Key to Identity with a seed of 1 and...
8
3273
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 access, but doesn't mention primary keys. However, it seems logical to think that having no primary key means that when a user updates a record, the database has to do comparisons on multiple fields to identify the specific record being...
9
3909
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 eg: area.txt, school.txt, students.txt.... and so on (ok?!?) now, 1. area code used in the school.txt must be defined in the area.txt (Primary key in area => area_code defined in area.txt & Foreign key on school => area_code defined in...
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.