C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Filling a Dropdownlist dynamically, with Validation Control Enforced


Posted Date: 12 Sep 2006    Resource Type: Articles    Category: Web Applications
Author: Harish RanganathanMember Level: Gold    
Rating: Points: 10



Introduction


Many of the people new to ASP.Net would be excited about the Dropdownlist control which is very useful in appliactions. Howevever, much as it is easy to add items to the dropdownlist with static asp:ListItem tags, adding values to the dropdownlist dynamically is a bit complex and involves a little bit of coding.


Common Issues


Also, one of the common issues faced by developers is how to enforce Required Field Validator for Dropdownlist.

Herebelow is the code snippet for a Dropdownlist with validator and dynamically filling the same from Database. It uses an imaginary table tblCountry which has the fields CountryId, CountryName. The select statement is pretty straightforward and can be tweaked accordingly to one's requirement.

ASPX Code for declaring the control with Required Field Validator



<asp:dropdownlist id="ddlCountry" runat="server" DataTextField="CountryName" DataValueField="CountryId" />

<asp:requiredfieldvalidator id="CountryValidator" Runat="server" ControlToValidate="ddlCountry"
ErrorMessage="Please select your Country" InitialValue="0" />


C# Code for populating the dropdownlist dynamically from the database



public void Bind_ddlCountry()
{
SqlConnection objcon = new SqlConnection("Connectionstring");
SqlCommand objcmd;
SqlDataReader objRdr;
string selstr;
selstr = "SELECT CountryId, CountryName FROM tblCountry";
objCmd = new SqlCommand(selstr, objCon);
try
{
objCon.Open();
objRdr = objCmd.ExecuteReader();

DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("CountryName", typeof(string)));
dt.Columns.Add(new DataColumn("CountryId", typeof(string)));
dr = dt.NewRow();
dr["CountryName"] = "Choose One";
dr["CountryId"] = 0;
dt.Rows.Add(dr);
while(objRdr.Read())
{
dr = dt.NewRow();
dr["CountryName"] = objRdr["CountryName"];
dr["CountryId"] = objRdr["CountryId"];
dt.Rows.Add(dr);
}
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryId";
ddlCountry.DataBind();
objRdr.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally
{
objCon.Close();
objCon.Dispose();
objCmd.Dispose();
}
}


Summary


This is just one way to implement dynamic population of Dropdownlist and there are also alternatives.





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Getting the IE Webcontrols work in ASP.NET 2.0 while using Visual Studio 2005
Previous Resource: Error : "Unable to find script library '/aspnet_client/system-web/1-1-4322/webvalidation.js'" ...
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use