我的编码有点问题.我想做的是在表中添加一行,但是我遇到了错误.任何想法请帮助我 [英] I've a bit problem with my coding. What i m trying to do is to add a row in a table, but i m having errors. Any idea Pls help me

查看:76
本文介绍了我的编码有点问题.我想做的是在表中添加一行,但是我遇到了错误.任何想法请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I've a bit problem with my coding. What i m trying to do is to add a row in a table, but i m having errors.
When i use
Dim row as Datarow
row = ds.Tables("Admin").Rows.Add()
I m having error "Object reference not set to an instance of an object."

any idea please help me





问候
......





regards
......

推荐答案

您正试图访问null或未设置的对象的成员. 阅读有关此错误的更多信息这里 [
You are trying to access a member of an object that is null or not set.
Read more about this error here[^].


此链接将指导您

http://www.homeandlearn.co.uk/net/nets12p10.html [ ^ ]

http: //www.wallaceit.co.uk/asp-net-vb/adding-a-new-row-to-datatable-with-vb-net/208/article.aspx [ http://msdn.microsoft.com/en-us/library/5ycd1034 (v = vs.71).aspx [
this links will guide you

http://www.homeandlearn.co.uk/net/nets12p10.html[^]

http://www.wallaceit.co.uk/asp-net-vb/adding-a-new-row-to-datatable-with-vb-net/208/article.aspx[^]

http://msdn.microsoft.com/en-us/library/5ycd1034(v=vs.71).aspx[^]


将dsNewRow用作DataRow

如果要向数据集添加新行,则需要一个DataRow对象.该行仅设置了一个名为dsNewRow的变量.变量的类型是DataRow.

要创建新的DataRow对象,请在下一行:

dsNewRow = ds.Tables("AddressBook").NewRow()

我们只是说:在AddressBook数据集中创建一个新行对象,并将其存储在名为dsNewRow的变量中."如您所见,NewRow()是ds.Tables的方法.使用此方法将行添加到您的DataSet.

我们要存储在行中的实际值来自文本框.所以我们有这两行:

dsNewRow.Item("FirstName")= txtFirstName.Text
dsNewRow.Item("Surname")= txtSurname.Text

我们创建的dsNewRow对象具有一个称为Item的属性.这就像您先前使用的Item属性.它代表数据集中的一列.我们可以这样说:

dsNewRow.Item(1)= txtFirstName.Text
dsNewRow.Item(2)= txtSurname.Text

Item属性现在使用的是DataSet列的索引号,而不是名称.结果是一样的:在这些属性中存储新值.我们会将文本从文本框中存储到新行中.

现在,我们只需要调用将行实际添加到数据集的方法即可:

ds.Tables("AddressBook").Rows.Add(dsNewRow)

若要添加行,请使用DataSet的Rows属性的Add方法.在圆括号之间,需要输入DataRow的名称(在本例中为dsNewRow变量).
Dim dsNewRow As DataRow

If you want to add a new row to your DataSet, you need a DataRow object. This line just sets up a variable called dsNewRow. The type of variable is a DataRow.

To create the new DataRow object, this line comes next:

dsNewRow = ds.Tables("AddressBook").NewRow()

We''re just saying, "Create a New Row object in the AddressBook DataSet, and store this in the variable called dsNewRow." As you can see, NewRow() is a method of ds.Tables. Use this method to add rows to your DataSet.

The actual values we want to store in the rows are coming from the textboxes. So we have these two lines:

dsNewRow.Item("FirstName") = txtFirstName.Text
dsNewRow.Item("Surname") = txtSurname.Text

The dsNewRow object we created has a Property called Item. This is like the Item property you used earlier. It represents a column in your DataSet. We could have said this instead:

dsNewRow.Item(1) = txtFirstName.Text
dsNewRow.Item(2) = txtSurname.Text

The Item property is now using the index number of the DataSet columns, rather than the names. The results is the same, though: to store new values in these properties. We''re storing the text from the textboxes to our new Row.

We now only need to call the Method that actually adds the Row to the DataSet:

ds.Tables("AddressBook").Rows.Add(dsNewRow)

To add the Row, you use the Add method of the Rows property of the DataSet. In between the round brackets, you need the name of your DataRow (the variable dsNewRow, in our case).


这篇关于我的编码有点问题.我想做的是在表中添加一行,但是我遇到了错误.任何想法请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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