Trouble updating data from textboxes.

Peinecone

Member
Joined
Feb 6, 2009
Messages
13
Programming Experience
Beginner
I have the following code to update the database from the textboxes on a form. But I am doing something wrong, because it only updates the first line of code (dr.Item("Category") = cboCategory.SelectedValue) and the other data returns to their previous value. No matter what the first line is, it only updates that. So if I comment out the "category" line then the "Price" line will update. I am pretty sure it has to do with the DataRow, but I don't know another way to update.

Edit: The row that gets updated corresponds to the item selected in a listbox.

VB.NET:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dr As DataRow = DirectCast(bs.Current, DataRowView).Row

        dr.Item("Category") = cboCategory.SelectedValue
        dr.Item("Price") = txtPrice.Text
        dr.Item("UM") = cboUM.SelectedValue
        dr.Item("Units") = txtUnits.Text
        dr.Item("UnitSize") = txtUnitSize.Text
        If cboUnivUM.SelectedValue <> Nothing Then
            dr.Item("UniversalUM") = cboUnivUM.SelectedValue
        End If
        dr.Item("UnivUnitsPer") = txtUnivPer.Text

        da.Update(ds, "SyscoPrice")
    End Sub
 
Last edited:
Howcome youre doing this in such an old/manual way? Did you follow a tutorial targeted at .NET 1.0 ?

If you follow the steps in the DW2 link in my signature, section for Creating a Simple Data App you'll fidn a way of making your data access a lot easier

If you were to post a version of your project, including the database, I'd rework it so you could see..
 
Howcome youre doing this in such an old/manual way? Did you follow a tutorial targeted at .NET 1.0 ?

If you follow the steps in the DW2 link in my signature, section for Creating a Simple Data App you'll fidn a way of making your data access a lot easier

If you were to post a version of your project, including the database, I'd rework it so you could see..

I am still very new to VB.net. My previous experiance has been with VBA working with excel. So I have been putting things together, but probably not the best way.

