本文介绍了使用datareader填充下拉列表中的值时如何提供DataValueField和DataTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用了这段代码.但是我无法在另一个查询的条件下获取其datavaluefield.任何人都可以帮助我.
Hi,
I used this code. but i cant get its datavaluefield in where condition to another query. Any one plz can help me.
cmdq2.Dispose()
cmdq2.Parameters.Clear()
cmdq2.Connection = con
cmdq2.CommandType = CommandType.StoredProcedure
cmdq2.CommandText = "sp_inout"
cmdq2.Parameters.Add("@companyid", SqlDbType.BigInt).Value = Session("maincompanyid")
cmdq2.Parameters.Add("@contactid", SqlDbType.BigInt).Value = Session("Customer_id")
cmdq2.Parameters.Add("@invoiceid", SqlDbType.Int).Value = cash_invid
cmdq2.Parameters.Add("@currency", SqlDbType.Char).Value = currency
'****************
Dim idr As SqlDataReader = cmdq2.ExecuteReader()
If idr.Read Then
If idr.HasRows = True Then
ddlinv.Items.Add(idr(1).ToString())
ddlinvid = idr(0).ToString
ddlinv.DataValueField = idr(0).ToString()
ddlinv.DataTextField = idr(1).ToString()
idr.Close()
Else
End If
End If
推荐答案
cmdq2.Dispose()
cmdq2.Parameters.Clear()
cmdq2.Connection = con
cmdq2.CommandType = CommandType.StoredProcedure
cmdq2.CommandText = "sp_inout"
cmdq2.Parameters.Add("@companyid", SqlDbType.BigInt).Value = Session("maincompanyid")
cmdq2.Parameters.Add("@contactid", SqlDbType.BigInt).Value = Session("Customer_id")
cmdq2.Parameters.Add("@invoiceid", SqlDbType.Int).Value = cash_invid
cmdq2.Parameters.Add("@currency", SqlDbType.Char).Value = currency
'Using Datareader to set DatavalueField and Datatextfield
Dim idr As SqlDataReader = cmdq2.ExecuteReader()
If idr.Read() Then
Dim item As ListItem = New ListItem()
item.Text = idr("invoiceno").ToString()
item.Value = idr("invoiceid").ToString()
ddlinv.Items.Add(item)
End If
Con = New SqlConnection("Your ConnectionString")
Con.Open()
Cmd = New SqlCommand("Select * from Products")
dt = New SqlDataAdapter("Select ProductId,ProductName+ProductPrice as Name from Products", Con)
' Using System.Data;
Dim ds As New DataSet()
'FillDataSet
dt.Fill(ds)
'Bind Drodown
DropDownList1.DataSource = ds
DropDownList1.DataTextField = Convert.ToString(ds.Tables(0).Rows(0)("Name"))
DropDownList1.DataValueField = Convert.ToString(ds.Tables(0).Rows(0)("Id"))
DropDownList1.DataBind()
Con.Close()
这篇关于使用datareader填充下拉列表中的值时如何提供DataValueField和DataTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!