Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Problem inserting data to database

  1. #1

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

    Resolved [RESOLVED] [2005] Problem inserting data to database

    Hi!

    I'm trying to add some data from textboxes to my database. I had it working in another application using the same code just the other day. But no, niether the new app or the old is functioning. All I get is my error message, and the truth told, I'm not very good at error searching through my code.

    Any help?

    This is in the top of the main form.

    Code:
    Imports System.Text
    Imports System.Data.SqlClient
    Imports System.Data.OleDb
    
    Public Class StartForm
    
        Dim cn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader
        Dim icount As Integer
        Dim str As String
    And here is the sub that is my trouble.

    Code:
    Private Sub AddMovieBtn_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddMovieBtn.Click
    
            Dim addresponce As Integer
            addresponce = MsgBox(" Do you want to add the following object? " + "Movie Title: " + Title.Text, MsgBoxStyle.YesNo)
            If addresponce = vbYes Then
    
    
                If Title.Text = "" And Actor.Text = "" And Descrip.Text = "" And Length.Text = "" Then
    
    
    
                    MsgBox("You have to fill at least 1 field!")
                Else
    
    
                    Try
    
    
    [B]                    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=movies.mdb;")
                        cn.Open()
                        str = "INSERT INTO moviedata VALUES('" & Title.Text & "' , '" & Length.Text & "' , '" & Actor.Text & "' , '" & Descrip.Text & "' , '" & Rating.SelectedItem & "' , '" & Genre.SelectedItem & "  ' , '" & FileLocation.Text & "')"[/B]
                        'string stores the command and CInt is used to convert number to string
                        cmd = New OleDbCommand(str, cn)
                        icount = cmd.ExecuteNonQuery
                        'MsgBox(icount)
                        'displays number of records inserted
                        MsgBox("The Movie Has Been Added To The Database")
    
                        'Clear all data
                        Title.Text = ""
                        Length.Text = ""
                        Actor.Text = ""
                        Descrip.Text = ""
                        Rating.Text = ""
                        Genre.Text = ""
                        FileLocation.Text = ""
    
                    Catch
                        MsgBox("An error was reported! Please try again!")
                    End Try
                    cn.Close()
    
    
    
    
    
                End If
    
            Else
                If addresponce = vbNo Then
                    Me.Select()
                End If
            End If
            Me.MoviedataTableAdapter.Fill(Me.MoviesDataSet.moviedata)
    
    
        End Sub

    Just one more quick question. When I use ComboBoxes, and I want the value I've choosen in it to go to the database. What should I select? Text, SelectedValue or SelectedItem?

    Thanks for the help!

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

    Re: [2005] Problem inserting data to database

    Quote Originally Posted by maco
    All I get is my error message
    And what error message is that? If the IDE gives you an error message and you want help with the error then always pass the error message on to us.
    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 inserting data to database

    All I get is my own error message, the one that is catched.

    vb Code:
    1. Catch
    2.                     MsgBox("An error was reported! Please try again!")
    3.                 End Try

    And I dunno what to do more, I'm trying to learn but error searching isn't my stron side.

  4. #4
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Problem inserting data to database

    Print the error message to have more information about your error

    vb Code:
    1. catch ex as Exception
    2.      MessageBox.Show(ex.ToString())
    3. End Try

  5. #5

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

    Re: [2005] Problem inserting data to database



    I Guess that means there is something with the number of cells in my database. Does the SQL query count the first cell which is a counter to give the record an ID?

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

    Re: [2005] Problem inserting data to database

    While what you have used is legal syntax when inserting a value to EVERY column in the table, e.g.
    Code:
    INSERT INTO MyTable VALUES (Value1, Value2, Value3)
    it is certainly not legal if you are omitting values for one or more columns. In that case how would the system know what values are for what columns.

    Even if you are specifying a value for every column that is still considered bad practice. You should consider ALWAYS specifying the columns explicitly regardless, e.g.
    Code:
    INSERT INTO MyTable (Column1, Column2, Column3) VALUES (Value1, Value2, Value3)
    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

  7. #7
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Problem inserting data to database

    vb Code:
    1. Private cn as OleDbConnection = New OleDbConnection
    2. Private ds as DataSet = New DataSet()
    3. Private da as OleDbDataAdapter
    4.  
    5.  
    6. Private Sub Form_Load(sender as Object, e as EventArgs)
    7.    'Load the table movieData
    8.  
    9.    dim selectQuery as String = "SELECT ID, Name FROM movieData"
    10.    dim insertQuery as String = "INSERT INTO movieData (ID, Name) VALUES(@ID, @Name)"
    11.  
    12.    dim SelectCmd as OleDbCommand = New OleDbCommand(selectQuery, cn)
    13.    dim InsertCmd as OleDbCommand = New OleDbCommand(insertQuery, cn)
    14.  
    15.    InsertCmd.Parameters.Add("@ID", OleDbType.Integer, 'something', Me.TextBoxID.text)
    16.    InsertCmd.Parameters.Add("@Name", OleDbType.Varchar, 50, Me.TextBoxName.text)
    17.  
    18.    da.SelectCommand = SelectCmd
    19.    da.InsertCommand = InsertCmd
    20.  
    21.    Try
    22.      cn.Open      
    23.      da.Fill(ds, "movieData")
    24.      cn.Close
    25.    Catch ex as Execption
    26.      MessageBox.Show(ex.ToStrong())
    27.    End Try
    28. End Sub
    29.  
    30. Private Sub ButtonAddMovie_Click(sender, e)
    31.    'CorrectInput = Check if the the content of the TextBox are correct
    32.    If CorrectInput Then
    33.      dim result as DialogResult = MessageBox.Show("Add Movie?", MessageBox.vbYesNo)
    34.      If result = MessageBox.vbYes Then
    35.        dim row as ds.Tables("movieData").NewRow()
    36.        row("ID") = Me.TextBoxID.Text
    37.        row("Name") = Me.TextBoxName.Text
    38.        ds.Tables("movieData").Rows.Add(row)
    39.      End If
    40.    End If
    41. End Sub
    42.  
    43. Private Sub buttonUpdateDatabase_Click(sender, e)
    44.   try
    45.     cn.Open()
    46.     ds.Update()
    47.     cn.Close()
    48.     MessageBox.Show("Update successfull")
    49.   Catch ex as Exception
    50.     MessageBox.Show(ex.ToString())
    51.   End Try
    52. End Sub

  8. #8

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

    Re: [2005] Problem inserting data to database

    Thanks guys! I got it working now, defining what columns to insert to worked out fine!

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