473,399 Members | 2,858 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,399 software developers and data experts.

Primary Key in Dynamic DataSet


I have a dynamically created dataset, created thusly:

DataTable dtTime = new DataTable("times");
DataColumn [] dtColumns = new DataColumn[7];
dtColumns[0] = new DataColumn("id", System.Type.GetType("System.String"));
dtColumns[0].AutoIncrement = true;
dtColumns[0].AutoIncrementSeed = 1;
dtColumns[1] = new DataColumn("date",
System.Type.GetType("System.String"));
dtColumns[2] = new DataColumn("proj",
System.Type.GetType("System.String"));
dtColumns[3] = new DataColumn("name",
System.Type.GetType("System.String"));
dtColumns[4] = new DataColumn("desc",
System.Type.GetType("System.String"));
dtColumns[5] = new DataColumn("time",
System.Type.GetType("System.String"));
dtColumns[6] = new DataColumn("saved",
System.Type.GetType("System.Boolean"));
dtTime.Columns.AddRange(dtColumns);
dtTime.PrimaryKey = new DataColumn [] { dtColumns[0] };
this._TimeSheets.Tables.Add(dtTime);

The first column is an autoincrementing ID field, and is also set to be
the primary key.

Later on in my code, in an attempt to either add a new record (row) or
update an existing row, I'm using the following code:

oTask = oFrm.Task;
object [] newRow = new object[7];

if(oTask.TaskID == 0)
{
// add a new row
newRow[0] = null;
}
else
{
// should update already existing row
newRow[0] = oTask.TaskID;
}

newRow[1] = oTask.TaskDate;
newRow[2] = oTask.TaskProject;
newRow[3] = oTask.TaskName;
newRow[4] = oTask.TaskDescription;
newRow[5] = oTask.TaskTime;
newRow[6] = oTask.TaskReported;

this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);

However, on the LoadDataRow call, I'm getting the following exception:

An unhandled exception of type 'System.Data.ConstraintException' occurred
in system.data.dll

Additional information: Column 'id' is constrained to be unique. Value
'1' is already present.

Well of course the id already has a value of '1', I want to update this
row, which is what the LoadDataRow is supposed to do. If it find the row,
based on the primary key, it updates it. Otherwise is inserts a new row.
So why would I be getting this error?? And how can I overcome it??

Thanx!
Nov 17 '05 #1
1 7392
I guess I found my own answer... dunno why this happens, but if I change
the line:

this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);

to

this._TimeSheets.Tables["times"].BeginLoadData();
this._TimeSheets.Tables["times"].AcceptChanges();
this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);
this._TimeSheets.Tables["times"].EndLoadData();

it works as expected... "'Curiouser and curiouser', said Alice..."

On Wed, 19 Oct 2005 09:27:59 -0700, JSheble <js***********@logicor.com>
wrote:

I have a dynamically created dataset, created thusly:

DataTable dtTime = new DataTable("times");
DataColumn [] dtColumns = new DataColumn[7];
dtColumns[0] = new DataColumn("id",
System.Type.GetType("System.String"));
dtColumns[0].AutoIncrement = true;
dtColumns[0].AutoIncrementSeed = 1;
dtColumns[1] = new DataColumn("date",
System.Type.GetType("System.String"));
dtColumns[2] = new DataColumn("proj",
System.Type.GetType("System.String"));
dtColumns[3] = new DataColumn("name",
System.Type.GetType("System.String"));
dtColumns[4] = new DataColumn("desc",
System.Type.GetType("System.String"));
dtColumns[5] = new DataColumn("time",
System.Type.GetType("System.String"));
dtColumns[6] = new DataColumn("saved",
System.Type.GetType("System.Boolean"));
dtTime.Columns.AddRange(dtColumns);
dtTime.PrimaryKey = new DataColumn [] { dtColumns[0] };
this._TimeSheets.Tables.Add(dtTime);

The first column is an autoincrementing ID field, and is also set to be
the primary key.

Later on in my code, in an attempt to either add a new record (row) or
update an existing row, I'm using the following code:

oTask = oFrm.Task;
object [] newRow = new object[7];

if(oTask.TaskID == 0)
{
// add a new row
newRow[0] = null;
}
else
{
// should update already existing row
newRow[0] = oTask.TaskID;
}

newRow[1] = oTask.TaskDate;
newRow[2] = oTask.TaskProject;
newRow[3] = oTask.TaskName;
newRow[4] = oTask.TaskDescription;
newRow[5] = oTask.TaskTime;
newRow[6] = oTask.TaskReported;

this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);

However, on the LoadDataRow call, I'm getting the following exception:

An unhandled exception of type 'System.Data.ConstraintException'
occurred in system.data.dll

Additional information: Column 'id' is constrained to be unique. Value
'1' is already present.

Well of course the id already has a value of '1', I want to update this
row, which is what the LoadDataRow is supposed to do. If it find the
row, based on the primary key, it updates it. Otherwise is inserts a
new row. So why would I be getting this error?? And how can I overcome
it??

Thanx!


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Nov 17 '05 #2

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

Similar topics

2
by: Sam | last post by:
Hello everyone, I have a table, which contains a picture column, I put URL info into it. "www.myweb.com/1.jpg..." I want to show this picture in my crystal report, I find some samples show the...
1
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
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...
10
by: Bernie Yaeger | last post by:
I have a need to add a primary key to a dataset/datatable. How can this be done using a standard oledb data provider? Tx for any help.
9
by: Steven C. | last post by:
Hello: I'm getting an error, "primary key not defined" when trying to use the FIND method on the DataTable Rows collection. I have a typed dataset called 'MortgagesDS' that I created with the...
0
by: Phil | last post by:
Environment: XP sp2 VS.Net 2005 (Asp.Net 2.0 - c#) Sql Server 2005 Scenario: -Table (Table1) with two columns: Table1Id <- Primary Key Column1
5
by: Monty M. | last post by:
Hello; I was wondering if anyone can assist me with this problem. Here are the tools I am using: Language: C# Database: MS SQL Server 2000 Application: Visual Studio 2005 1. I have a...
1
by: GS | last post by:
set primary key option is greyed out in the dataset designer for a table Details In an vb.net express 2005 project, I have a dataset associated with table. lets call this table codetable. the...
1
by: Lacutas | last post by:
Hi I'm having some problems getting a dynamic LINQ query to work on my DataSet. The idea is that a user selects certain criteria, and then the LINQ query filters through the dataset making the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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,...
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...
0
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,...
0
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...

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.