Filling a Dropdownlist dynamically, with Validation Control Enforced

Hi,

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.

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

Hope it helps.

Print | posted on Monday, April 25, 2005 7:31 AM

Comments on this post

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
It works fine but i'm have a little problem with the first record in database, Datareader skips it. Can you tell me why?

thx
Marc
Left by Marc on Jun 27, 2005 3:07 PM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
Thanks! I'd like to be able to add values using a child form (showmodaldialog), edit values, and delete values... How do I pass values from the child to the parent?
Left by Eve on Aug 11, 2005 12:53 PM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
Thanks, i was searching for exactly this issue Validating my Dropdownlist, and i found the best solution.

Keep up good work.
Left by Madhu Menon on Nov 27, 2007 7:15 PM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
Its good
Left by arun on May 22, 2008 10:35 PM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
gud one
Left by aaa on Nov 17, 2008 7:45 PM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
Dim objConn As New SqlConnection(ConfigurationManager.AppSettings.Get("connstring"))
Dim command As SqlDataAdapter = New SqlDataAdapter("AlttaEncuestas_Ciudad_User_Obtener", objConn)
command.SelectCommand.CommandType = CommandType.StoredProcedure
Dim ds As DataSet = New DataSet
command.Fill(ds, "ciudad")
objConn.Close()
ddlciudad.DataSource = ds.Tables("ciudad").DefaultView
ddlciudad.DataValueField = "CiudadID"
ddlciudad.DataTextField = "CiudadNombre"
ddlciudad.DataBind()
Left by Albertoi on May 03, 2009 1:33 AM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
i was searching for exactly this issue Validating my Dropdownlist
Left by mario oyunları on Sep 22, 2009 12:05 AM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
3d animation, video editing, visual effects, 3ds max tutorials, maya institutes, game designing schools, training for web design, 2D Animation, Multimedia Institutes, Internet marketing, Web development, Graphic Designing, A Professional Multimedia training institute in india and delhi, sketching
Left by article directory on May 08, 2010 5:15 PM

# re: Filling a Dropdownlist dynamically, with Validation Control Enforced

Requesting Gravatar...
It's a valuable article one. it's more helpful for me to meet my requirements
Left by mahendran k.p on Jul 27, 2011 3:39 PM

Your comment:

 (will show your gravatar)