DadaAdapter Error

otherside

Well-known member
Joined
Mar 16, 2003
Messages
127
Location
UK - Greece
Hey guys can you tell me whats wrong here ?
Im getting the error:
Object reference not set to an instance of an object
on line 8

Code:
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")
 
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..
 
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. Im assuming that might have something to do with the problem.

Does anyone have any thoughts?
Thanks in advance.
Rick
 
Well the first code you posted shouldnt even compile since you never create DS (by setting it to a New DataSet()).

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

-nerseus
 
It doesnt, 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.
 
Back
Top