Results 1 to 11 of 11

Thread: [RESOLVED]Insert Command not updating

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    [RESOLVED]Insert Command not updating

    Hi,

    I am doing my first Web.Application and I have connected to a database and from the webform the user will input his details to be stored in the first available row and update it. I have this procedure


    VB Code:
    1. Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
    2.         MyConn.Open()
    3.        Dim sql As String = "Select * FROM ApplicantsDetails"
    4.         Dim mydataadapter As New OleDbDataAdapter
    5.         mydataadapter = New OleDbDataAdapter(Sql, MyConn)
    6.         Dim mycmdbuilder As New OleDbCommandBuilder(mydataadapter)
    7.         Dim myDatatable As New DataTable
    8.         mydataadapter.Fill(myDatatable)
    9.         Dim mynewrow As DataRow = myDatatable.NewRow
    10.         mynewrow(0) = TxtID.Text
    11.         mynewrow(1) = TxtName.Text
    12.         mynewrow(2) = Txtsurname.Text
    13.         mynewrow(6) = dropdschool.SelectedValue.ToString
    14.         myDatatable.Rows.Add(mynewrow)
    15.         mydataadapter.Update(myDatatable)
    16.         MyConn.Close()
    17.     End Sub

    and it is giving me an error:

    HTML Code:
    Syntax error in INSERT INTO statement. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
    
    Source Error: 
    
    
    Line 97:         mynewrow(6) = dropdschool.SelectedValue.ToString
    Line 98:         myDatatable.Rows.Add(mynewrow)
    Line 99:         mydataadapter.Update(myDatatable)
    Line 100:        MyConn.Close()
    Line 101:    End Sub
    Can someone please tell me where I am missing... thanks
    Last edited by angelica; Oct 6th, 2005 at 04:08 AM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: Insert Command not updating

    hi Guys,

    I've been going into some reader and changed to my code below which uses parameter. I get no syntax error yet I am still unable to update the table. Can someone pls help, much appreciated


    VB Code:
    1. Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
    2.         Dim sql As String = "Select * FROM ApplicantsDetails"
    3.         Dim mydataadapter As New OleDbDataAdapter
    4.         mydataadapter = New OleDbDataAdapter(Sql, MyConn)
    5.         Dim mycmdbuilder As New OleDbCommandBuilder(mydataadapter)
    6.         Dim myDatatable As New DataTable
    7.  
    8.         Dim reader As OleDbDataReader
    9.         Dim parmID As New OleDbParameter("@ID Number", OleDbType.VarChar)
    10.         Dim parmName As New OleDbParameter("@FirstName", OleDbType.VarChar)
    11.         Dim parmSurname As New OleDbParameter("@SecondName", OleDbType.VarChar)
    12.  
    13.         cmd = New OleDbCommand(sql, MyConn)
    14.         With cmd
    15.             .Parameters.Add("@ID Number", TxtID.Text)
    16.             .Parameters.Add("@FirstName", TxtName.Text)
    17.             .Parameters.Add("@LastName", Txtsurname.Text)
    18.  
    19.             MyConn.Open()
    20.             .ExecuteNonQuery()
    21.             mydataadapter.Fill(myDatatable)
    22.             Dim mynewrow As DataRow = myDatatable.NewRow
    23.             myDatatable.Rows.Add(mynewrow)
    24.  
    25.             mydataadapter.Update(myDatatable)
    26.         End With
    27.         MyConn.Close()
    28.     End Sub

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert Command not updating

    It appears to me that your datatable and dataadapter remain in local scope of your subs. The best thing for you to do is to execute an insert statement directly. There is no point in loading all those records and then adding a new row.

    Check out the example here:
    http://msdn.microsoft.com/library/de...querytopic.asp

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert Command not updating

    Sure.

    VB Code:
    1. Dim myCommand As New SqlCommand("INSERT INTO ApplicantDetails([ID Number],FirstName, LastName) VALUES('" & Textbox1.text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')" , myConn)
    2.     myCommand.Connection.Open()
    3.     myCommand.ExecuteNonQuery()
    4.     myConnection.Close()

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: Insert Command not updating

    Hi Mendhak, why does ID Number have square brackets??

    ([ID Number],FirstName, LastName)

    Mendhak, can you edit my previous code with the insert cos Ive added the bit you sent me but although I get no error my table didnt get the new row. thanks
    Last edited by angelica; Oct 5th, 2005 at 07:32 AM.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Insert Command not updating

    Quote Originally Posted by angelica
    Hi Mendhak, why does ID Number have square brackets??
    Because it has a space in it. If the field name was ID_Number, there would be no need for the brackets because there would be no space.

    Brackets are also required if you use a reserved word as a field name; Example: Date Time

    Both of those would have to be in brackets because they are reserved words.

    You should never used reserved words as field names, and I avoid using spaces in field names just because I hate having to including the brackets in queries.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert Command not updating

    Me too, I hate square brackets. They're more disrupting than beneficial.

    I'm a little reluctant to be writing this for you because I thought the code was pretty self-explanatory and you should learn to do this yourself. Nevertheless, just this once.

    VB Code:
    1. Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
    2.         Dim sql As String = "Select * FROM ApplicantsDetails"
    3.  
    4.          Dim myCommand As New SqlCommand("INSERT INTO ApplicantDetails([ID Number],FirstName, LastName) VALUES(" & TxtID.text & ",'" & TxtName.Text & "','" & Txtsurname.Text & "')" , myConn)
    5.  
    6.     myCommand.Connection.Open()
    7.     myCommand.ExecuteNonQuery()
    8.     myConnection.Close()
    9.  
    10.     End Sub

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: Insert Command not updating

    Hi mendhak,

    Thanks for your help, but I had coded that, see I try hard... but I'm still getting the error that the on:

    VB Code:
    1. myCommand.ExecuteNonQuery()

    error says it :

    Code:
    Could not find output table 'ApplicantDetails'
    Here's my latest code. any clues where Im wrong pls.

    VB Code:
    1. Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
    2.         Dim sql As String = "Select * FROM ApplicantsDetails"
    3.         Dim mydataadapter As New OleDbDataAdapter
    4.         mydataadapter = New OleDbDataAdapter(Sql, MyConn)
    5.         Dim myCommand As New OleDbCommand("INSERT INTO ApplicantDetails([IDNumber],FirstName, LastName) VALUES(" & TxtID.Text & ",'" & TxtName.Text & "','" & Txtsurname.Text & "')", MyConn)
    6.  
    7.         myCommand.Connection.Open()
    8.         myCommand.ExecuteNonQuery()
    9.         MyConn.Close()
    10.  
    11.     End Sub

  9. #9
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Insert Command not updating

    Dim myCommand As New OleDbCommand("INSERT INTO ApplicantDetails...

    You put down ApplicantDetails... Doesn't it need to be ApplicantsDetails? You missed the "s" on Applicant.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: Insert Command not updating

    hi MetalKid,

    Uh! It works fine now. Slip of the types and Ive been haking my brain. Thanks for your help. Cheers

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert Command not updating

    Now that it's working, add [Resolved] to the thread title... and you know the drill

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