Saturday, March 24, 2012
populate one dropdownlist based on the selected of another dropdownlist
i am using c# asp.net
i have one datagrid in my form and i bound two dropdown list using coding. i loaded my database values in my first dropdown list by using this code:
private void Page_Load(object sender, System.EventArgs e)
{// Put user code to initialize the page here
if(!Page.IsPostBack)
{ BindData();
PopulateList();
}
}
public void BindData()
{
SqlDataAdapter ad = new SqlDataAdapter("SELECT Course_NAME FROM JTSISControlManager_Course",conn);
DataSet ds = new DataSet();
ad.Fill(ds,"JTSISControlManager_Course");
DataGrid2.DataSource = ds;
DataGrid2.DataBind();
}
public DataSet PopulateList()
{
SqlDataAdapter ad = new
SqlDataAdapter("SELECT Course_Name FROM JTSISControlManager_Course", conn);
DataSet ds = new DataSet();
ad.Fill(ds,"JTSISControlManager_Course");
return ds;
}
here is the html coding:
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid2" style="Z-INDEX: 101; LEFT: 328px; POSITION: absolute; TOP: 64px"
runat="server" Width="408px" AutoGenerateColumns="False" DataKeyField="Course_NAME">
<Columns>
<asp:BoundColumn DataField="Course_NAME" HeaderText="Course_NAME" />
<asp:TemplateColumn>
<HeaderTemplate>
<aspropDownList ID="Dropdownlist1" Runat="server" DataTextField="Course_NAME" OnSelectedIndexChanged ="DropDown_SelectedIndexChanged" DataSource =" <%#PopulateList()%> " AutoPostBack="True" />
<aspropDownList ID="Dropdownlist3" Runat="server" DataTextField="Dept_NAME" AutoPostBack="True" />
</HeaderTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></form>
then using the following code i retreived my first dropdownlist value
protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
li=list.SelectedValue.Trim();
Response.Write(li);
}
my need is i want to use the retreived value of the first dropdownlist in query and fill the values in my second dropdownlist of the datagrid.
tell me how to use that retreived value of first dropdown list in next qurey and how to fill the second dropdownlist .so please help me to do this. give example coding to do my requirement
Edit/Delete MessageHi karthikeyan,
In my application I've Make and Model dropdowns. Model dropdown gets populated as soon as we select a make in a dropdown, it gets all the models for that make;
Here is my code;
Populating Model Dropdown
private void ddlMake_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (ddlMake.SelectedValue != "")
{
PopulateFieldsInDropDowns("SELECT cat.DisplayText Model, to_char(cat.CATEGORYID) ModelID FROM TDCATEGORIES cat WHERE cat.CATEGORYLEVEL=4 and parentid= " + ddlMake.SelectedValue + " ORDER BY cat.DisplayText", ddlModel);
}
else
ddlModel.Items.Clear();
}
Populate Method
private void PopulateFieldsInDropDowns(string sqlQuery, DropDownList drpList)
{
try {
conn.Open();
OleDbCommand cmCommand = new OleDbCommand(sqlQuery, conn);
OleDbDataReader dreader = cmCommand.ExecuteReader();
drpList.Items.Clear();
drpList.Items.Add(new ListItem("", ""));
while (dreader.Read())
{
drpList.Items.Add(new ListItem(dreader.GetString(0), dreader.GetString(1)));
}
dreader.Close();
if (dreader != null)
dreader = null;
if (cmCommand != null)
cmCommand.Dispose();
}
catch
{
drpList.Items.Clear();
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Cannot Get Data: Cannot Get Required Data.";
}
conn.Close();
}
It should rock.
thanks,
hi ahmarshi,
i tried which u given, but i got error:
System.NullReferenceException: Object reference not set to an instance of an object.
my first dropdown name is DropDownList1 and second name is ddlmodel
i modified my coding like this:
protected void DropDown_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList list = (DropDownList)sender;
li=list.SelectedValue.Trim();
Response.Write(li);
if (li != "")
{
PopulateFieldsInDropDowns("SELECT Dept_NAME from JTSISControlManager_Department where Course_ID in (select Course_ID from JTSISControlManager_Course where Course_NAME= '" + li + "')", ddlmodel);
}
else
ddlmodel.Items.Clear();
}
private void PopulateFieldsInDropDowns(string sqlQuery, DropDownList drpList)
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlQuery, conn);
SqlDataReader dreader =cmd.ExecuteReader();
drpList.Items.Clear();
drpList.Items.Add(new ListItem("", ""));
while (dreader.Read())
{
drpList.Items.Add(new ListItem(dreader.GetString(0), dreader.GetString(1)));
}
dreader.Close();
if (dreader != null)
dreader = null;
if (cmd!= null)
cmd.Dispose();
}
catch
{
drpList.Items.Clear();
//lblMessage.ForeColor = System.Drawing.Color.Red;
Response.Write ( "Cannot Get Data: Cannot Get Required Data.");
}
conn.Close();
}
i kept break point and run this but while comming to
this line
private void PopulateFieldsInDropDowns(string sqlQuery, System.Web.UI.WebControls.DropDownList drpList)
in the drplist comes <undefined value>.
then after comming to this line
drpList.Items.Add(new ListItem("", ""));
it automatically goes to catch.please tell me and give sugestion to rectify this prooblem.
Wednesday, March 21, 2012
Populating a Drop Down List using a SQL Stored Procedure
I posted this question yesterday but I do not see it in the list, and I cannot search and locate it.
I am rather new to coding in ASP.NET. However, I have been coding with Classic ASP for many years. Please be patient with me and my seemingly simple questions
I am in need of some help with Populating a Drop Down List is ASp.NET using C#. I want to be able to populate a drop down list with the ID as the value and the Text as the text shown by calling a SQL stored procedure. If possible, can you give me an example and comment what the steps are.
After much searching yesterday, I found a good example using access. But this is not what I needed. However, it did get me started with the concept.
And another question. How would I code to see the value choosen? My web forms contain mostly drop down list for items such as: state names, postal codes, area codes, dates, times, etc... Just about anything that I can use a DDL, I use it. Saves so much in the error checking.
Thank you in advance for your time.
Andrew
SQL DBATheres a few steps involved in getting data to be present on a page.
First for your application you'll need to create a DataReader object to interact with your SQLCommand which is your stored procedure. (research on how to implement a DataReader)
Then once you have data in your DataReader, you can bind it do the DDL like so:
ddl.DataSource = YourDataReaderName
ddl.DataValueField = "FieldNameofYourChoice" from your DataReader
ddl.DataTextFiled = "FiledNameofYourchoice" from your DataReader
ddl.DataBind
Basically that's it
Thank you
I was hoping to get more of a step by step instruction.
I am having alot of trouble making the database connection. I am seeing so many different types and ways of doing the same thing, that it is getting very confusing. Some inside the aspx page and some in the cs page. I would like to use the code behind cs page.
Any sites out there tell how to do that? And give good examples.
I used to use that same signature many years ago.
Thanks
Andrew