inserting a record - Error: Object reference not set to and instance of an object

rvermeulen

Member
Joined
Apr 2, 2003
Messages
17
Location
New Jersey
I am getting an error when I attempt to insert a record. Here is my code
Code:
insert
sql = "insert into EMPLOYEES values (@UserID," + _
txtFirstName.Text + "," + txtLastName.Text + "," + _
txtAddress.Text + "," + txtCity.Text + "," + txtState.Text + "," _
+ txtZipCode.Text + ",#" + txtHireDate.Text + "#," + txtTitle.Text + ")"
Dim func As New CommonFunctions()
Dim cmdIns = New OleDb.OleDbCommand(sql, conn)
Dim parm1 = New OleDb.OleDbParameter("@UserID", OleDbType.Integer)
parm1.value = func.getNextID("EMPLOYEES")
cmdIns.parameters.add(parm1)
conn.Open()
cmdIns.executenonquery()

conn.Close()
cmdIns = Nothing
It is failing on the line attempting to do the ExecuteNonQuery(). I
noticed that the OleDbCommand object did not recognize the
ExecuteNonQuery method when I typed it. Im assuming that
might have something to do with the problem.


I am a newbie at this and have a question about how Im going
about doing this. I have created a dataset that is bound to a
datagrid. The grid populates fine. I understand, that I should be
able to update/delete those records. However, if I want to insert
a new record into the datagrid, is this the correct way to do it ?
Im going to a new page, allowing the user to enter the
information into a form, and then I have the above code
triggering. Am I doing this correctly?

Does anyone have any thoughts?
I appreciate any advice
Rick
 
Last edited by a moderator:
try it this way:

Dim cmdIns = New OleDb.OleDbCommand()

cmdIns = New OleDb.OleDbCommand(sql, conn)

I sometimes get error like this and find splitting the lines into two works
 
Use "As", the same as you did for "func". Also, since youre having this problem, you might want to turn Option Strict On, which would have caught this.

Code:
Dim cmdIns As New OleDb.OleDbCommand(sql, conn)
Dim parm1 As New OleDb.OleDbParameter("@UserID", OleDbType.Integer)

hog, hopefully you mistyped and meant something more like this:
Code:
Dim cmdIns = OleDb.OleDbCommand
cmdIns = New OleDb.OleDbCommand(sql, conn)

The way youve got it right now actually creates two instances, one on the first line, which immediately gets thrown away with the second line. Not good.
 
Tim,
Thanks for your help. That was the ticket.

As I mentioned, I have a dataset bound to the data grid. Ive included a Button column that has the Edit, Update, and Cancel linked buttons on the grid. What do I have to do to be able to edit the record. When I click the Edit link button, nothing is happening. Im sorry if this is a stupid question, but Im missing the idea here.

From what I read, I have to program the Grid_Edit event. But what am I looking to do? What I would like to do is open the same form that they insert new users to open with the information for the specific row they have clicked the Edit link button. I have the ID column in the grid hidden, and Id like to pass that ID to the response.redirect in the Grid_Edit event. Can I do that?

Thanks again for your help.
Rick
 
Back
Top