How to bind data to Dropdownlist

Posted by Dhivakar under Regular Expressions on 2/9/2011 | Points: 10 | Views : 11062 | Status : [Member] | Replies : 6
hi guys..
i want to loa the records from database to dropdown list when at the time of page load...
how to do this?...
post me the sample code....


thanks...

Dhivakar.M


Responses

Posted by: Karthikanbarasan on: 2/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

try the below code

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();
}

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();
}

}


Thanks
Karthik
www.f5Debug.net

Dhivakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Also check this link for a straight forward method

http://asp-net-example.blogspot.com/2009/03/how-to-data-bind-dropdownlist-on-page.html

Thanks
Karthik
www.f5Debug.net

Dhivakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 2/9/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Dhivakar,


it's very easy you fill your dropdown Dynamically . Add dropdown in ur page . Set Data source then set Text-field & Value Field.

Dropdown Automatically fill on Page load !

Best Regard's
Prabhakar

Dhivakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhivakar on: 2/9/2011 [Member] Starter | Points: 25

Up
0
Down
it shows following error...


DataBinding: 'System.Char' does not contain a property with the name 'dealer_level'

Dhivakar.M

Dhivakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 2/9/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Dhivakar,

You run the page then Error is generated . . what to u do fully Described in Step wise step . .

Best Regard's
Prabhakar

Dhivakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Which place its showing this error?

Thanks
Karthik
www.f5Debug.net

Dhivakar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response