Object reference not set to an instance of an object

Posted by MBTHQ on Stack Overflow See other posts from Stack Overflow or by MBTHQ
Published on 2010-04-14T07:13:19Z Indexed on 2010/04/14 7:23 UTC
Read the original article Hit count: 271

Filed under:

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!!

© Stack Overflow or respective owner

Related posts about vb.net