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 for calculating totalCode: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 cmbo box operationsCode: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: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




Reply With Quote