otherside Posted April 4, 2003 Posted April 4, 2003 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") Quote
Leaders quwiltw Posted April 4, 2003 Leaders Posted April 4, 2003 Reverse lines 8 and 9. The Ada.SelectCommand doesn't exist because you don't set it til the next line. Quote --tim
hemenkap Posted April 4, 2003 Posted April 4, 2003 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.. Quote
rvermeulen Posted April 4, 2003 Posted April 4, 2003 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 Quote
*Experts* Nerseus Posted April 4, 2003 *Experts* Posted April 4, 2003 Well the first code you posted shouldn't even compile since you never create DS (by setting it to a New DataSet()). -Nerseus Quote "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 quwiltw Posted April 4, 2003 Leaders Posted April 4, 2003 that'd likely be his next run-time error:), he probably wouldn't have trouble compiling though. Quote --tim
*Experts* Nerseus Posted April 4, 2003 *Experts* Posted April 4, 2003 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 Quote "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 quwiltw Posted April 5, 2003 Leaders Posted April 5, 2003 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. Quote --tim
otherside Posted April 8, 2003 Author Posted April 8, 2003 Thank you guys, actually the first reply did the job, but for some reason i couldn't post a reply. Sorry Thanks again Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.