如何在asp.net网站上用dropdownlist绑定sql表行 [英] How to Bind sql table row with dropdownlist in asp.net website
问题描述
他是专家,
因为我认为这是一个非常简单的问题,但在工作一天时无法理清。
As i guess its very simple problem but not able to sort out while a day of working.
我有一个带有文本框和下拉列表的简单表单 这是基于id文本框从SQL表中进行的。
i have a simple form with a textboxes and dropdownlist which polputed from SQL table based on id textbox.
已根据登录详细信息填充了id文本框。
the id textbox already filled based on login details.
文本框正在进行良好的报告,但是下拉列表正在显示empty:
textboxes are polputing well but dropdownlist are showing empty:
这里是绑定DDL的代码:
here is the code to bind DDL :
Protected Sub LoadGender()
If Not Me.IsPostBack Then
Dim constr As String = (ConfigurationManager.ConnectionStrings("kckcmsDB").ConnectionString)
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("select genderID,gender from tblcategories Where genderID IS NOT NULL")
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
ddlFGender.DataSource = cmd.ExecuteReader()
ddlFGender.DataTextField = "gender"
ddlFGender.DataValueField = "genderID"
ddlFGender.DataBind()
con.Close()
End Using
End Using
ddlFGender.Items.Insert(0, New ListItem("--Select Gender--", "0"))
End If
End Sub
,这是所有文本框和下拉列表的绑定:
and here is the binding of all textboxes and dropdownlists:
Protected Sub filldetail()
Dim CSs As String = ConfigurationManager.ConnectionStrings("kckcmsDB").ConnectionString
Using conn As New SqlConnection(CSs)
Dim query As String = "select * from sturegI where uid='" + txtFuid.Text + "';"
Dim cmd As New SqlCommand(query, conn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "sturegI")
Dim dt As DataTable = ds.Tables(0)
If dt.Rows.Count > 0 Then
conn.Close()
txtFemail.Text = dt.Rows(0)("email").ToString()
txtFmobile.Text = dt.Rows(0)("mobile").ToString()
txtFSname.Text = dt.Rows(0)("Sname").ToString()
txtFFname.Text = dt.Rows(0)("Fname").ToString()
txtFMname.Text = dt.Rows(0)("Mname").ToString()
txtFdob.Text = dt.Rows(0)("dob").ToString()
ddlFGender.SelectedItem.Text = dt.Rows(0)("gender").ToString()
ddlFCategory.SelectedItem.Text = dt.Rows(0)("category").ToString()
ddlFReligion.SelectedItem.Text = dt.Rows(0)("religion").ToString()
ddlFMinority.SelectedItem.Text = dt.Rows(0)("minority").ToString()
Else
lblmsg.Text = "Record Not Found"
txtFemail.Text = ""
txtFmobile.Text = ""
txtFSname.Text = ""
txtFFname.Text = ""
txtFdob.Text = ""
End If
End Using
End Sub
为什么ddlgender显示为空并且在检查下拉列表时 - - 选择性别---已删除。
why ddlgender showing empty and when checking dropdown ---Select Gender--- is removed.
当SQL性别列为NULL时发生。
its happen when SQL gender column is NULL.
请帮助.....
推荐答案
You can post to the ASP.NET forum for help.
这篇关于如何在asp.net网站上用dropdownlist绑定sql表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!