我需要帮助将所有逻辑语句按正确顺序排列

时间:2014-06-08 06:19:59

标签: vb.net

当我点击SEARCH按钮时,这就是我想要发生的事情:

如果搜索字段为空,则显示一条消息,说明"请输入学号#34;。 如果搜索字段不为空,请在数据库中搜索与用户在搜索字段中输入的内容相匹配的特定记录,但要确保搜索字段中只有数字,如果找到了字母,则显示消息&# 34;仅限数字",然后在数据库中搜索特定记录。如果找不到记录,则显示一条消息,说明"学生不存在"。如果找到记录,请使用数据填充一些字段,然后根据记录中的内容,启用某些按钮并更改其颜色。

这就是我想要的。它确实有效......主要是。 SOMETHING出了故障或出错了。我无法弄明白。最底层的捕获声明被注释掉了,因为它可以用来工作,但现在它没有 - 再次,某些事情或某些事情发生故障。我根本不是高级开发人员,对我很轻松。

以下是我的搜索按钮点击事件的代码:

If stunumtxtbox.Text = "" Then
        MsgBox("Please enter a student number.", MsgBoxStyle.Exclamation)
        stunumtxtbox.Select()
    Else
        Try
            Using connection As New SqlConnection("Data Source=SERVER-NAME\IAWDB;Initial Catalog=iawdb;Persist Security Info=True;Integrated Security=True")
                connection.Open()
                Dim dt As New DataTable
                Dim ds As New DataSet
                Dim da As New SqlDataAdapter
                ds.Tables.Add(dt)
                da = New SqlDataAdapter("SELECT DISTINCT * FROM Student_Info WHERE studentId = '" & stunumtxtbox.Text & "'", connection)
                Dim count = da.Fill(dt)
                If count = 0 Then
                    MsgBox("Student ID not found.", MsgBoxStyle.Critical)
                Else
                    Dim g1 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("g1") = "y" Or r.Field(Of String)("g1") = "n" Select r.Field(Of String)("g1")).ToList()
                    Me.G1Button.BackColor = If(g1.Contains("y"), Color.Green, If(g1.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.G1Button.Enabled = (g1.Count > 0)

                    Dim g2 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("g2") = "y" Or r.Field(Of String)("g2") = "n" Select r.Field(Of String)("g2")).ToList()
                    Me.G2Button.BackColor = If(g2.Contains("y"), Color.Green, If(g2.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.G2Button.Enabled = (g2.Count > 0)

                    Dim af1 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("af1") = "y" Or r.Field(Of String)("af1") = "n" Select r.Field(Of String)("af1")).ToList()
                    Me.AF1Button.BackColor = If(af1.Contains("y"), Color.Green, If(af1.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.AF1Button.Enabled = (af1.Count > 0)

                    Dim af2 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("af2") = "y" Or r.Field(Of String)("af2") = "n" Select r.Field(Of String)("af2")).ToList()
                    Me.AF2Button.BackColor = If(af2.Contains("y"), Color.Green, If(af2.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.AF2Button.Enabled = (af2.Count > 0)

                    Dim af3 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("af3") = "y" Or r.Field(Of String)("af3") = "n" Select r.Field(Of String)("af3")).ToList()
                    Me.AF3Button.BackColor = If(af3.Contains("y"), Color.Green, If(af3.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.AF3Button.Enabled = (af3.Count > 0)

                    Dim pp1 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("pp1") = "y" Or r.Field(Of String)("pp1") = "n" Select r.Field(Of String)("pp1")).ToList()
                    Me.PP1Button.BackColor = If(pp1.Contains("y"), Color.Green, If(pp1.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.PP1Button.Enabled = (pp1.Count > 0)

                    Dim pp2 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("pp2") = "y" Or r.Field(Of String)("pp2") = "n" Select r.Field(Of String)("pp2")).ToList()
                    Me.PP2Button.BackColor = If(pp2.Contains("y"), Color.Green, If(pp2.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.PP2Button.Enabled = (pp2.Count > 0)

                    Dim pp3 As List(Of String) = (From r As DataRow In dt Where r.Field(Of String)("pp3") = "y" Or r.Field(Of String)("pp3") = "n" Select r.Field(Of String)("pp3")).ToList()
                    Me.PP3Button.BackColor = If(pp3.Contains("y"), Color.Green, If(pp3.Contains("n"), Color.Red, SystemColors.ControlDarkDark))
                    Me.PP3Button.Enabled = (pp3.Count > 0)

                    EditStudentToolStripMenuItem.Enabled = True
                End If
                For Each DataRow In dt.Rows
                    If stunumtxtbox.Text = dt.Rows(0)("studentId").ToString Then
                        fnametxtbox.Text = dt.Rows(0)("firstName").ToString
                        mnametxtbox.Text = dt.Rows(0)("midleInitial").ToString
                        lnametxtbox.Text = dt.Rows(0)("lastName").ToString
                        addresstxtbox.Text = dt.Rows(0)("addressStreet").ToString
                        address2txtbox.Text = dt.Rows(0)("addressOption").ToString
                        citytxtbox.Text = dt.Rows(0)("addressCity").ToString
                        statetxtbox.Text = dt.Rows(0)("addressState").ToString
                        ziptxtbox.Text = dt.Rows(0)("addressZip").ToString
                        countrytxtbox.Text = dt.Rows(0)("addressCountry").ToString
                        celltxtbox.Text = dt.Rows(0)("contactcellphone").ToString
                        studentidtxtbox.Text = dt.Rows(0)("studentId").ToString
                        TimeOwedTxtBox.Text = dt.Rows(0)("timeOwed").ToString
                        StudentNameTextBox.Text = lnametxtbox.Text & "," & " " & fnametxtbox.Text & " " & mnametxtbox.Text
                    Else
                    End If
                Next
            End Using
        Catch se As SqlException
            'If se.ErrorCode = -2146232060 Then
            'MsgBox("Enter numbers only.", MsgBoxStyle.Information)
            MsgBox(se.Message)
            'Else
            MsgBox(se.Message)
            'End If
        Finally
        End Try
    End If

那里有人可能比我更好一点,为我翻转它并让它工作然后帮助我理解所做的改变以及为什么要制作它们?哈哈,我知道,那要求很多。我虽然如此接近......我想。

1 个答案:

答案 0 :(得分:1)

嗯,首先需要测试stunumtxtbox.Text是否包含可以解析为Integer的值。简单的方法是使用Integer.TryParse方法。 在测试后立即使用它为空字符串。此外,您对空字符串的测试可能会更好。使用String.IsNullOrWhiteSpace方法。 请参阅此代码示例:

if Not String.IsNullOrWhiteSpace(stunumtxtbox.Text) Then
Dim i as Integer
    If Integer.TryParse(stunumtxtbox.Text, i) Then
        '' The string contains a valid integer, you can continue your code here.
    Else
        MsgBox("Enter numbers only.", MsgBoxStyle.Information)
        stunumtxtbox.Select()
    End If
Else
    MsgBox("Please enter a student number.", MsgBoxStyle.Exclamation)
    stunumtxtbox.Select()
End If

通常,不应将异常用于输入验证。 从不使用try ... catch来查找可以用其他简单方法测试的内容。

最后一件事:循环是多余的。如果你只有一行,则无需循环。