473,699 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read inserted Identity value

I'm trying to update every table in DataSet, but getting inserted "id_user"
gets me an exception:
column "id_user" is read only

if(_dataSet.Has Changes())
{
SqlDataAdapter _adapter;
SqlCommandBuild er _builder;
SqlConnection conn = new
SqlConnection(G lobalSettings.C onnectionString );
conn.Open();
foreach(DataTab le dt in _dataSet.Tables )
{
string selectString = "SELECT * FROM " + dt.TableName;
_adapter = new SqlDataAdapter( selectString, conn);
_builder = new SqlCommandBuild er(_adapter);
_adapter.RowUpd ated += new
SqlRowUpdatedEv entHandler(_ada pter_RowUpdated );
_adapter.Update (dt);
}
}
private void _adapter_RowUpd ated(object sender, SqlRowUpdatedEv entArgs e)
{

SqlCommand command = new SqlCommand("SEL ECT @@IDENTITY",
e.Command.Conne ction);
object o = command.Execute Scalar();
e.Row.Table.Col umns["id_osoba"].ReadOnly = false;
e.Row["id_user"] = o;
e.Row.AcceptCha nges();
} conn.Close();
Please make comments about this code, but my prime problem is WHY do i get
an exception:
"column 'id_user' is read only"???
of course it's read only, it's Identity column, but everywhere i look, thi
is recommended way to retrieve inserted Identity

Thax for help.
Nov 17 '05 #1
2 5839
How is your table declared ?

If your field ("id_user") is Identity (presuming your table is in MSSQL
database) then you cannot write data to the field, the value is
automatically assigned by MSSQL.

Fitim Skenderi

"Danko Greiner" <no*********@bc c.com> wrote in message
news:dk******** **@ss405.t-com.hr...
I'm trying to update every table in DataSet, but getting inserted
"id_user" gets me an exception:
column "id_user" is read only

if(_dataSet.Has Changes())
{
SqlDataAdapter _adapter;
SqlCommandBuild er _builder;
SqlConnection conn = new
SqlConnection(G lobalSettings.C onnectionString );
conn.Open();
foreach(DataTab le dt in _dataSet.Tables )
{
string selectString = "SELECT * FROM " + dt.TableName;
_adapter = new SqlDataAdapter( selectString, conn);
_builder = new SqlCommandBuild er(_adapter);
_adapter.RowUpd ated += new
SqlRowUpdatedEv entHandler(_ada pter_RowUpdated );
_adapter.Update (dt);
}
}
private void _adapter_RowUpd ated(object sender, SqlRowUpdatedEv entArgs e)
{

SqlCommand command = new SqlCommand("SEL ECT @@IDENTITY",
e.Command.Conne ction);
object o = command.Execute Scalar();
e.Row.Table.Col umns["id_osoba"].ReadOnly = false;
e.Row["id_user"] = o;
e.Row.AcceptCha nges();
} conn.Close();
Please make comments about this code, but my prime problem is WHY do i get
an exception:
"column 'id_user' is read only"???
of course it's read only, it's Identity column, but everywhere i look, thi
is recommended way to retrieve inserted Identity

Thax for help.

Nov 17 '05 #2
Hi Danko,

I suggest you to avoid using SqlCommandBuild er and rather create sql
commands by hand.
The insert statement would look something like: INSERT INTO table (...)
VALUES (...); SELECT SCOPE_IDENTITY( ) As Id (assuming Id is your pk)

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Danko Greiner" <no*********@bc c.com> wrote in message
news:dk******** **@ss405.t-com.hr...
I'm trying to update every table in DataSet, but getting inserted
"id_user" gets me an exception:
column "id_user" is read only

