Hawiie13 Hawiie13 - 4 years ago 125
Vb.net Question

Compute and Update vaules if four textboxes are filled VB.net

I want to compute and update the database if four text boxes are filled.

If 1 or 2 or 3 are filled it will not compute, it will just update to database.

here is my code.

Dim constring As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=|DataDirectory|\SLACdb.mdb;"
Dim con As OleDbConnection = New OleDbConnection(constring)
Dim cmd As OleDbCommand
Dim dt As DataTable = New DataTable()

Dim prelim, midterm, prefinal, final, ave, eq As String

prelim = txtboxprelim.Text
midterm = txtboxmidterm.Text
prefinal = txtboxsemi.Text
final = txtboxfinals.Text
ave = txtboxave.Text
eq = txtboxequivalent.Text

Dim sql As String = "UPDATE StudentLoad SET prelim = ?, midterm = ?, semifinals = ?, finals = ?" & _
" WHERE name = '" & txtboxname.Text & "' "

If prelim = "" Or midterm = "" Or prefinal = "" Or final = "" Then
cmd = New OleDbCommand(sql, con)


cmd.Parameters.AddWithValue("@p1", prelim)
cmd.Parameters.AddWithValue("@p2", midterm)
cmd.Parameters.AddWithValue("@p3", prefinal)
cmd.Parameters.AddWithValue("@p4", final)

Try
con.Open()
If (cmd.ExecuteNonQuery > 0) Then

End If
con.Close()

Catch ex As Exception
MsgBox(ex.Message)
End Try
Me.StudentRemarksTableAdapter.Fill(Me.SLACdbDataSet.StudentRemarks)
Else
txtboxave.Text = (prelim + midterm + prefinal + final) / 4
If txtboxave.Text <= 77 Then
txtboxequivalent.Text = 3
ElseIf txtboxave.Text <= 80 Then
txtboxequivalent.Text = 2.75
ElseIf txtboxave.Text <= 83 Then
txtboxequivalent.Text = 2.5
ElseIf txtboxave.Text <= 86 Then
txtboxequivalent.Text = 2.25
ElseIf txtboxave.Text <= 89 Then
txtboxequivalent.Text = 2
ElseIf txtboxave.Text <= 92 Then
txtboxequivalent.Text = 1.75
ElseIf txtboxave.Text <= 95 Then
txtboxequivalent.Text = 1.5
ElseIf txtboxave.Text <= 98 Then
txtboxequivalent.Text = 1.25
ElseIf txtboxave.Text <= 100 Then
txtboxequivalent.Text = 1
Else
txtboxequivalent.Text = 5
End If

Dim constring1 As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=|DataDirectory|\SLACdb.mdb;"
Dim con1 As OleDbConnection = New OleDbConnection(constring1)
Dim cmd1 As OleDbCommand
Dim dt1 As DataTable = New DataTable()


Dim sql1 As String = "UPDATE StudentLoad SET prelim = ?, midterm = ?, semifinals = ?, finals = ?, average = ?, equivalent = ?" & _
" WHERE name = '" & txtboxname.Text & "'"
cmd1 = New OleDbCommand(sql1, con1)


cmd1.Parameters.AddWithValue("@p1", prelim)
cmd1.Parameters.AddWithValue("@p2", midterm)
cmd1.Parameters.AddWithValue("@p3", prefinal)
cmd1.Parameters.AddWithValue("@p4", final)
cmd1.Parameters.AddWithValue("@p5", ave)
cmd1.Parameters.AddWithValue("@p6", eq)


Try
con1.Open()
If (cmd1.ExecuteNonQuery > 0) Then

End If
con1.Close()

Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Me.StudentRemarksTableAdapter.Fill(Me.SLACdbDataSet.StudentRemarks)
Admin.StudentLoadTableAdapter.Update(SLACdbDataSet.StudentLoad)


with this code it only update the database if the four text boxes are filled it doesn't compute.

sorry for bad english

Answer Source

The problem in your code is that the prelim, midterm, prefinal, final, ave and eq are string.

In order for you to compute your average, you need to make those variables as an integer. Thus Dim prelim as integer = convert.toint32(txtboxprelim.text) and also do that to midterm, prefinal, final, ave and eq. Your cmd.parameters should be

cmd.Parameters.AddWithValue("@p1", txtboxprelims.text)
cmd.Parameters.AddWithValue("@p2", txtboxmidterm.text)
cmd.Parameters.AddWithValue("@p3", txtboxprefinal.text)
cmd.Parameters.AddWithValue("@p4", txtboxfinal.text)

in your else statement below:

txtboxave.Text = (prelim + midterm + prefinal + final) / 4

should be

ave = (prelim + midterm + prefinal + final) / 4

replace your txtboxave.text to ave, then below the if and else statements add:

txtboxequivalent.text = eq
txtboxave.text = ave

finally, your cmd.paremeters below should be:

cmd1.Parameters.AddWithValue("@p1", prelim.tostring)
cmd1.Parameters.AddWithValue("@p2", midterm.tostring)
cmd1.Parameters.AddWithValue("@p3", prefinal.tostring)
cmd1.Parameters.AddWithValue("@p4", final.tostring)
cmd1.Parameters.AddWithValue("@p5", ave.tostring)
cmd1.Parameters.AddWithValue("@p6", eq.tostring)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download