在数据集中创建新行 [英] Create new row in dataset

查看:92
本文介绍了在数据集中创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在数据集中添加新行,该数据集已在数据集中创建,名称已停用,

i得到以下错误:

对象引用未设置为实例一个对象。



有什么想法吗?



我尝试了什么:



i am trying to add new row in dataset for table already created in dataset with name retired ,
i get below error:
Object reference not set to an instance of an object.

any idea please?

What I have tried:

 DataRow dr = ds.Tables["retired"].NewRow();
                   dr["Empno"] = empno.Text;
                   dr["Empname"] = name.Text;
                   dr["Formno"] = formon.Text ;
                   dr["x1"] = "X";
ds.Tables["retired"].Rows.Add(dr);

推荐答案

没有更多信息,我只能猜测可能是退休表中不存在Tables集合。
Without more info, I can only guess that it might be that the table "retired" does not exist in the Tables collection.


First Thing First。替换ds.Tables [retired]。NewRow();与ds.Tables [0] .NewRow()。不要给表名。提供表ID。它将解决您的问题。



您的示例

DataSet ds = new DataSet();

DataTable dt = new DataTable();

dt.Columns.Add(ID,typeof(string));

dt.Columns.Add(name, typeof(string));

DataRow dr = dt.NewRow();

dr [ID] =1;

dr [name] =Chaueby;

dt.Rows.Add(dr);

ds.Tables.Add(dt);

//来自Here Your Solution Start

DataRow dr1 = ds.Tables [0] .NewRow();

dr1 [ID] =Chaubey1 ;

dr1 [name] =Chaubasdasdey1;

ds.Tables [0] .Rows.Add(dr1);

你的解决方案应该是这样的。 。 NO表名
First Thing First . replace ds.Tables["retired"].NewRow(); with ds.Tables[0].NewRow(). Dont give table name . Give Table ID. And it will solve your issue.

Example For you
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("name",typeof(string));
DataRow dr = dt.NewRow();
dr["ID"] = "1";
dr["name"] = "Chaueby";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
//from Here your Solution Start
DataRow dr1 = ds.Tables[0].NewRow();
dr1["ID"] = "Chaubey1";
dr1["name"] = "Chaubasdasdey1";
ds.Tables[0].Rows.Add(dr1);
Your solution should be like this. . NO Table Name


公共部分类Class1:Page

{

DataSet ds = null;

protected void Page_Load(object sender,EventArgs e)

{

DataSet ds2 = GetDataSet();



DataTable dt2 = ds.Tables [retired];

DataRow dr = dt2.NewRow();

dr [Empno] =2;

dr [Empname] =Aa;

dr [Formno] =Bb;



dt2.Rows.Add(dr);

ds.Tables.Remove(已退休);

ds.Tables.Add(dt2);

}



public DataSet GetDataSet()

{

ds = new DataSet() ;

DataTable dt = new DataTable(已退役);

dt.Columns.Add(Empno);

dt.Columns .Add(Empname);

dt.Columns.Add(Formno);



DataRow dr = dt.NewRow();

dr [Empno] =1;

dr [Empname] =A;

dr [Formno] =B;



dt.Rows.Add(博士);

ds.Tables.Add(dt);

返回ds;

}

}

查看此代码是否有帮助。
public partial class Class1 : Page
{
DataSet ds = null;
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds2 = GetDataSet();

DataTable dt2 = ds.Tables["retired"];
DataRow dr = dt2.NewRow();
dr["Empno"] = "2";
dr["Empname"] = "Aa";
dr["Formno"] = "Bb";

dt2.Rows.Add(dr);
ds.Tables.Remove("retired");
ds.Tables.Add(dt2);
}

public DataSet GetDataSet()
{
ds = new DataSet();
DataTable dt = new DataTable("retired");
dt.Columns.Add("Empno");
dt.Columns.Add("Empname");
dt.Columns.Add("Formno");

DataRow dr = dt.NewRow();
dr["Empno"] = "1";
dr["Empname"] = "A";
dr["Formno"] = "B";

dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
}
Check this code if it may help you.


这篇关于在数据集中创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