VB.NET:
Public Class frmSysco

    Dim MaxRows As Integer
    Dim NewItem As Boolean = False

    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim dt As New DataSet
    Dim db As New OleDb.OleDbDataAdapter
    Dim bs As New BindingSource
    Dim bsCat As New BindingSource
    Dim daCat As New OleDb.OleDbDataAdapter
    Dim dsCat As New DataSet
    Dim bsPUnit As New BindingSource
    Dim daPUnit As New OleDb.OleDbDataAdapter
    Dim dsPUnit As New DataSet
    Dim bsUvUM As New BindingSource
    Dim daUvUM As New OleDb.OleDbDataAdapter
    Dim dsUvUM As New DataSet
    Dim sql As String


    Private Sub frmSysco_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = D:\Ben's Documents\Work Things\Cost Manangment System\MainDatabase.accdb"
        con.Open()
        sql = "SELECT * FROM SyscoMasterPriceGuide"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "SyscoPrice")
        con.Close()

        bs.DataSource = ds.Tables("SyscoPrice")
        bs.Sort = "ProductName"
        lstName.DataSource = bs
        lstName.DisplayMember = "ProductName"
        lstName.ValueMember = "ProductCode"

        con.Open()
        sql = "SELECT * FROM PurchaseCategories"
        daCat = New OleDb.OleDbDataAdapter(sql, con)
        daCat.Fill(dsCat, "Category")
        con.Close()
        bsCat.DataSource = dsCat.Tables("Category")
        bsCat.Sort = "PurchaseCategory"
        cboCategory.DataSource = bsCat
        cboCategory.DisplayMember = "PurchaseCategory"
        cboCategory.ValueMember = "ID"

        con.Open()
        sql = "SELECT * FROM PurchaseUnits"
        daPUnit = New OleDb.OleDbDataAdapter(sql, con)
        daPUnit.Fill(dsPUnit, "Unit")
        con.Close()
        bsPUnit.DataSource = dsPUnit.Tables("Unit")
        bsPUnit.Sort = "Unit"
        cboUM.DataSource = bsPUnit
        cboUM.DisplayMember = "Unit"
        cboUM.ValueMember = "ID"

        con.Open()
        sql = "SELECT * FROM UniversalUnit"
        daUvUM = New OleDb.OleDbDataAdapter(sql, con)
        daUvUM.Fill(dsUvUM, "Unit")
        con.Close()
        bsUvUM.DataSource = dsUvUM.Tables("Unit")
        bsUvUM.Sort = "Unit"
        cboUnivUM.DataSource = bsUvUM
        cboUnivUM.DisplayMember = "Unit"
        cboUnivUM.ValueMember = "ID"

        MaxRows = ds.Tables("SyscoPrice").Rows.Count
        txtPC.Enabled = False
        Populate()
    End Sub

    Private Sub lstName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstName.SelectedIndexChanged
        Populate()
    End Sub

    Private Sub Populate()
        Dim dr As DataRow = DirectCast(bs.Current, DataRowView).Row
        txtName.Text = dr.Item("ProductName").ToString
        txtPC.Text = dr.Item("ProductCode").ToString
        cboCategory.SelectedValue = dr.Item("Category")
        txtPrice.Text = dr.Item("Price").ToString
        cboUM.SelectedValue = dr.Item("UM")
        txtUnits.Text = dr.Item("Units").ToString
        txtUnitSize.Text = dr.Item("UnitSize").ToString
        cboUnivUM.SelectedValue = dr.Item("UniversalUM")
        txtUnivPer.Text = dr.Item("UnivUnitsPer").ToString
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If MessageBox.Show("Do you really want to Delete this Record?", _
            "Delete", MessageBoxButtons.YesNo, _
            MessageBoxIcon.Warning) = DialogResult.No Then
            Exit Sub
        End If
        da.AcceptChangesDuringUpdate = True
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        bs.RemoveCurrent()
        da.Update(ds, "SyscoPrice")
        Populate()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Select Case NewItem
            Case False
                [COLOR="Red"]'Where I am having trouble[/COLOR]
                Dim dr As DataRow = DirectCast(bs.Current, DataRowView).Row
                dr.Item("ProductName") = txtName.Text
                dr.Item("Category") = cboCategory.SelectedValue
                dr.Item("Price") = txtPrice.Text
                dr.Item("UM") = cboUM.SelectedValue
                dr.Item("Units") = txtUnits.Text
                dr.Item("UnitSize") = txtUnitSize.Text
                If cboUnivUM.SelectedValue <> Nothing Then
                    dr.Item("UniversalUM") = cboUnivUM.SelectedValue
                End If
                dr.Item("UnivUnitsPer") = txtUnivPer.Text
                da.Update(ds, "SyscoPrice")
            Case True
                Dim dsNewRow As DataRow
                Dim PC As Integer
                PC = txtPC.Text
                dsNewRow = ds.Tables("SyscoPrice").NewRow()
                dsNewRow.Item("ProductName") = txtName.Text
                dsNewRow.Item("ProductCode") = txtPC.Text
                dsNewRow.Item("Category") = cboCategory.SelectedValue
                dsNewRow.Item("Price") = txtPrice.Text
                dsNewRow.Item("UM") = cboUM.SelectedValue
                dsNewRow.Item("Units") = txtUnits.Text
                dsNewRow.Item("UnitSize") = txtUnitSize.Text
                dsNewRow.Item("UniversalUM") = cboUnivUM.SelectedValue
                dsNewRow.Item("UnivUnitsPer") = txtUnivPer.Text
                ds.Tables("SyscoPrice").Rows.Add(dsNewRow)
                da.AcceptChangesDuringUpdate = True
                da.Update(ds, "SyscoPrice")

                NewItem = False
                lstName.Enabled = True
                txtPC.Enabled = False
                btnDelete.Enabled = True
                btnAddNew.Enabled = True
                lstName.SelectedValue = PC
        End Select
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If NewItem = True Then
            lstName.Enabled = True
            txtPC.Enabled = False
            btnDelete.Enabled = True
            btnAddNew.Enabled = True
            NewItem = False
        End If
        Populate()
    End Sub
    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        NewItem = True
        lstName.Enabled = False
        txtPC.Enabled = True
        btnDelete.Enabled = False
        btnAddNew.Enabled = False
        txtName.Clear()
        txtPC.Clear()
        cboCategory.SelectedValue = 0
        txtPrice.Clear()
        cboUM.SelectedValue = 0
        txtUnits.Clear()
        txtUnitSize.Clear()
        cboUnivUM.SelectedValue = 0
        txtUnivPer.Clear()
    End Sub

End Class

My database has the following columns:
0 - ID
1 - ProductCode (PK)
2 - Category (FK)
3 - ProductName
4 - Price
5 - UM (FK)
6 - Units
7 - UnitSize
8 - UniversalUM (FK)
9 - UnivUnitsPer
 
Can you post a copy of the database, and give me an idea of which table you want to update and with what? I'll do an example project
 
Here is a copy of the database. With the form, I am trying to update the table "SyscoMasterPriceGuide" from the data in the text and combo boxes.
 

Attachments

  • CopyMainDatabase.zip
    180.3 KB · Views: 24
Hi. I did an example project, but I think i replied/attached it to another of your threads..
 

Latest posts

Back
Top