如果四个文本框填充VB.net,则计算和更新vaules

时间:2017-02-07 04:10:04

标签: database vb.net ms-access

如果填写了四个文本框,我想计算和更新数据库。

如果填写了1或2或3,它将无法计算,它只会更新到数据库。

这是我的代码。

 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)

使用此代码,只有填充了四个文本框才会更新数据库,但不会计算。

抱歉英语不好

1 个答案:

答案 0 :(得分:1)

你的代码中的问题是prelim,midterm,prefinal,final,ave和eq是字符串。

为了计算平均值,您需要将这些变量设为整数。从而 Dim prelim as integer = convert.toint32(txtboxprelim.text)也适用于期中,期初,最后,平均和均衡。您的cmd.parameters应为

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

在下面的其他声明中:

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

应该是

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

将你的txtboxave.text替换为ave,然后在if和else语句下面添加:

txtboxequivalent.text = eq
txtboxave.text = ave

最后,您的cmd.paremeters应该是:

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)