Showing posts with label selected. Show all posts
Showing posts with label selected. Show all posts

Monday, March 26, 2012

Populate a DropDownList

I need to populate a dropdownlist with items form one SQL Table in SqlDataSource1 but the selected value and the data text need to match the ones that are already into another SQL Table in SqlDataSource2.

Any Sugestions?

You can manually compare the data between the tables in codebehind using the classes found in System.Data.SqlClient & System.Data.Sql. Then you can use the DropDownList.Items.Add(new ListItem()) methods to add new items to your dropdownlist.

Saturday, March 24, 2012

populate one dropdownlist based on the selected of another dropdownlist

hi
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.

Populate textbox from item selected in a drop down list

I am a newbie to .net so bear with me here.

I have a page that has a dropdown list of customers. I want to select a customer from that dropdown and populate the appropriate textboxes.

So when I select 'John Smith' from the dropdown list I have a form with textboxes for first name, last name, accout id, etc that needs to populate accordingly. I am using VB.net with MS SQL. I have been successful populating the dropdown list from the DB. Where I am lost is populating the appropriate textboxes for the customer selected in the dropdown list. Any help is appreciated.Hi,

If you are having the dropdownlist and the textbox in the same form, then you need to do as below:-

Set the autopostback property for the dropdownlist to true. This will make the dropdownlist post back to the server, once an element is selected. Then, you can put the following code in your codebehind file to get the selected item.

If Not IsPostBack Then

textbox1.text = dropdownlist1.SelectedItem.Text

End If

If you are having the textbox in a different form, then you need to pass the selected value by querystring to that form.

If Not IsPostBack Then

Response.Redirect("NewForm.aspx?name=" & dropdownlist1.SelectedItem.Text)

End If

Then in the new form, you can get the value using the following code:-

textbox1.text = Request.Params("name")

Hope it helps.
Something to keep in mind:

The name of the user (or whatever) may not be unique. You can have two people with the name "John Smith." You propably have a column in your DB that is unique for each person like UserID or whatever.

So when you update the dropdownlist from the database, you need to retrieve the unique id for each person as well, so that when the index of the selected index in the drop down list changes, you can use the corresponding unique ID to retrieve the other info from the database.

I cant remember exactly, but you should take a loot at the dropdown control itself. Many such controls have a 'Value' for each item in them. You van set the 'Value' to the unique ID somehow.

Youll have to take a look...

Wednesday, March 21, 2012

populate value in dropdown

Hi, I have an update form which contains the selected employee information. I was able to use DataReader to retrieve the data from the database. I want to place the department value in the dropdown which contains other department values so user can change the dept value and save the information. How can I display my department value in the dropdown? I was able to populate the dropdown without any problem except the department value wasn't highlighted. It is like a person originally worked at Marketing department and is going to switch to IT.

Thanks in advance.

<asp:DropDownListID="DDDept"runat="server"AutoPostBack="True" DataSourceID="ObjectDataSource1"DataTextField="DeptName"DataValueField="DeptID"Style="z-index: 112; left: 372px;position: absolute; top: 415px"Width="159px"></asp:DropDownList>

SO you have your datasource all set up and the drop down list displays the departments correctly. However you want the users current department to be selected..

use the SelectedValue Property of the drop down list and set it to whatever key field in the users table that has the department id in it.

<asp:DropDownList SelectedValue='<# Bind("DepartmentIdFieldFromUsersTable") %>' .../>

should do it.

hth,

mcm


Hi, Thank you so much for the help!! I can see the department correctly now !!