473,386 Members | 1,827 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,386 software developers and data experts.

c# and SQL Server problem.

My application is using an identity field of an SQL Server.
Identity field called "ID" is a Primary Key too. The problem
is when i add some record to the table SQL Server is
assigning it sime new id. But thouse is not returned into a
Data Table so the id number on SQL Server is not equal
thouse in the data table. So when I'm trying for exemple
delete row, it causes an error because UPDATE command
cannot find an ID on the SQL Server equal to the DataSet Table.
Close and re open table solves that problem but is not acceptable
for performance goals ...

How to make DataSet Table to retrieve id from the SQL Server
after insert command?
Nov 17 '05 #1
3 1537
Jacek,

The only way to do this would be to select the identity from SQL server
after the insert, like this:

select @@identity

This query will return the last inserted identity. You can then get
this value and set it on your row.

Either way, you will have to perform another query to get that value.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jacek Jurkowski" <jj********@datacomp.szczecin.pl> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
My application is using an identity field of an SQL Server.
Identity field called "ID" is a Primary Key too. The problem
is when i add some record to the table SQL Server is
assigning it sime new id. But thouse is not returned into a
Data Table so the id number on SQL Server is not equal
thouse in the data table. So when I'm trying for exemple
delete row, it causes an error because UPDATE command
cannot find an ID on the SQL Server equal to the DataSet Table.
Close and re open table solves that problem but is not acceptable
for performance goals ...

How to make DataSet Table to retrieve id from the SQL Server
after insert command?

Nov 17 '05 #2
"Jacek Jurkowski" <jj********@datacomp.szczecin.pl> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
My application is using an identity field of an SQL Server.
Identity field called "ID" is a Primary Key too. The problem
is when i add some record to the table SQL Server is
assigning it sime new id. But thouse is not returned into a
Data Table so the id number on SQL Server is not equal
thouse in the data table. So when I'm trying for exemple
delete row, it causes an error because UPDATE command
cannot find an ID on the SQL Server equal to the DataSet Table.
Close and re open table solves that problem but is not acceptable
for performance goals ...

How to make DataSet Table to retrieve id from the SQL Server
after insert command?


Are you using a data adapter? If so just change your INSERT statement to
look like this:

INSERT INTO customers(name, addr, city, state, zip) VALUES(@name, @addr,
@city, @state, @zip) ; SELECT id, name, addr, city, state, zip FROM
customers WHERE id = @@IDENTITY

Similarly, for the UPDATE statement, you should do something like:

UPDATE customers SET name = @name, ... WHERE id = @id ; SELECT id, name,
addr, city, state, zip FROM customers WHERE id = @id

This will catch any timestamp or similar columns that sql might have updated
but your app otherwise wouldn't know about.

HTH,

Mike Rodriguez
Nov 17 '05 #3
So maby I should replace identity field with
Guid assigned by myself? Another select will
slow down the performance and the mathod
with changing the INSERT command ... The
problem is that command is generated automatically
by CommandBuilder and I would like to keep that way ...
Nov 17 '05 #4

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

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
2
by: epaetz | last post by:
I'm getting Not associated with a trusted SQL Server connection errors on a .Net windows service I wrote, when it's running on my application server. It's not a problem with mixed mode...
0
by: AlessanBar | last post by:
Hello Friends !! I have a strange problem, and I need to know what would be the source of this. I have a laptop computer with the following configuration: Pentium III Brand : Toshiba Speed :...
3
by: Michael | last post by:
Dear All I have problem with my database server which running SQL server 2000. The server running very slow. The worst case, to save a record required more than 20-30 seconds. Since this...
2
by: Helge Kalnes | last post by:
We are running an ASP.NET application on a cluster of 3 web-servers, using the Network Load Balancing feature of Application Center. We have synchronized the machineKey in machine.config on the 3...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
22
by: EP | last post by:
When running my asp.net hosting service (asp.net without IIS), on server 2003 with IIS not installed, I get the following when trying to process a request. "System.DllNotFoundException: Unable to...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.