我如何...从db获取值到下拉列表 [英] How do I...get a value from db to dropdownlist

查看:78
本文介绍了我如何...从db获取值到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string username = gdvList.DataKeys[e.NewEditIndex].Values["fname"].ToString();
        sqlcon.Open();
        SqlCommand cmd = new SqlCommand("select * from tbl_DocReg where fname='" + username + "'", sqlcon);
        DataTable dt = new DataTable();
        SqlDataReader dr = cmd.ExecuteReader();
        ddlstate.DataTextField = "state";
        ddlstate.DataValueField = "StateId";
        if (dt.Rows.Count > 0)
            {
                ddlstate.DataSource = dt;
                ddlstate.DataTextField = "state";
                ddlstate.DataValueField = "StateId";
                ddlstate.DataBind();
            }
}

推荐答案

试试这个:

Try This:
string username = gdvList.DataKeys[e.NewEditIndex].Values["fname"].ToString();
SqlDataAdapter dAdapter=new SqlDataAdapter("select * from tbl_DocReg where fname='" + username + "'", sqlcon);
DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
if (dTable.Rows.Count > 0)
{
ddlstate.DataSource=dTable;
ddlstate.DataTextField = "state";
ddlstate.DataValueField = "StateId";
ddlstate.DataBind();
}


C#

using System.Data.SqlClient;
In our Web.config, we declare the connections string:

XHTML

<appsettings>
<add key="ConnString" value="Data Source=CLIENT-TASK2\SQLEXPRESS;Initial Catalog=BasicDataAccess;Integrated Security=True" />
</appsettings>

The ASPX page will look something like this:

ASP
<form id="form1" runat="server">
<div align="center">
<table><tr><th>Name:</th><td><asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
</asp:dropdownlist></td></tr>
<tr><th>City:</th><td><asp:dropdownlist id="DropDownList2" runat="server" xmlns:asp="#unknown">
</asp:dropdownlist></td></tr></table>
</div>
</form>

IN the code-behind, we can create two methods and then call them on page load:

C#

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Populate1();
Populate2();
}

public void Populate1()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM [tblOne]", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();

SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();

DropDownList1.DataSource = ddlValues;
DropDownList1.DataValueField = "theName";
DropDownList1.DataTextField = "theName";
DropDownList1.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}

public void Populate2()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM [tblOne]", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();

SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();

DropDownList2.DataSource = ddlValues;
DropDownList2.DataValueField = "theCity";
DropDownList2.DataTextField = "theCity";
DropDownList2.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}
}


这篇关于我如何...从db获取值到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