Question

My project is coming along nicely thanks in part to all of your help! I have come across another bump in the road. I need to enable (up to 8) buttons based on whether or not a cell in a sql db has a 'y', 'n' or is null. I can get it to work for one button, but not 2 or more. Before I show you my code, let me give a bit more detail: My database stores info about students and grades (an numerous other things, but they don't pertain to this question). Each student must complete all eight(8) classes to graduate. In the db, I have columns labeled for each of the eight classes. If they took the class, it's a 'y' in the row. If they failed the class, it's a 'n'. If they haven't taken it yet or it's in progress, the cell has null when I view it in sql server management studio because they neither completed it nor failed it, so no data is entered yet.

I have eight(8) buttons with labels for each class. When a studentId is entered by the user and the db searched, I want the buttons enabled based on the database values. I got one to work, but that's it and I can't figure out why. Would greatly appreciate someone pointing out what is wrong in my code and point me in the correct direction.

    Try
        Using connection As New SqlConnection("Data Source=?;Initial Catalog=?;Persist Security Info=True;User ID=?;Password=?")
            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 g1, g2, af1, af2, af3, pp1, pp2, pp3 from student_info where studentId = '" & stunumtxtbox.Text & "'", connection)
            Dim count = da.Fill(dt)
            If count = 1 Then
                G1Button.Enabled = True
            End If
            If count = 2 Then
                G2Button.Enabled = True
            End If
            If count = 3 Then
                AF1Button.Enabled = True
            End If
            If count = 4 Then
                AF2Button.Enabled = True
            End If
            If count = 5 Then
                AF3Button.Enabled = True
            End If
            If count = 6 Then
                PP1Button.Enabled = True
            End If
            If count = 7 Then
                PP2Button.Enabled = True
            End If
            If count = 8 Then
                PP3Button.Enabled = True
            End If
        End Using
    Catch se As SqlException
        MsgBox(se.Message)
    Finally
    End Try

Thank you in advance!

Était-ce utile?

La solution

You can use LINQ to achieve this. The following code will enable the button if the table contains a row where the specified field/column has the value y or n. If not, the button is disabled.

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()
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()
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()
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()
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()
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()
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()
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.G1Button.BackColor = If(g1.Contains("y"), Color.Green, If(g1.Contains("n"), Color.Red, SystemColors.Control))
Me.G1Button.Enabled = (g1.Count > 0)

Me.G2Button.BackColor = If(g2.Contains("y"), Color.Green, If(g2.Contains("n"), Color.Red, SystemColors.Control))
Me.G2Button.Enabled = (g2.Count > 0)

Me.AF1Button.BackColor = If(af1.Contains("y"), Color.Green, If(af1.Contains("n"), Color.Red, SystemColors.Control))
Me.AF1Button.Enabled = (af1.Count > 0)

Me.AF2Button.BackColor = If(af2.Contains("y"), Color.Green, If(af2.Contains("n"), Color.Red, SystemColors.Control))
Me.AF2Button.Enabled = (af2.Count > 0)

Me.AF3Button.BackColor = If(af3.Contains("y"), Color.Green, If(af3.Contains("n"), Color.Red, SystemColors.Control))
Me.AF3Button.Enabled = (af3.Count > 0)

Me.PP1Button.BackColor = If(pp1.Contains("y"), Color.Green, If(pp1.Contains("n"), Color.Red, SystemColors.Control))
Me.PP1Button.Enabled = (pp1.Count > 0)

Me.PP2Button.BackColor = If(pp2.Contains("y"), Color.Green, If(pp2.Contains("n"), Color.Red, SystemColors.Control))
Me.PP2Button.Enabled = (pp2.Count > 0)

Me.PP3Button.BackColor = If(pp3.Contains("y"), Color.Green, If(pp3.Contains("n"), Color.Red, SystemColors.Control))
Me.PP3Button.Enabled = (pp3.Count > 0)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top