Click here to Skip to main content
15,121,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to populate a dropdownlist dymnamically.I am using mysql database and a linq query to retrieve data from the table
Am not not getting how to populate them from table.I am trying to filter the data basing on this selection
My view page contains dhtmlx form and i am trying to retrieve data using ajax functionality
Please provide me samples on how to do this

The DropdownList method on the html helper has an override for doing this:

<![CDATA[<%=Html.DropDownList("CategoryID", (SelectList) ViewData["Categories"], "--Select One--") %>]]>

This is take from a post at this URL: It's about halfway down.[^]
Hi, Please find the code snippet to bind the dropdownlist dynamically using storedprocedure (Ajax and jquery).I have not used Linq but I have used Sql server database.Hope this will help.

Aspx Page:
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MachineCode.aspx.cs" Inherits="SampleApp.MachineCode" %>

<html xmlns="">
<head runat="server">
<script src=""></script>
<script type="text/javascript">
$(document).ready(function () {
type: "POST",
contentType: "application/json;charset=utf-8",
//URL is the path of our Ajax webmethod (pagename/functionname
url: "MachineCode.aspx/PopulateDropDownList",
data: "{}",
dataType: "json",
//Called the jquery ajax call success
success: function (result) {
$("#ddlStates").append("<option value="0">--Select--</option>");
$.each(result.d, function (key, value) {
//called on jquery ajax call failure
error: function ajaxError(result) {
alert(result.status + ':' + result.statusText)
<form id="form1" runat="server">

Populate a dropdown Dynamically via stored procedure
States Name:
<asp:dropdownlist id="ddlStates" runat="server" width="160px" xmlns:asp="#unknown">

<asp:label id="dbErrorLabel" runat="server" visible="False" xmlns:asp="#unknown">Label
</form>"server" Visible="False">Label

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Configuration;

namespace SampleApp
public partial class MachineCode : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

public class States
public string State { get; set; }
public string Country { get; set; }


public static List<states> PopulateDropDownList()
DataTable dt = new DataTable();
List<states> objStates = new List<states>();

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Name_Of_Your_SP"].ConnectionString))
using (SqlCommand cmd = new SqlCommand("GeographyLocations", con))
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (dt.Rows.Count > 0)
for (int i = 0; i < dt.Rows.Count; i++)
objStates.Add(new States
State = dt.Rows[i]["States"].ToString(),
//Country = dt.Rows[i]["Countries"].ToString()
return objStates;

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