tags:

views:

66

answers:

2

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

A: 

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

Gerrie Schenck
Well, did tried using debugger, but couldn't get where exactly 'm going wrong.What i actuli want is when i click on curent stock column it should get the stock as per the item id inserted.And for that i wrote the sql asselect * from item where itemid = '" + datagridview.currentcell.value +"'But how can i reference on click on current stock column to run the query to get the value from the database as per the itemid typed on the itemid column.Help really appreciated.
MBTHQ
+1  A: 
Stock_checkDataSet1.Tables(1)

I think you need to use a 0 index.

leppie
Thanks for response. But where should i make the changes.
MBTHQ
@MBTHQ: Use 0 instead of 1.
leppie