vb.net错误:对象引用未设置为对象的实例 [英] vb.net Error: Object reference not set to an instance of an object

查看:119
本文介绍了vb.net错误:对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误在以下行:
将newRow设置为DataRow = ds.Tables("Purchases_Return").NewRow
在这里提到的代码中...

The error is in the following line:
Dim newRow As DataRow = ds.Tables("Purchases_Return").NewRow
in the code mentioned here...

Private Sub btn_AddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_AddNew.Click
        Dim sql As String = "SELECT Inventory.Item_Code, Purchases_Return.PR_ID, Purchases_Return.PR_Date, Purchase_Return_Inventory.PR_Qty, Purchases_Return.PR_Ttl FROM Purchases_Return INNER JOIN (Inventory INNER JOIN Purchase_Return_Inventory ON Inventory.Item_Code = Purchase_Return_Inventory.Item_Code) ON Purchases_Return.PR_ID = Purchase_Return_Inventory.PR_ID"
                Dim da As New OleDb.OleDbDataAdapter(sql, conn)
        da.Fill(ds, "Purchase_Return, Inventory, Purchase_Return_Inventory")
        
        Dim cbS As New OleDb.OleDbCommandBuilder(da)
        Dim newRow As DataRow = ds.Tables("Purchases_Return").NewRow
        Dim newRow1 As DataRow = ds.Tables("Purchases_Return_Inventory").NewRow
        newRow("PR_ID") = txt_PR_ID.Text
        newRow("PR_Date") = txt_PR_Date.Text
        newRow("PR_Ttl") = txt_PR_ttl.Text
        ds.Tables("Purchases_Return").Rows.Add(newRow)
        newRow1("PR_ID") = txt_PR_ID.Text
        newRow1("Item_Code") = cmb_I_ID.Text
        newRow1("PR_Qty") = txt_PR_Qty.Text
        ds.Tables("Purchases_Return_Inventory").Rows.Add(newRow1)
        da.Update(ds, "Purchase_Return, Inventory, Purchase_Return_Inventory")


        DataGridView1.DataSource = ds
                DataGridView1.DataMember = "Purchase_Return,Inventory,Purchase_Return_Inventory"

    End Sub



请尽快回复我....预先感谢



kindly reply me as soon as possible....thanx in advance

推荐答案

Yasir-Iq写道:
Yasir-Iq wrote:

SELECT Inventory.Item_Code,Purchases_Return.PR_ID,Purchases_Return.PR_Date,Purchase_Return_Inventory.PR_Qty,Purchases_Return.PR_Ttl from Purchases_Return INNER JOIN(库存INNER JOIN Purchase_Return_Inventory ON库存_ID_ Purchase_ReturnReturn_Inventory_ID = Purchase_ReturnReturn_Inventory_ID = Purchase_Return_Inturn_Inventory_ID = Purchase_Return_Inturn .PR_ID

SELECT Inventory.Item_Code, Purchases_Return.PR_ID, Purchases_Return.PR_Date, Purchase_Return_Inventory.PR_Qty, Purchases_Return.PR_Ttl FROM Purchases_Return INNER JOIN (Inventory INNER JOIN Purchase_Return_Inventory ON Inventory.Item_Code = Purchase_Return_Inventory.Item_Code) ON Purchases_Return.PR_ID = Purchase_Return_Inventory.PR_ID



您认为该查询将返回多少张表?您是否在SQL Server中尝试过?这是一个Innerjoin查询,如果根据条件找到任何内容,它将返回一组表.




How many tables do you think this query is going to return? Have you tried it in your SQL Server? It''s a Innerjoin query that will return one set of table if anything found based on the condition.


Yasir-Iq写道:
Yasir-Iq wrote:

da.Fill(ds,购买_退货,库存,Purchase_Return_Inventory")

da.Fill(ds, "Purchase_Return, Inventory, Purchase_Return_Inventory")



将在数据集中创建一个名称为"Purchase_Return,Inventory,Purchase_Return_Inventory"的表.



A single table with name ''Purchase_Return, Inventory, Purchase_Return_Inventory'' is going to be created in your dataset.

Yasir-Iq写道:
Yasir-Iq wrote:

Dim newRow As DataRow = ds.Tables("Purchases_Return").NewRow Dim newRow1 As DataRow = ds.Tables("Purchases_Return_Inventory).NewRow

Dim newRow As DataRow = ds.Tables("Purchases_Return").NewRow Dim newRow1 As DataRow = ds.Tables("Purchases_Return_Inventory").NewRow


两件事:
1.您正在尝试访问数据集中的两个表,其中查询仅返回一个表
2.没有要尝试访问的名称的表.

一个简单的调试本可以告诉您所有这些.

根据单个查询结果,您尝试将数据获取到3个表中.那是不正确的.建议您在ADO.NET和DataAdaptors上阅读如何用一个以上的表填充数据集.


Two things:
1. You are trying to access TWO tables in the dataset, where your query returns only one table
2. There is no table with the name that you are trying to access.

A simple DEBUG could had told you all these.

Based on a single query result, you tried to get data into 3 tables. Thats not correct. Would suggest you to read on ADO.NET and on DataAdaptors of how to fill a dataset with more than one table.


这篇关于vb.net错误:对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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