我试图用SQL Server存储过程返回的数据填充一个下拉列表,但要使它成为数据值文本字段是存储过程返回的一个字段,而数据值字段是由存储过程返回的另一个值存储过程(该存储过程返回一个名为“父母姓名”的字段和另一个名为“父母ID”的字段)。后面的代码是VB,我将其发布在下面:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim s_SQL_Database_Connection As SqlConnection
Dim s_Connection_String As String = Session("Connection_String")
Dim s_SQL_Command As SqlCommand
Dim s_Record_Set As SqlDataReader
s_SQL_Database_Connection = New SqlConnection(s_Connection_String)
s_SQL_Database_Connection.Open()
s_SQL_Command = New SqlCommand("EXEC Krandor.MattA.__Parent_List", s_SQL_Database_Connection)
s_Record_Set = s_SQL_Command.ExecuteReader()
Dim parent_name As String
Dim parent_id As Integer
While s_Record_Set.Read
parent_name = s_Record_Set.Item("Parent Name")
Me.cbo_parent.Items.Add(New ListItem(parent_name))
End While
s_Record_Set.Close()
s_SQL_Database_Connection.Close()
End Sub
Public ReadOnly Property parent_id() As String
Get
Dim s_parent As String
s_parent = cbo_parent.DataValueField
Return s_parent
End Get
End Property
基本上,在while循环中,我想为每个下拉菜单项的文本字段设置“父母姓名”,并为每个下拉菜单项的值字段设置“父母ID”。任何帮助将不胜感激,如果对此问题已经提出,我深表歉意(我进行了搜索,但没有找到适合我的情况的任何信息)。
答案 0 :(得分:0)
您可以将Parent ID
字段值作为第二个参数传递给ListItem构造函数:
While s_Record_Set.Read
parent_name = s_Record_Set.Item("Parent Name")
Me.cbo_parent.Items.Add(New ListItem(parent_name, s_Record_Set.Item("Parent ID")))
End While