Results 1 to 13 of 13

Thread: [2008] INSERT sql runs fine but no data is entered into the DB

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    18

    Exclamation [2008] INSERT sql runs fine but no data is entered into the DB

    HI

    I am trying to write data to an access table from vb.net. The code all seems to run fine but when i check the db no new data has been entered!

    the code i have got is:


    Code:
    Dim itemcount As Integer
            Dim MyCn As OleDbConnection
            Dim sql As String
            'Dim Value As String
            Dim Command As OleDbCommand
            Try
    
                con.Open()
                MyCn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\BillAnalyst\ReferenceData.mdb")
                For Each ListView In lstErrors.Items
                    itemcount = itemcount + 1
    
                    sql = "INSERT INTO CAllRates (CallRates.Destination) VALUE (" & ListView & ")"
                    Command = New OleDbCommand(sql, MyCn)
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message & " - " & ex.Source)
                con.Close()
            End Try
    
            lblcount.Text = itemcount
    Dim itemcount As Integer
            Dim MyCn As OleDbConnection
            Dim sql As String
            'Dim Value As String
            Dim Command As OleDbCommand
            Try
    
                con.Open()
                MyCn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\BillAnalyst\ReferenceData.mdb")
                For Each ListView In lstErrors.Items
                    itemcount = itemcount + 1
    
                    sql = "INSERT INTO CAllRates (CallRates.Destination) VALUE (" & ListView & ")"
                    Command = New OleDbCommand(sql, MyCn)
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message & " - " & ex.Source)
                con.Close()
            End Try
    
            lblcount.Text = itemcount
    I wondered if anyone had ideas what i was doing wrong

    many thanks

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    You're not opening your connection (con is not the connection you are using, MyCn is).

    You're not executing your command.

    You're not closing your connection.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    Also, I believe it should be VALUES, not VALUE and, if it's a string, you'll need single quotes.
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    18

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    my bad - i changed the conenction string just in case it was that and forgot to change the other bit . but corrected it and still the same result. it appears to run fine without error but adds no records to the db. my adjusted code is:

    Code:
     Dim itemcount As Integer
            Dim MyCn As OleDbConnection
            Dim sql As String
            'Dim Value As String
            Dim Command As OleDbCommand
            Try
    
    
                MyCn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\BillAnalyst\ReferenceData.mdb")
                MyCn.Open()
                For Each ListView In lstErrors.Items
                    itemcount = itemcount + 1
    
                    sql = "INSERT INTO CallRates VALUES ('" & ListView & "')"
                    Command = New OleDbCommand(sql, MyCn)
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message & " - " & ex.Source)
                MyCn.Close()
            End Try
    
            lblcount.Text = itemcount

  5. #5
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    Change:

    Command = New OleDbCommand(sql, MyCn)

    to:

    Command = New OleDbCommand(sql, MyCn)
    Command.ExecuteNonQuery
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    18

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    that seems to have sorted the connection thing - many thanks

    but i am now getting errors with the location of the db, even though that path works fine on another connection. will updated with the error now

  7. #7
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    Quote Originally Posted by ninjaimp
    it appears to run fine without error but adds no records to the db.
    Perhaps this:
    http://msdn2.microsoft.com/en-us/library/ms246989.aspx

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    You're still not closing your connection. Get that MyCn.Close() OUT of the Catch block! If you're not closing your connections properly, you will have problems later when they get used up!
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    18

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    it is now connecting, or sort of but i get the error:


    could not find file: c:\documents and settings\Simon\My Documents\Visual Studio 2008\Projects\BillAnalyst\BillAnalyst\bin\Debug\Callrates.mdb

    but the connection string points to:

    C:\BillAnalyst\ReferenceData.mdb


    cant understand why the path is different and it cant see it where im pointing it to

  10. #10
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    Where in the code does it give you this error? Somewhere, the code is trying to open a connection to "Callrates.mdb".

    I'm suspecting you're using a DataConnection through the GUI that is pointed to this database. What does your "Server Explorer" show?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    18

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    it gives the error on the very first cycle as it hits the Command.ExecuteNonQuery().


    the connection string value for the con connection reads: "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\BillAnalyst\ReferenceData.mdb"

    so that appears correct

    Im not sure what my Server Explorer is?

    many thanks for your help

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    18

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    UPDATE!

    It appeared to be an error with the SQL. Changed it to:

    INSERT INTO CallRates (Destination) VALUES ('" & ListView & "')


    and it all worked fine.

    many thanks for everyones help

    Regards

  13. #13
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] INSERT sql runs fine but no data is entered into the DB

    Before you go... it's good that you got it to work, but here's a better way of doing it. Using a Parameter is not only faster, but doesn't mess up your SQL if someone puts a ' or a \ in the name you're inserting.

    It's smaller code too.

    Code:
            Dim MyCn As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\BillAnalyst\ReferenceData.mdb")
            Dim sql As String = "INSERT INTO CallRates (Destination) VALUES (@CR)"
            Dim Command As OleDbCommand = New OleDbCommand(sql, MyCn)
            Try
                Command.Parameters.Add("CR", OleDbType.VarChar, 50)
    
                MyCn.Open()
                For Each li As ListViewItem In lstErrors.Items
                    Command.Parameters("CR").Value = li.Text
                    Command.ExecuteNonQuery()
                Next
                MyCn.Close()
    
                lblCount.Text = lstErrors.Items.Count.ToString
            Catch ex As Exception
                MessageBox.Show(ex.Message & " - " & ex.Source)
                If Not MyCn.State.Equals(ConnectionState.Closed) Then MyCn.Close()
            End Try
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width