Adding Record to Database (lost)

RD Frame

New member
Joined
Dec 10, 2005
Messages
2
Programming Experience
5-10
Hey guys,

I'm fairly new to VB.NET, but not development, and believe it or not, I've spent the past two days trying to get nothing more than a simple INSERT to work. I've already posted on a couple other forums, but never got the problem solved. Below is my entire Sub Main(), and any advice to what I'm doing wrong would be really, really appreciated right now. ;)

I manually inserted one record into the 'accounts' table, and it always shows up just fine, and stays in the table. When I run the following code, the extra two records are successfully inserted into the database and show up in the combo box as needed, but when I exit the application, those two records always disappear. Why aren't the records being saved / updated / commited?

Any help you could offer would be great. BTW... I know the code is messy, but I'm just trying to make it work for now, and will worry about cleaning it up later. Thanks guys.

VB.NET:
    Sub Main()

        Dim accountAdapter As New AccountInfoDataSetTableAdapters.accountsTableAdapter
        accountAdapter.Insert("testing", 23, "developer", 1)

        Dim accountDataSet As DataSet = New AccountInfoDataSet
        Dim newRow As DataRow = accountDataSet.Tables("accounts").NewRow()
        newRow("username") = "blah"
        newRow("account_id") = 235
        newRow("type") = "developer"
        newRow("default_account") = 1

        accountDataSet.Tables("accounts").Rows.Add(newRow)
        accountDataSet.AcceptChanges()

        'Try to update (doesn't work)
        'accountAdapter.Update(accountDataSet)

        Dim newRows As DataTable = accountDataSet.Tables("accounts").GetChanges(Data.DataRowState.Added)
        accountAdapter.Update(newRows)

        'Connect to the database
        AccountDBConnection.Open()

        'Get needed info
        Dim SQLcmd As New SqlCommand("SELECT username,default_account FROM accounts", AccountDBConnection)
        Dim myReader As SqlDataReader = SQLcmd.ExecuteReader()

        'Check for existing accounts
        If (myReader.HasRows) Then

            Dim x As Integer = 0
            frmLogin.Show()

            While (myReader.Read())
                frmLogin.comboUsername.Items.Add(myReader.GetString(0))
                If myReader.GetInt32(1) = 1 Then
                    frmLogin.comboUsername.SelectedIndex = x
                End If

                x = x + 1
            End While

        Else
            frmCreateAccount.Show()
        End If

        myReader.Close()
        Application.Run()
        AccountDBConnection.Close()
        MsgBox("DONE")

    End Sub

Thanks,
Matt
 
Ok, here's something else I found. I followed one of the beginner tutorials, created a new form, and from my Data Sources dragged the 'accounts' table onto the form. This placed the BindingNavigator and DataGridView controls on my form, all linked and setup to manage the 'accounts' table easily.

I run the form, insert a row or two into the 'accounts' table, click the Save button, and the same result after I exit the form. The data shows up fine in the database, but once the application exits, the data disappears. And I know my other attempts to insert a record through code were right. I've tried several different methods, and they all insert the data, but it never gets saved / updated.

Anyone have any advice?

Thanks,
Matt
 
Back
Top