if(_dataSet.Has Changes())
{
SqlDataAdapter _adapter;
SqlCommandBuild er _builder;
SqlConnection conn = new
SqlConnection(G lobalSettings.C onnectionString );
conn.Open();
foreach(DataTab le dt in _dataSet.Tables )
{
string selectString = "SELECT * FROM " + dt.TableName;
_adapter = new SqlDataAdapter( selectString, conn);
_builder = new SqlCommandBuild er(_adapter);
_adapter.RowUpd ated += new
SqlRowUpdatedEv entHandler(_ada pter_RowUpdated );
_adapter.Update (dt);
}
}
private void _adapter_RowUpd ated(object sender, SqlRowUpdatedEv entArgs e)
{

SqlCommand command = new SqlCommand("SEL ECT @@IDENTITY",
e.Command.Conne ction);
object o = command.Execute Scalar();
e.Row.Table.Col umns["id_osoba"].ReadOnly = false;
e.Row["id_user"] = o;
e.Row.AcceptCha nges();
} conn.Close();
Please make comments about this code, but my prime problem is WHY do i get
an exception:
"column 'id_user' is read only"???
of course it's read only, it's Identity column, but everywhere i look, thi
is recommended way to retrieve inserted Identity

Thax for help.

Nov 17 '05 #3

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

Similar topics

1
8744
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware tblSoftware ------------------- ------------------ ---------------------- PID PName PID* SID* SID SWName --- ----- --- --- --- ------ 1 Thomas 1 1 ...
4
13243
by: Carlos San Miguel | last post by:
Hi, I'm going to be using Oracle with a customer. I will access Oracle with ODBC. All the tables have an Identity column, this is the ID for the record. I need to know how I can get the value of the Identity column for the last record inserted. A stored procedure maybe? TIA Carlos San Miguel
6
1744
by: Billy Cormic | last post by:
Hello, I would like do an insert into a table. The table has an auto incrimenting unique int id. After I do the insert how do i get the unique int id of the record that I just inserted? Is there a straight forward way of accomplishing this? Thanks, Billy
1
261
by: Alberto | last post by:
The following example works, but the retrival of the returned identity does not work 'Set Command Object Propertie objCommand.ActiveConnection = GV_objConnectio objCommand.CommandText = "DAO.RCCBABY_Add_Test objCommand.CommandType = ADODB.CommandTypeEnum.adCmdStoredPro 'Set The Parameter objCommand.Parameters.Append(objCommand.CreateParameter("TestKey", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamReturnValue)...
4
7151
by: louise raisbeck | last post by:
Hi, I do a sqlcommand.executenonquery on the click of a button, which performs an insert statement. What is the most reliable way (i.e. someone else could insert at similar time) to get the id i have JUST created?
1
4413
by: Dan Sikorsky | last post by:
Using: MS SQL Server 2005 Sept 05 CTP, MS Visual Studio 2005 Beta 2 How can I get the SQL Server Row ID of the newly inserted row when adding a new record to the database using a FormView Control on a web page? I've looked at the FormView_ItemInserted FormViewInsertedEventArgs but to no avail. I need to immediately update a secondary table using this ID gotten from the newly created row.
1
1614
by: Craig | last post by:
In ASP.NET 2.0 and the formview control how do you get the value of the identity field of a newly inserted record? In ASP.NET 1.1 after the new record was saved you just said intNewIdentityID = Dataset.tblMtTable.IndentityID Simple but can't seem to do it in the formview. Thanks
8
1938
by: Doru Roman | last post by:
Hi, I insert a new record with a command: private SqlCommand comInsert; comInsert = conDataBase.CreateCommand(); comInsert.CommandType = CommandType.Text;
3
2846
by: Susanne Klemm | last post by:
Hello! I use a procedure to insert a new row into a table with an identity column. The procedure has an output parameter which gives me the inserted identity value. This worked well for a long time. Now the identity value is over 700.000 and I get errors whiles retrieving the inserted identitiy value. If I delete rows and reset the identity everything works well again. So I think it is a data type problem. My Procedure:
0
8687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9174
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9034
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
8883
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
7750
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
6534
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
5874
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
4376
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
3057
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

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.