Results 1 to 5 of 5

Thread: [2005] Problem with SQL Update / Delete query

  1. #1

    Thread Starter
    Junior Member maco's Avatar
    Join Date
    Aug 2006
    Location
    Linköping, Sweden
    Posts
    20

    [2005] Problem with SQL Update / Delete query

    Hi!

    I'm using the following code to update my table, it reports no error but the data isnt being updated. I have no clue what I'm doing wrong here so all suggestions are in place.

    vb Code:
    1. 'Open connection
    2.             Dim DBPath As String
    3.             DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "/movies.mdb"
    4.             cn = New OleDbConnection(DBPath)
    5.             cn.Open()
    6.  
    7.  
    8.             str = "UPDATE moviedata SET Length = '" & Length.Text & "', Actors = '" & Actor.Text & "', Description = '" & Descrip.Text & "', Rating = '" & Rating.SelectedItem & "', Genre = '" & Genre.SelectedItem & "', Location = '" & FileLocation.Text & "' WHERE Title='" & Title.Text & "';"
    9.  
    10.             cn.Close()
    11.  
    12.             MsgBox("OK")
    13.  
    14.         Catch ex As Exception
    15.             MessageBox.Show(ex.ToString())
    16.  
    17.         End Try
    Last edited by maco; Apr 22nd, 2007 at 06:14 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Problem with SQL Update query

    All you've done is open a connection and create a string. That's all. You have not sent any data over that connection in either direction. Take a look at the OleDbCommand class and its ExecuteNonQuery method. That's how you execute the SQL code you have created.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member maco's Avatar
    Join Date
    Aug 2006
    Location
    Linköping, Sweden
    Posts
    20

    Re: [2005] Problem with SQL Update query

    Ok, thanks, just my bad, forgot to add this:

    vb Code:
    1. cmd = New OleDbCommand(str, cn)
    2.                 icount = cmd.ExecuteNonQuery

    The UPDATE command is working now. But I have the same problem with the DELETE command, it reports no error, but the command goes through, but the record still remains?

    Code:

    vb Code:
    1. Try
    2.  
    3.                 'Open connection
    4.                 Dim DBPath As String
    5.                 DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "/movies.mdb"
    6.                 cn = New OleDbConnection(DBPath)
    7.                 cn.Open()
    8.  
    9.  
    10.                 str = "DELETE FROM moviedata WHERE Title='" & SelectMovieMenu.SelectedIndex & "';"
    11.                 cmd = New OleDbCommand(str, cn)
    12.                 icount = cmd.ExecuteNonQuery
    13.  
    14.  
    15.                 cn.Close()
    16.  
    17.                 'IO.File.Delete(Application.StartupPath + "/images/" + Title.Text)
    18.                 Me.MoviedataTableAdapter.Fill(Me.MoviesDataSet.moviedata)
    19.                 MsgBox("The movie has been deleted!")
    20.  
    21.             Catch ex As Exception
    22.                 MessageBox.Show(ex.ToString())
    23.  
    24.             End Try

  4. #4

    Thread Starter
    Junior Member maco's Avatar
    Join Date
    Aug 2006
    Location
    Linköping, Sweden
    Posts
    20

    Re: [2005] Problem with SQL Update / Delete query

    Can anyone tell me a solution for this problem?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Problem with SQL Update / Delete query

    vb Code:
    1. str = "DELETE FROM moviedata WHERE Title='" & SelectMovieMenu.SelectedIndex & "';"
    SelectedIndex is a number. I doubt the name of your movie is a number.

    If something doesn't work the way you expect you need to debug. That does NOT mean just look at your code and see if you can see something that doesn't look right. Just like a mechanic would start a car's engine and watch it run, you need to start your app and watch it run. You should place a break point at the top of that code and then step through it line by line, testing the values of variables at each step. If you'd done that you'd have immediately seen that the SQL code you were executing was not what you intended. Use F9 to set a break point and F10 and F11 to step through your code. I suggest you go to the MSDM library and read about debugging too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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