locked
Object reference not set to an instance of an object RRS feed

  • Question

  • Hey guys!

    please help to get my vb.net code working. As i have posted this or similar question before, but failed to get the correct code for it.

    Can you please help me out of this, as its my uni project work,

    Well, M getting n error saying "Object reference not set to an instance of an object" , I tired all  the possibilites but couldnt make that code work.

    Basically, what 'm trying to do is to get the current stock from the database table (Item) where item_id ='@itemid'

    Below is my full code which 'm using it on CellMouseClick event of datagridview (in vb.net):

    Private

     

    Sub DataGridView_stockout_CellMouseClick(ByVal sender AsObject,ByVal eAs System.Windows.Forms.DataGridViewCellMouseEventArgs)Handles DataGridView_stockout.CellMouseClick

     

    Dim iAsInteger = Stock_checkDataSet1.Tables(0).Rows.Count >= 1

     

    Dim thiscur_stokAsNew System.Data.SqlClient.SqlConnection("Data Source=MBTHQ\SQLEXPRESS;Initial Catalog=stock_check;Integrated Security=True")

     

    ' Sql Query

     

    'Error line is below **** ("'Object reference not set to an instance of an object")

     

    Dim sql_1AsString ="SELECT * FROM item where item_id = '" + DataGridView_stockout.CurrentCell.Value.ToString() +"'"

     

    'Error line is Above **** ("'Object reference not set to an instance of an object")

     

    ' Create Data Adapter

     

    Dim da_1AsNew SqlDataAdapter(sql_1, thiscur_stok)

     

    ' Fill Dataset and Get Data Table

    da_1.Fill(Stock_checkDataSet1,

    "item")

     

    Dim dt_1As DataTable = Stock_checkDataSet1.Tables("item")

     

    If i >= DataGridView_stockout.Rows.CountThen

     

    'MessageBox.Show("Sorry, DataGridView_stockout doesn't any row at index " & i.ToString())

     

    ExitSub

     

    EndIf

     

    If 1 >= Stock_checkDataSet1.Tables.CountThen

     

    'MessageBox.Show("Sorry, Stock_checkDataSet1 doesn't any table at index 1")

     

    ExitSub

     

    EndIf

     

    If i >= Stock_checkDataSet1.Tables(1).Rows.CountThen

     

    'MessageBox.Show("Sorry, Stock_checkDataSet1.Tables(1) doesn't any row at index " & i.ToString())

     

    ExitSub

     

    EndIf

     

    IfNot Stock_checkDataSet1.Tables(1).Columns.Contains("os")Then

     

    'MessageBox.Show("Sorry, Stock_checkDataSet1.Tables(1) doesn't any column named 'os'")

     

    ExitSub

     

    EndIf

     

    'DataGridView_stockout.Item("cs_stockout", i).Value = Stock_checkDataSet1.Tables(0).Rows(i).Item("os")

     

    Dim abAsString = Stock_checkDataSet1.Tables(1).Rows(i)(0).ToString()

     

    EndSub

    Help much appreciated!!

    Thanks!!

    Ps: The error comes up while clicking on the blank cell of curentstock, infact it shld retrieve the value as per the inserted itemid.

     

    Friday, April 16, 2010 6:53 PM

Answers

  • You are accessing a table at index 1 that your query doesn't appear to populate.  Indexes are 0 based, "item" is at index 0.

     

    Also I would suggest you do some research about sanitizing sql queries using parameterization.  Queries should never be written using string concatenation.

    Friday, April 16, 2010 7:28 PM

All replies

  • You are accessing a table at index 1 that your query doesn't appear to populate.  Indexes are 0 based, "item" is at index 0.

     

    Also I would suggest you do some research about sanitizing sql queries using parameterization.  Queries should never be written using string concatenation.

    Friday, April 16, 2010 7:28 PM
  • Thanks Man!

    realli appreciate for leting me know where i went wrong.

    I jst got one problem its now i have the query for the cureent stock where item id = item id

    sql query is written as bellow:

    select * from itemid where itemid = '" + ??? (Is there anyway i can link the itemid of the datagirdview column with the sql database to get the current stock of the inserted item id) + "'

    I knw how the prameterzied query for the textbox n cbo or lstbox n aoll..bt 'm struggling to do for the datagridview colums.

    Appreciate if you can help me..

    Thanks

    Friday, April 16, 2010 7:54 PM