A column named 'Prize' already belongs to this DataTable

613 views Asked by At

I have developed an application in winform which on the main from it imports an excel file and display it. on the second form user ask to create N random numbers with specific prize and i store them in a 2D array. after that i pull the same records from the excel file matching ID with random numbers i created and display them. this is how i pull the data and bind them with the random numbers:

Dictionary<string, string> winnersList = new Dictionary<string, string>();
for (int i = 0; i < MyVariables.TotalWinners - 1; i++)
{
    winnersList.Add(MyVariables.Winners[i, 0], MyVariables.Winners[i, 1]);
}
string idList = string.Join("','", winnersList.Select(x => x.Key));
dsWinners.Tables.Add(Form1.ds.Tables[0].Select("ID IN ('" + idList + "')").CopyToDataTable());
dsWinners.Tables[0].Columns.Add("Prize", typeof(string));
dsWinners.Tables[0].Rows.OfType<DataRow>().ToList().ForEach(x =>
{
    x["Prize"] = winnersList[x["ID"].ToString()];
});
dsWinners.Tables[0].Columns["ID"].SetOrdinal(0);
dsWinners.Tables[0].Columns["Prize"].SetOrdinal(1);
dataGridView1.DataSource = dsWinners.Tables[0];

now everything works fine on the first time till i try to draw/create a new list. thats when i get this error on this line:

dsWinners.Tables[0].Columns.Add("Prize", typeof(string));

now i tried to dispose dsWinners in form closing,

i tried removing the columns just before in a try and catch like:

Dictionary<string, string> winnersList = new Dictionary<string, string>();
dsWinners.Tables[0].Columns.Remove("Prize");
for (int i = 0; i < MyVariables.TotalWinners - 1; i++)
{
    winnersList.Add(MyVariables.Winners[i, 0], MyVariables.Winners[i, 1]);
}
string idList = string.Join("','", winnersList.Select(x => x.Key));
try
{
    dsWinners.Tables.Add(Form1.ds.Tables[0].Select("ID IN ('" + idList + "')").CopyToDataTable());
    dsWinners.Tables[0].Columns.Add("Prize", typeof(string));
    dsWinners.Tables[0].Rows.OfType<DataRow>().ToList().ForEach(x =>
    {
        x["Prize"] = winnersList[x["ID"].ToString()];
    });
    dsWinners.Tables[0].Columns["ID"].SetOrdinal(0);
    dsWinners.Tables[0].Columns["Prize"].SetOrdinal(1);
    dataGridView1.DataSource = dsWinners.Tables[0];
}
catch
{
    dsWinners.Tables.Add(Form1.ds.Tables[0].Select("ID IN ('" + idList + "')").CopyToDataTable());
    dsWinners.Tables[0].Columns.Add("Prize", typeof(string));
    dsWinners.Tables[0].Rows.OfType<DataRow>().ToList().ForEach(x =>
    {
        x["Prize"] = winnersList[x["ID"].ToString()];
    });
    dsWinners.Tables[0].Columns["ID"].SetOrdinal(0);
    dsWinners.Tables[0].Columns["Prize"].SetOrdinal(1);

    dataGridView1.DataSource = dsWinners.Tables[0];
}

and i get this error "The given key was not present in the dictionary" on this line in catch:

x["Prize"] = winnersList[x["ID"].ToString()];

when i try to do this at form closing of the form:

dsWinners.Tables[0].Clear();
dsWinners.Tables[0].Rows.Clear();
dsWinners.Tables[0].Columns.Clear();

i get this error "Object reference not set to an instance of an object." at this line:

dsWinners.Tables[0].Columns["ID"].SetOrdinal(0);

and if i remove that line and the one bellow it, i dont get any error but i dont get any result as well! once again, it works fine the first time but when i close the form and redo it ...i get these errors. how can i overcome this problem?

0

There are 0 answers