Trying to Use INSERT INTO in VB.Net but is not INSERTing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evilbungle
    New Member
    • Apr 2008
    • 26

    Trying to Use INSERT INTO in VB.Net but is not INSERTing

    Hi, I am using VB.Net in Visual Studio 2008 with a connected SQL Server Compact Database but for some reason I can not get a record to insert into my Database from my Windows Form.

    I have used the data connection on the form already to populate a couple of combo boxes but when it comes to commiting the data the sub runs through but there is no additional data in my database.

    my code is as follows

    Code:
        Private Sub cmdAddQuote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddQuote.Click
            Dim cnData As New SqlCeConnection 
            Dim cdData As SqlCeCommand
            Dim strsql As String
            strsql = "INSERT INTO HS_QuoteDetail (nbrRecordNbr, nbrItemNbr, nbrItemCount, CurCost) VALUES ('" & Me.txtJobID.Text
            strsql = strsql & "', '" & Me.ItemIDNbr.Text & "', '" & Me.nbrUnits.Text & "', '" & Me.curSubTotal.Text & "');"
            cnData.ConnectionString = "data source = Database1.sdf; Persist Security Info = False"
            If cnData.State = ConnectionState.Closed Then
                cnData.Open()
            End If
            cdData = New SqlCeCommand(strsql, cnData)
            'cdData.ExecuteNonQuery()
            cdData.ExecuteNonQuery()
            MsgBox("Run")
            Me.nbrUnits.Focus.Equals(True)
            cnData.Close()
        End Sub
    Teh table only contains these 4 fields and another field which is an automatically generated number called nbrQuoteID

    All of the fields are number fields but I have tried sending them as text to see if it made a difference (Threw up an error) but it didn't.
  • marcellus7
    New Member
    • Oct 2008
    • 33

    #2
    Why not use a TableAdapter and use the auto-generated insert query?

    Comment

    • evilbungle
      New Member
      • Apr 2008
      • 26

      #3
      Ok thanks, I will try and see if I can find anything about that.

      I have actually managed to get the data onto the table and to populate a listview to show it is there but when I go back to view the table the records are not there.

      Comment

      • evilbungle
        New Member
        • Apr 2008
        • 26

        #4
        Sorry I must be being really thick but I can not for the life of me work out how to INSERT the record, it is asking for five fields yet the first field is an AutoNumber field so I am not sure how to Insert this record.

        Code:
        Dim taQuote As New HS_fieldDatasetTableAdapters.HS_QuoteDataTableAdapter
                taQuote.Insert(, Me.txtJobID.Text, Me.ItemIDNbr.Text, Me.nbrUnits.Text, Me.curSubTotal.Text)
        EDIT:

        Sorry you will not believe how stupid I have been, I have two tables with similar names and fields and dragged the wrong one.
        Last edited by evilbungle; Sep 15 '10, 08:08 AM. Reason: Stupidity

        Comment

        • evilbungle
          New Member
          • Apr 2008
          • 26

          #5
          Sorry back again I have run through the Sub with the Table Adapter and again when I go back to view table data it does not show the updated data again, although the Autonumber keeps going up.

          Comment

          • evilbungle
            New Member
            • Apr 2008
            • 26

            #6
            Solved it, It is due to the debug and production Databases being different, A well known issue apparently but not to me. I simply have to copy the database back from Debug to the main folder and teh data is there.

            Comment

            Working...