Add Record

I'm able to display data from my database with this code but can't add a new record. What am I doing wrong

Dim MyConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db.mdb")

Dim MyComm As OleDb.OleDbCommand

Dim strSQL As String

MyConn.Open()

strSQL = "INSERT INTO [Names] (LastName, FirstName) VALUES ('" & LastNameTextBox.Text & "','" & FirstNameTextBox.Text & "')"

MyComm = New OleDb.OleDbCommand(strSQL, MyConn)

MyComm.ExecuteNonQuery()

MyConn.Close()




Answer this question

Add Record

  • Nianz

    Well lol I figured out what's going on. i'm looking at my database that's in my project folder instead of the database that is in the debug folder. I'm so sorry that I wasted your time trucker and thanks for your help.

  • Docile

    Still Not working. Not getting any errors either.

  • jresnick

    Are you getting an exception at all or is it just not appearing to add the data


  • LetsConvert

    Glad you figured it out!! You didn't waste my time. I like trying to solve puzzles and this was just one more. Sometimes I actually give a correct answer too!!!

    james

    aka:Trucker


  • Ramzee

    You need to write the data back to the database. Something like this, after adding the new row:

    yourConnection.Open()

     yourDataAdaptor.Update(dbDataSet,"Your Table Name")

    yourConnection.Close()

    Where "Your Table Name" is the name of your data table. And  yourConnection being the name you gave to your data connection and  yourDataAdaptor being the name you gave to your Data Adaptor.  

    james

    aka:Trucker

     

     

     


  • Ofer Zadikario

    DbDataSet.Names.Rows.Add(NewOffender) may need to be changed (along with the other stuff I posted) to this:

    dbDataSet.Tables("yourTableName").Rows.Add(newOffender)

     

    Then do the other code I posted.

    james

    aka:Trucker

     

     Edit: Forgot to mention, the YourTableName needs to be in quotes"YourTableName".

     


  • jeremydeanmartin

    Nope no exceptions. It's just not adding the data. I tried it a new way and still not working.

    New Code:

    Dim NewOffender As dbDataSet.NamesRow

    NewOffender = DbDataSet.Names.NewNamesRow

    NewOffender.LastName = LastNameTextBox.Text

    NewOffender.FirstName = FirstNameTextBox.Text

    DbDataSet.Names.Rows.Add(NewOffender)

    Samething no exceptions and no data.



  • Add Record