CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: saving data

  1. #1
    Join Date
    Mar 2008
    Posts
    142

    saving data

    i have a small program which calculates weights
    but i want to store those weights into database
    so i have one table in database and it has two fields FWeight and SWeight
    there is button which calculates weights and i want that as i press it it should calculate and store first weight
    secondly i have added a combo box to the form
    i want that as i save first weight it should updated and i can select stored value from it and then it can display those value against first weight text box
    code for save method
    Code:
     Public Sub save()
            Try
    
                conn.Open()
    
                'command.Connection = conn
                Dim sql As String = String.Empty
                sql = "INSERT INTO Weight(FWeight,SWeight)Values(?,?,)"
                Dim command As New OleDbCommand(sql, conn)
                command.Parameters.AddWithValue("FWeight", txtFWeight.Text)
                command.Parameters.AddWithValue("SWeight", txtSWeight.Text)
                command.ExecuteNonQuery()
    
            Catch ex As Exception
                ' MsgBox(ex.ToString)
    
     Finally
                conn.Close()
    
            End Try
        End Sub
    code for calculating total
    Code:
    Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click
            Try
                FWeight = CLng(txtFWeight.Text)
                SWeight = CLng(txtSWeight.Text)
                total = FWeight - SWeight
                txtTotal.Text = total & " Kg"
                totalMon = total / 40
                txtTotalMon.Text = totalMon
                save()
                Fill_FCombo()
            Catch ex As Exception
                MessageBox.Show("Values are too large", "Weight Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End Sub
    code for cmbo box operations
    Code:
    Private Sub Fill_FCombo()
            Try
                Dim r As DataRow
                cmboFWeight.Items.Clear()
                For Each r In dSet.Tables(0).Rows
                    cmboFWeight.Items.Add(r.Item(1))
                Next
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub cmboFWeight_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmboFWeight.SelectedIndexChanged
            Try
                dSet.Tables(0).PrimaryKey = New DataColumn() {dSet.Tables(0).Columns("FWeight")}
                Dim row As DataRow
                row = dSet.Tables(0).Rows.Find(cmboFWeight.Text)
                txtFWeight.Text = row("FWeight")
                txtSWeight.Text = row("SWeight")
            Catch ex As Exception
    
            End Try
        End Sub

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: saving data

    Is there a question in there somewhere?????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Mar 2008
    Posts
    142

    Re: saving data

    problem of saving a record into table has been solved
    but as i click btnTotal then record is saved in database but also i want that cmboFWeight should be updated and filled with all records
    so i did it as follow
    Code:
    Private Sub Fill_FCombo()
            Try
                Dim r As DataRow
                cmboFWeight.Items.Clear()
                For Each r In dSet.Tables(0).Rows
                    cmboFWeight.Items.Add(r.Item(1))
                Next
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
        End Sub
    but this gives exception "System.NullReferenceExceptionbject instance not set to an instance of an object

    so i also want that i can select a value from combo box and these values should be set into textboxes
    so i did it as follow
    Code:
      Private Sub cmboFWeight_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmboFWeight.SelectedIndexChanged
            Try
                dSet.Tables(0).PrimaryKey = New DataColumn() {dSet.Tables(0).Columns("FWeight")}
                Dim row As DataRow
                row = dSet.Tables(0).Rows.Find(cmboFWeight.Text)
                txtFWeight.Text = row("FWeight")
                txtSWeight.Text = row("SWeight")
            Catch ex As Exception
    
            End Try
        End Sub
    kindly tell me the solution.

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

Featured