Community discussion forum

Problems with updating databases from a dataset in visual basic .net

  • 5 months ago

    I am writitng a program that uses a database as the backend and visual basic .net for the gui. I have created a table in the ms sql express that a user can pick from and record to. I can bind the information from the table to a combobox so the users can pick it. The problem is that any new data that is input into the dataset is not being saved back to the origional table in the database.

    This is my code so far:

    Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\StudentODPLogBook.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True") Dim cmd As New SqlCommand

        'insert SQL command'
        cmd.CommandText = "Insert Into OperationsTable (OperationName,OperationId) Values (@OperationName,5)"
    
        'Takes the text box data as the parameter to be stored in the dataset'
        cmd.Parameters.AddWithValue("@OperationName", TextBox4.Text)
    
        'Open connection ,execute sql command and close connection'
    
        cn.Open()
        'cmdBuilder = New SqlCommandBuilder(SqlDataAdapter1)
        cmd.Connection = cn
    
        cmd.ExecuteNonQuery()
        SqlDataAdapter1.Update(OperationsDS1.OperationsTable)
    
        cn.Close()
        'if problem with connection on insertion exception caught a relayed to user
    
        MsgBox("Inserted " + TextBox4.Text) ' shows succesful insertion of data into dataset'
    
        'refils adapter with new data from dataset'
        SqlDataAdapter1.Fill(OperationsDS1)
    

    Iam using the SqlDataAdapter.update method to try and update the database, but it does not seem to be working.

    Can anyone please help or point me in the right direction.

    thanks

    Post was edited on 06/06/2009 14:44:43 Report abuse

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback