你调用的对象是空的

时间:2010-04-14 07:13:20

标签: vb.net

任何人都可以使用以下代码吗?

我正在尝试从数据库列中获取数据到datagridview ... 我在这里得到错误“Dim sql_1 As String =”SELECT * FROM item where item_id ='“+ DataGridView_stockout.CurrentCell.Value.ToString()+”'“”

Private Sub DataGridView_stockout_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView_stockout.CellMouseClick
    Dim i As Integer = Stock_checkDataSet1.Tables(0).Rows.Count > 0
    Dim thiscur_stok As New System.Data.SqlClient.SqlConnection("Data Source=MBTHQ\SQLEXPRESS;Initial Catalog=stock_check;Integrated Security=True")
    ' Sql Query 

    Dim sql_1 As String = "SELECT * FROM item where item_id = '" + DataGridView_stockout.CurrentCell.Value.ToString() + "'"
    ' Create Data Adapter
    Dim da_1 As New SqlDataAdapter(sql_1, thiscur_stok)
    ' Fill Dataset and Get Data Table
    da_1.Fill(Stock_checkDataSet1, "item")
    Dim dt_1 As DataTable = Stock_checkDataSet1.Tables("item")
    If i >= DataGridView_stockout.Rows.Count Then
        'MessageBox.Show("Sorry, DataGridView_stockout doesn't any row at index " & i.ToString())
        Exit Sub
    End If
    If 1 >= Stock_checkDataSet1.Tables.Count Then
        'MessageBox.Show("Sorry, Stock_checkDataSet1 doesn't any table at index 1")
        Exit Sub
    End If
    If i >= Stock_checkDataSet1.Tables(1).Rows.Count Then
        'MessageBox.Show("Sorry, Stock_checkDataSet1.Tables(1) doesn't any row at index " & i.ToString())
        Exit Sub
    End If
    If Not Stock_checkDataSet1.Tables(1).Columns.Contains("os") Then
        'MessageBox.Show("Sorry, Stock_checkDataSet1.Tables(1) doesn't any column named 'os'")
        Exit Sub
    End If

    'DataGridView_stockout.Item("cs_stockout", i).Value = Stock_checkDataSet1.Tables(0).Rows(i).Item("os")
    Dim ab As String = Stock_checkDataSet1.Tables(0).Rows(i)(0).ToString()
End Sub

我继续收到错误说“对象引用没有设置为对象的实例” 我不知道我哪里出错了。 真的很感激!!

2 个答案:

答案 0 :(得分:1)

Stock_checkDataSet1.Tables(1)

我认为你需要使用0索引。

答案 1 :(得分:0)

启动调试器并打开异常中断,您将看到发生异常的确切位置。