VB如果其他语句在组合框中不起作用

时间:2018-10-16 12:58:44

标签: vb.net combobox

我有一个组合框,用于根据学期选择学科。就像第一学期一样,这些将是科目,依此类推。我的组合框已经被填充,来自我的数据库。我的问题是,即使是在第一学期,组合框上显示的主题也是第二学期的主题。我觉得错误与查询无关,而与条件本身有关。

Private Sub fillsubjectcombo()

    If yearlabel.Text = "Grade 11" Or course_label.Text = "STEM" Or semester_label.Text = "Second semester" Then

        Dim str As String

        con.Close()
        con.Open()

        str = "SELECT* FROM [Subjects] ORDER BY [STEM G11 SS]"
        cmd = New SqlClient.SqlCommand(str, con)
        cmd.CommandText = str
        cmd.Connection = con
        drd = cmd.ExecuteReader
        subject_cbx.Items.Clear()

        While (drd.Read())

            subject_cbx.Items.Add(drd("STEM G11 SS"))

        End While

        cmd.Dispose()
        drd.Close()
        con.Close()

    End If

    If yearlabel.Text = "Grade 11" Or course_label.Text = "STEM" Or semester_label.Text = "First semester" Then

        Dim sti As String

        con.Close()
        con.Open()

        sti = "SELECT* FROM [Subjects] ORDER BY [STEM G11 FS]"
        cmd = New SqlClient.SqlCommand(sti, con)
        cmd.CommandText = sti
        cmd.Connection = con
        drd = cmd.ExecuteReader
        subject_cbx.Items.Clear()

        While (drd.Read())

            subject_cbx.Items.Add(drd("STEM G11 FS"))

        End While

        cmd.Dispose()
        drd.Close()

        con.Close()

    End If

有人可以帮助我吗?请:(

4 个答案:

答案 0 :(得分:1)

根据经验,当您在同一页面上复制和粘贴行时,请回想一下您在做什么,因为它可能会有所改进。

关于您的问题,我想到了一点:

/Qpar

请注意,这几乎完全是您的代码,但是以避免重复的方式进行安排。另外,现在请选中您的组合框!

答案 1 :(得分:0)

尝试一下:

Private Sub fillsubjectcombo()

If (yearlabel.Text = "Grade 11" Or course_label.Text = "STEM") AndAlso (semester_label.Text = "Second semester") Then

    Dim str As String

    con.Close()
    con.Open()

    str = "SELECT* FROM [Subjects] ORDER BY [STEM G11 SS]"
    cmd = New SqlClient.SqlCommand(str, con)
    cmd.CommandText = str
    cmd.Connection = con
    drd = cmd.ExecuteReader
    subject_cbx.Items.Clear()

    While (drd.Read())

        subject_cbx.Items.Add(drd("STEM G11 SS"))

    End While

ElseIf (yearlabel.Text = "Grade 11" Or course_label.Text = "STEM") AndAlso (semester_label.Text = "First semester") Then

    Dim sti As String

    con.Close()
    con.Open()

    sti = "SELECT* FROM [Subjects] ORDER BY [STEM G11 FS]"
    cmd = New SqlClient.SqlCommand(sti, con)
    cmd.CommandText = sti
    cmd.Connection = con
    drd = cmd.ExecuteReader
    subject_cbx.Items.Clear()

    While (drd.Read())

        subject_cbx.Items.Add(drd("STEM G11 FS"))

    End While

End If

cmd.Dispose()
drd.Close()
con.Close()

答案 2 :(得分:0)

如果我理解您的疑问的方式是正确的,这将起作用。

DataGridRow

答案 3 :(得分:0)

@laancelot先生,谢谢您提供的密码,我想出了这些密码,一切正常!

最终代码在这里,我在If-Else语句中的问题已更改为Select-Case

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">

    <!-- Logging Properties -->
    <Properties>
        <Property name="LOG_PATTERN">%d [%t] %-5p %c:%L [%X{CLIENT_ID}] | %m%n</Property>
        <Property name="APP_LOG_ROOT">C:/shivom/LoggerPOC</Property>
    </Properties>

    <Appenders>

        <RollingFile name="Logger1File"
            fileName="${APP_LOG_ROOT}/LoggerWebservices.log"
            filePattern="${APP_LOG_ROOT}/LoggerWebservices-{yyyy-MM-dd}-%d [%c] [%m] %-5level: %msg%n-%i.log">
            <LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG"
                onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="${LOG_PATTERN}" />
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
<RollingFile name="LoggerIngrianFile"
            fileName="${APP_LOG_ROOT}/LoggerIngrianFile.log"
            filePattern="${APP_LOG_ROOT}/LoggerIngrianFile-{yyyy-MM-dd}-%d [%c] [%m] %-5level: %msg%n-%i.log">
            <LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG"
                onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="${LOG_PATTERN}" />
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
    </Appenders>

    <Loggers>
<logger name="ingrian" level="debug" >
<AppenderRef ref="LoggerIngrianFile" />
</logger>

        <Root level="DEBUG">
<AppenderRef ref="Logger1File" />
        </Root> 

    </Loggers>

</Configuration>

再次感谢您,@ laancelot先生!