Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
What am i doing wrong? I can seem to understand this error "Object reference not set to an instance of an object." Pls help me, thanks!

VB
da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB)
        da2.Fill(ds2, "PurchaseDB2")
        For i = 0 To poDG.Rows.Count - 1
            ds2.Tables("PurchaseDB2").Rows(inc).Item("ItemNo") = poDG.Rows.Item(i).Cells(0).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("Description") = poDG.Rows.Item(i).Cells(1).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("Qty") = poDG.Rows.Item(i).Cells(2).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("UnitPrice") = poDG.Rows.Item(i).Cells(3).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("Amount") = poDG.Rows.Item(i).Cells(4).Value.ToString
            Dim cb2 As New OleDbCommandBuilder(da2)
            da2.Update(ds2, "PurchaseDB2")
        Next
Posted
Comments
AnnSJ 15-Sep-11 5:13am    
which line r u getting the error on
Joanne0005 15-Sep-11 5:17am    
in the first line after for...
ds2.Tables("PurchaseDB2").Rows(inc).Item("ItemNo") = poDG.Rows.Item(i).Cells(0).Value.ToString
[no name] 16-Sep-11 14:52pm    
Why don't you use databinding to change the data in dataset through datagrid or datagridview automatically?
Like as datagridView1.DataSource = dataset1;
datagridView1.DataMember = "the name of a datatable in dataset1" ;
Your code is so messy to debug online!

You say this is the line of code that errors:
VB
ds2.Tables("PurchaseDB2").Rows(inc).Item("ItemNo") = poDG.Rows.Item(i).Cells(0).Value.ToString


So you need to figure out which part is not set to an instance, or in other words, which part is null or nothing. You can do this by setting a break point and running the code. Then use QuickWatch. (highlight the part you want to look into, right click, select quickwatch). First make sure that ds2 exists. Then make sure there is really a table inside it named PurchaseDB2. Then make sure that there are rows inside that table. etc.

I noticed that you are trying to set row(inc), but I don't see what/where inc is. Did you mean to just use the variable i here? Or perhaps the DataGridView object that you are setting it equal to doesn't have any rows and it's the .Cells(0) part that is not set to an object?

Also, you may want to consider using a For Each to iterate through tables or grids. It appears you are iterating through a datagridview in which case you could use something like this:
VB
For Each dgvr as DataGridView Row in poDG.Rows
  'Now when you want to reference the DataGridViewRow you can use dgvr.Item(0).Value instead of poDG.Rows(i).Cells(0).Value

Next

or looping through a table:
VB
For Each myRow as DataRow in dtMyDataTable.Rows
   'Code that uses myRow instead of dtMyDataTable.Rows(i).Item(0)

Next


It's just a bit easier to use than setting up those count variables. Hope this helps.
 
Share this answer
 
here is the scenario of the code, i will input data in the datagridview, then if i click save, it will be saved in the database, adding is now working properly. My problem is, searching the database for records, for example i searched the "Description" of a record so all the records i added in database from datagridview should "be" still there in the datagridview. But this is not happening, it will just output the first record in the database, what if i want multiple rows to appear in the datagridview?
 
Share this answer
 
What is your method of accessing the database?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900