Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using datatable to add customer information.

i am using it inside a foreach loop.
at the end of each loop am trying to add it to a data set.
but it's throwing a error saying that the data table already exists in the Set.

What should i do to overcome the error?

public DataTable  getDrugName(Product product)
    {
            DataTable drugNameTable = new DataTable("Drug Name");
            DataColumn drugNameCol = new DataColumn("Drug Name");
            drugNameTable.Columns.Add(drugNameCol);
            DataRow drugNameRow = drugNameTable.NewRow();
            drugNameRow[drugNameCol] = product.SecondaryPreferredName ;
            drugNameTable.Rows.Add(drugNameRow);
            return(drugNameTable);
           // drugInfo.Tables.Add(drugNameTable);
    }

public DataSet Drug(String drugName)
    {
          try
        {
            foreach (Product product in productList)
            {
                foreach (Pack pack in product.GetRelatedPacks(filter))
                {
                    pack.LoadAllAttributes();
                    product.LoadAllAttributes();
                    drugSet.Tables.Add(getDrugName(product));
                }
            }
            return drugSet;
        }
        catch (Exception excp)
        {
            Exception ev = new Exception(excp.Message);
            throw ev;
        }
    }
Posted
Updated 11-May-11 2:03am
v2

Mahendra, he is adding DataTable to a Dataset so No need to do
Dataset ds = new DataSet();


In your getDrugName function, at time of creating new DataTable object you are passing TableName.

Instead DataTable drugNameTable = new DataTable("Drug Name");, you should use DataTable drugNameTable = new DataTable();.

By giving table name in DataTable constructor it fixes a table name and next time it will try to add same table name and it started giving error.

Or you can pass Count parameter to your getDrugName code which uses counter in its table name.

Regards
Rushi
 
Share this answer
 
Comments
bsb25 11-May-11 8:23am    
works...thanks rushi
#realJSOP 11-May-11 10:41am    
I think you should have marked mahendra's answer as accepted as well. It was obviously the first correct answer submitted.
try this

Dataset ds = new DataSet();

//Create Your DataTable dt and then add it to DataSet like Below

ds.Tables.Add(dt);
 
Share this answer
 
Comments
#realJSOP 11-May-11 8:07am    
The code he gave you was an example to put his answer in context. You said you wanted to add a table to dataset. He showed you how to do it.

NOTE: The OP had submitted a comment, and this was a response to it. He has since deleted his comment, leaving this reply hanging in the wind.
#realJSOP 11-May-11 8:07am    
5 - proposed as answer (even if the OP didn't understand it)
#realJSOP 11-May-11 10:42am    
You were robbed. This is the first correct answer, and should have been accepted by the OP.
Mahendra.p25 12-May-11 0:38am    
Thanks John
I agree with rushijoshi..

DataTable drugNameTable = new DataTable("Drug Name"); is always returning the same table name "Drug Name"
you've got to variate the name..

DataTable drugNameTable = new DataTable(product.Name.ToString());

Something like this..?

grtz
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900