Tuesday, August 18, 2009

Binding Data to Dropdownlist from Database

Binding Data to Dropdownlist from DataBase
---------------------------------------------------------------
Today Iam going to write on Binding Data to Dropdownlist from Database

Consider that we want to populate the Dropdownlist with a list of countries. For this first we need to create a new .aspx page with the following code

<asp:dropdownlist id =" “dpdcountry”" runat="”server”">
</dropdownlist>

Now create a new classlibrary with the class name DataBinding.cs and write the following code

public class Databinding
{
public static DataTable Execute(string sqlstring)
{
SqlConnection con = new SqlConnection(connectionstring);
DataTable dt = new DataTable(“tb1”);
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlstring, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt) ;
}
catch(Exception e)
{
throw e;
}
finally
{
con.Close();
}


return dt;
}
}


In the above code Databinding is the class name, Execute is the method which returns a datatable. In the connectionstring you need to specify the 4 parameters of the SqlConnection string.

Now in the .aspx.cs file under Page_Load method you need to write the following the code

If(!IsPostBack)
{
string str = “select country_id , name from Country”;
Datatable dt = Databinding.Execute(str);
dpdcountry.DataValueField = dt.columns[0].ToString();
dpacountry.DataTextField = dt.cloumns[1].ToString();
dpdcountry.DataSource = dt;
dpdcountry.DataBind();
}



On a whole, first we declared a dropdownlist with the name dpdcountry in the .aspx page. Then in .aspx.cs page, in the Page_Load method we are sending the sql string to the class Databinding which connects to the database and returns the data table containg the country names. Then this data table is assigned to the dropdownlist as its datasource. So, when you run this code you get a page with the dropdownlist populated with country names.
Note:
1. You should add the Class Library name in the Referrences of the Project
2. You should use the Class Library name in the directories part of the .aspx.cs page

9 comments:

masmasika said...

This is a great post. Keep posting and thanks for sharing this to the readers.

Kraxpelax said...

Typically Swedish Next Door Girls Collection För Your LAPTOP!

:o)

My Babe Misogyn Wallpapers:

http://screenfonds.blogspot.com/

And my Stockholm photographies:

http://holmstock.blogspot.com/

And my English poetry:

http://singleswingle.blogspot.com/

And my tentative Spanish poetry:

http://hollb.blogspot.com/

Feel free to announce your blog on mine.

- Peter Ingestad, Sweden

Techno ganesh said...

Good Collection for SQL Materials,
dotnettrainingchennai

Emi Jackson said...

Thanks for your wonderful post.It is really very helpful for us and I have gathered some important information from this blog.If anyone wants to get Dot Net Training in Chennai reach FITA, rated as No.1 Dot Net Training Institute in Chennai.

john son said...

Java Training

Hi I am Johnson lives in Chennai. I am a technology freak. Recently I did Java Training in Chennai at a leading Java Training Institutes in Chennai. This is really helpful for me to make a bright career in IT industry.

Best Java Training in Chennai

Java Training Chennai


rebeka christy said...

Android Training in Velachery

Your blog is really useful for me. Thanks for sharing this useful blog..Suppose if anyone interested to learn Android Training in Chennai please visit fita academy which offers best Android Training Chennai at reasonable cost.

Android Training Institutes in Chennai





Emi Jackson said...

Unix Course Chennai

Thanks for sharing this informative blog. Suppose if anyone interested to learn Unix Training Chennai, Please visit Fita Academy located at Chennai, Velachery. Rated as No.1 Unix Training Centers in Chennai


Regards....

Best Unix Training in Chennai





Victoria John said...

HTML5 Training in Chennai

Your blog is really awesome. Thank you for your sharing this informative blog. Recently I did PHP course at a leading academy. If you are looking for best PHP Training Institute in Chennai visit FITA IT training academy which offer real time PHP Training in Chennai.

PHP Course in Chennai

dhanamlakshmi palu said...

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing. PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai