Click to See Complete Forum and Search --> : saving data


aamir55
October 31st, 2008, 11:42 PM
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

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

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

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

TheCPUWizard
November 1st, 2008, 12:01 AM
Is there a question in there somewhere?????

aamir55
November 1st, 2008, 09:52 AM
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

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.NullReferenceException:object 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

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.