MBTHQ
MBTHQ

Reputation: 1

Object reference not set to an instance of an object

Can anyone help with the following code?

I'm trying to get data from the database colum to the datagridview... I'm getting error over here "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

I keep on getting the error saying "Object reference not set to an instance of an object" I dont know where I'm going wrong. Help really appreciated!!

Upvotes: 0

Views: 1142

Answers (2)

leppie
leppie

Reputation: 117220

Stock_checkDataSet1.Tables(1)

I think you need to use a 0 index.

Upvotes: 1

Gerrie Schenck
Gerrie Schenck

Reputation: 22368

Start the debugger and turn on break on exception, you'll see the exact point where the exception occurs then.

Upvotes: 0

Related Questions