PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Re: LoadDataRow

Reply

Re: LoadDataRow

 
Thread Tools Rate Thread
Old 04-09-2003, 08:04 AM   #1
Igor Zhavrid
Guest
 
Posts: n/a
Default Re: LoadDataRow


Try this modified code

class Class1
{
static void Main(string[] args)
{
DataTable workTable = new DataTable("Customers");
DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.AutoIncrement = true;

DataColumn Col1 = workTable.Columns.Add("blah",typeof(Int32));
DataColumn Col2 = workTable.Columns.Add("blah2", typeof(Int32));
DataColumn Col3 = workTable.Columns.Add("StartD",typeof(string));

DataColumn[] PK = new DataColumn[]{Col1, Col2, Col3};


workTable.PrimaryKey = PK;
workTable.AcceptChanges();
workTable.BeginLoadData();
for(int i=0; i<10;i++)
{
workTable.LoadDataRow(new Object[]{null,i,i+2,"foo"},true);
}
for(int i=0; i<10;i++)
{
workTable.LoadDataRow(new Object[]{null,i,i+2,"foo"},true);
}
workTable.EndLoadData();
DataRow[] currRows = workTable.Select(null, null,
DataViewRowState.CurrentRows);
foreach (DataRow myRow in currRows)
{
foreach (DataColumn myCol in workTable.Columns)
Console.Write("\t{0}", myRow[myCol]);
Console.WriteLine("\t" + myRow.RowState);
}

Console.ReadLine();

}
}


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  Reply With Quote
Old 04-09-2003, 11:52 PM   #2
Mark Ihimoyan [MSFT]
Guest
 
Posts: n/a
Default Re: LoadDataRow

Well the exception is expected in this case because you have an auto
increment column which is not a primary key (or a part of primary key).
You’re adding duplicate records and getting an exception as expected.

In order for you not to get this exception you'll have to add the auto
increment column to the PK[ ]
as shown below
DataColumn[] PK = new DataColumn[]{workCol,Col1, Col2, Col3};

Please let me know if you need more help with this.

Thanks.

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off