如何将表添加到数据集中.

问题描述:

我正在使用数据表添加客户信息.

我正在foreach循环中使用它.
在每个循环的最后,我试图将其添加到数据集中.
但这会引发错误,表明数据表已存在于Set中.

我应该怎么做才能克服错误?

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;
        }
    }

Mahendra,他正在将DataTable添加到数据集中,因此无需执行
Mahendra, he is adding DataTable to a Dataset so No need to do
Dataset ds = new DataSet();



getDrugName函数中,在创建新的DataTable对象时,您正在传递TableName.

应该使用DataTable drugNameTable = new DataTable();代替DataTable drugNameTable = new DataTable("Drug Name");.

通过在DataTable构造函数中提供表名,它可以修复表名,下一次它将尝试添加相同的表名,并开始提供错误.

或者,您可以将Count参数传递给您的getDrugName代码,该代码在其表名称中使用counter.

问候
鲁西(Rushi)



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


尝试一下

try this

Dataset ds = new DataSet();

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

ds.Tables.Add(dt);


我同意rushijoshi ..

DataTable drugNameTable = new DataTable("Drug Name");总是返回相同的表名"Drug Name"
您必须更改名称..

DataTable drugNameTable =新的DataTable(product.Name.ToString());

像这样..?

grtz
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