通过pID链接数据库表

时间:2014-02-05 06:02:13

标签: asp.net vb.net drop-down-menu

我在使用pID链接2个数据表时遇到问题。它不是自动编号。问题是,当我在下拉列表中选择一个选项时,我必须将pID号保存到数据库中。但显示的选项是课程名称(文本)。

从下拉列表中选择后,将文本输入文本框,然后单击按钮将pID编号和说明保存到数据库中。在1个数据库表中,它具有课程名称和pID编号。第二个数据库表具有pID编号和描述(将信息保存到此处)。

我只能保存selectedindex,这不是pID号。

.aspx.vb代码

    Dim OdbcCon As New System.Data.Odbc.OdbcConnection
    Dim cmd As New OdbcCommand

    OdbcCon.Open()


    cmd.Connection = OdbcCon
    cmd.CommandText = " insert into StdBookInfo(pID,Description) values ('" & DropDownListStudent.SelectedIndex & "','" & TextBox1.Text & "') "

    cmd.ExecuteNonQuery()

下拉列表代码:

    sqlcmd3 = "select Course_code + ' - ' + package from StudentPackage order by pID asc "
    sqlcmd4 = "select pID from StudentPackage order by Course_code,package asc "

    cmd3.Connection = OdbcCon
    cmd3.CommandText = sqlcmd3
    DropDownListStudent.Items.Add("")

    reader3 = cmd3.ExecuteReader
    While reader3.Read
        DropDownListStudent.Items.Add(reader3.Item(0).ToString)
    End While
    DropDownListStudent.SelectedIndex = -1



    reader3.Close()
    cmd3.Dispose()
    cmd4.Connection = OdbcCon
    cmd4.CommandText = sqlcmd4
    reader4 = cmd4.ExecuteReader
    I = 0
    While reader4.Read
        pID(I) = reader4.Item(0)
        I = I + 1

    End While
    reader4.Close()
    cmd4.Dispose()
    OdbcCon.Close()

1 个答案:

答案 0 :(得分:0)

您需要设置DropDownList的每个项目的Text和Value属性,然后使用SelectedValue而不是SelectedIndex。

<asp:DropDownList id="DropDownListStudent" Runat="Server">
  <asp:ListItem Text="Item 1" Value="1"/>
  <asp:ListItem Text="Item 2" Value="2"/>
  <asp:ListItem Text="Item 3" Value="3"/>
  <asp:ListItem Text="Item 4" Value="4"/>
  <asp:ListItem Text="Item 5" Value="5"/>
</asp:DropDownList>