Hi,

I'd like to get the value of a column from a datagrid and insert that to a table. I get an error "Object reference not set to an instance of an object."

Here's my code:

Imports System.Data.SqlClient
Imports System.Data
Public Class Class1

Public Shared Sub insertrecord(ByVal query As String)
        con.Open()
        com.Connection = con
        com.CommandText = query
        com.ExecuteNonQuery()
        con.Close()

End Sub

Public Shared Sub readrecord(ByVal query As String)
        con.Open()
        com.CommandText = query
        com.Connection = con
        dr = com.ExecuteReader

    End Sub

End Class

Public Class FormRentals

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

Dim indx As Integer = DataGridView1.Rows.Add()
                DataGridView1.Rows(indx).Cells(0).Value = (indx + 1).ToString
                DataGridView1.Rows(indx).Cells(1).Value = txtboxName.Text
                DataGridView1.Rows(indx).Cells(2).Value = FormatNumber(txtboxamt.Text,2).ToString

End Sub

Private Sub btnaddtocart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddtocart.Click

'This generates a primary key called ID that is set to be identity(1,1)
Class1.insertrecord("Insert into myTable(Name) values('" + txtboxName.Text + "')")

For x As Integer = 0 To (DataGridView1.Rows.Count() - 1)
     Class1.readrecord("Select * from myTable where Name='" + DataGridView1.Rows(x).Cells(1).Value.ToString + "'")
     If Class1.dr.Read() then
       Dim myID as Integer = Class1.dr("ID")
       Class1.con.Close
       Class1.insertrecord("Insert into amtdue(nameID,name,amount) values('" + myID.ToString + "','" + DataGridView1.Rows(x).Cells(1).Value.ToString + "','" + CType(DataGridView1.Rows(x).Cells(2).Value,Double).ToString + "')")
     End if
     Class1.con.Close

End Sub

End Class

The thing is -- when I look at table amtdue, everything is actually saved there. I just don't understand why I'm getting the error, and so I can't proceed with what I have to do after clicking the btnaddtocart button.

Would really appreciate any help. Thanks much in advance!

Recommended Answers

All 8 Replies

The error means that one of your objects has no reference. You need to make sure that every variable and object has been declared or referenced.:)

Hi, thanks for replying! But is it possible to have the data successfully saved to the table if there's an error like this?

The error is pointing to this line --

Class1.readrecord("Select * from myTable where Name='" + DataGridView1.Rows(x).Cells(1).Value.ToString + "

'")

but I see that it's reading myID fine because this line

Class1.insertrecord("Insert into amtdue(nameID,name,amount) values('" + myID.ToString + "','" + DataGridView1.Rows(x).Cells(1).Value.ToString + "','" + CType(DataGridView1.Rows(x).Cells(2).Value,Double).ToString + "')")

has been successfully inserted into the table.

I don't understand :=)

Class1.readrecord("Select * from myTable where Name='" + DataGridView1.Rows(x).Cells(1).Value.ToString + "

This part should not because you are missing an operator -

Class1.readrecord("Select * from myTable where Name='" + DataGridView1.Rows(x).Cells(1).Value.ToString + "'" 

' Added' and " at the end.

Oops sorry, it wasn't included within the CODE tags. Pls look at my code above, the operators trailed behind

Hi,

Do you think it's because there's something wrong with how I programmatically created my datagrid? I did it this way:

Private Sub FormNewReservation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

DataGrid1.Columns.Add("Name", "Name")
DataGrid1.Columns.Add("AmtDue", "Amount Due")

Anyone please? Someone?

Sorry, wrong post above. Here's the correct one:

Hi,

Do you think it's because there's something wrong with how I programmatically created my datagrid? I did it this way:

Private Sub FormNewReservation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

DataGridView1.Columns.Add("Name", "Name")
DataGridView1.Columns.Add("AmtDue", "Amount Due")

Anyone please? Someone?

Hi,

Just want to post here that I finally solved my problem. Instead of using this code,

For x As Integer = 0 To (DataGridView1.Rows.Count() - 1)

I needed to have these lines:

For Each row as DataGridViewRow In DataGridView1.Rows
                    If Not row.IsNewRow Then

Whew. I'm just so relieved.

commented: Well done on solving this yourself. +6

I'm sorry I could not attend to your final solution, was up to my neck in work. Well done on solving this yourself though, this is what makes you a better developer and ensure that you understand the code as well.:) Some kudos for solving this on your own.;)

Please mark this as solved, found at the bottom of this page, thanks.

Happy coding.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.