Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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();
            }
}
Posted
Updated 24-May-15 21:42pm
v2

Try This:
C#
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();
}
 
Share this answer
 
v2
XML
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();
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900