1

I have a datatable with data. I want to add composite primary key to this table (say on 2 columns) and check if any row in the datatable validates the composite primary key we set. What I have done is I have added composite primary key to the datatable

DataColumn[] keys = new DataColumn[2];
keys[0] = dt.Columns["EmpId"];
keys[1] = dt.Columns["EmpName"];
dt.PrimaryKey = keys;

But I am not able to figure out how to check existing data and set RowError of respective rows.

quitprog
  • 491
  • 1
  • 9
  • 24

1 Answers1

0

Try with this:

1> For setting Row Errors

private void SetRowErrors(DataTable table)
{
    for(int i = 0; i < 10; i++)
    {
        table.Rows[i].RowError = "ERROR: " 
            + table.Rows[i][1];
    }
    DataSet dataSet = table.DataSet;
    TestForErrors(dataSet);
}

private void TestForErrors(DataSet dataSet)
{
    if(dataSet.HasErrors)
    {
        foreach(DataTable tempDataTable in dataSet.Tables)
        {
            if(tempDataTable.HasErrors) 
                PrintRowErrors(tempDataTable);
        }
        dataGrid1.Refresh();
    }
}

private void PrintRowErrors(DataTable table)
{
    foreach(DataRow row in table.Rows)
    {
        if(row.HasErrors) 
        {
            Console.WriteLine(row.RowError);
        }
    }
}

2>Check existing data for composite key violation: Refer the link shared below

Check existing data

Community
  • 1
  • 1
Vishal I P
  • 2,005
  • 4
  • 24
  • 41
  • I am not able to figure out how to check existing data for composite key violation. – quitprog Mar 28 '14 at 08:47
  • @parixit- i have updated my answer for checking existing data for icomposite key violation refer it.If you are satisfied with answer don't forget to vote up and to mark as an answer. – Vishal I P Mar 28 '14 at 14:58