PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
Re: LoadDataRow
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
Re: LoadDataRow
![]() |
Re: LoadDataRow |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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! |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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. |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

