Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey guys can you tell me what's wrong here ?

I'm getting the error:

Object reference not set to an instance of an object

on line 8

 

1:       Dim Conn As New OleDbConnection()
2:       Dim Ada As New OleDbDataAdapter()
3:       Dim St As New OleDbCommand()
4:       Dim DS As DataSet

5:        Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\students.mdb;Persist Security Info=False"
6:        Conn.Open()
7:        St.CommandText = "SELECT *FROM Staff"

8:       Ada.SelectCommand.Connection = Conn
9:       Ada.SelectCommand = St
10:       Ada.Fill(DS, "Staff")

Posted

you can even youe something like this

 

st = conn.CreateCommand

st.CommandText = "Sql Select Statement"

Ada.SelectCommand = st

 

ada.fill( ds , "Table Name")

 

' if you want to update /insert/delete in the same table then add

Dim cb as oledb.Commandbuilder = new _ oledb.Commandbuilderada)

 

however check the commandbuilder sysntax. i mya hav made some error. not sure..

Posted

I am also getting the same error when I attempt to insert a record. Here is my 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. I'm assuming that might have something to do with the problem.

 

Does anyone have any thoughts?

Thanks in advance.

Rick

  • *Experts*
Posted

Well the first code you posted shouldn't even compile since you never create DS (by setting it to a New DataSet()).

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

In C# I get "Use of unnasigned local variable DS" (I converted his code, but it was mostly the same). I'm not sure if VB.NET checks for this or not...?

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • Leaders
Posted
It doesn't, which is likely why 1 in every 15 or so posts have an error message "object reference not set to an instance":) Strange they made C# so smart and not VB.
--tim

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...