473,471 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dropdownlist populates Datagrid

Hi, I'm new to asp.net and c#, so this is probably an easy question for
everybody.

I want to have a dropdownlist (populated by states), when the user selects a
different value, populate a datagrid based on the dropdownlist's value.

I'm not quite sure what I'm missing here. Please advise.

THANKS!
Here's my code:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BindData("WHERE StateCode = 'CA' ");
}
} //page load

private void BindData( string strState)
{
fillStates();
getAbbreviations( strState );
}

private void fillStatees()
{
sqlDataAdapter1.Fill(dsStates1);
DataTable dataTable = dsStates1.Tables[0];
ddListState.DataTextField = dataTable.Columns["StateName"].ToString();
ddListState.DataValueField = dataTable.Columns["StateCode"].ToString();
ddListState.DataBind();
}

private void getAbbreviations( string strState )
{
string conn = "Data Source=myServer;Initial Catalog=myDB;Integrated
Security=SSPI;";

string cmd;

cmd = cmd + "SELECT ID, Name from Client ";

SqlDataAdapter dataAdapter = new SqlDataAdapter( cmd, conn );
DataSet dataSet = new DataSet();

dataAdapter.Fill(dataSet);
DataTable dataTable = dataSet.Tables[0];

dAbbreviations.DataSource = dataTable;
dAbbreviations.DataBind();
}

private void ddListState_SelectedIndexChanged(object sender,
System.EventArgs e)
{
string strState = "WHERE StateCode = '" +
ddListState.SelectedItem.Value + "' ";
BindData(strState);
}
Nov 19 '05 #1
1 941
Sorry, I edited my code too quickly... this is the correct code.

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BindData("WHERE StateCode = 'CA' ");
}
} //page load

private void BindData( string strState)
{
fillStates();
getAbbreviations( strState );
}

private void fillStates()
{
sqlDataAdapter1.Fill(dsStates1);
DataTable dataTable = dsStates1.Tables[0];
ddListState.DataTextField = dataTable.Columns["StateName"].ToString();
ddListState.DataValueField = dataTable.Columns["StateCode"].ToString();
ddListState.DataBind();
}

private void getAbbreviations( string strState )
{
string conn = "Data Source=myServer;Initial Catalog=myDB;Integrated
Security=SSPI;";

string cmd;

cmd = "SELECT ID, Name ";
cmd = cmd + "FROM Client ";
cmd = cmd + strBranch;
cmd = cmd + "ORDER BY Name;";

SqlDataAdapter dataAdapter = new SqlDataAdapter( cmd, conn );
DataSet dataSet = new DataSet();

dataAdapter.Fill(dataSet);
DataTable dataTable = dataSet.Tables[0];

dAbbreviations.DataSource = dataTable;
dAbbreviations.DataBind();
}

private void ddListState_SelectedIndexChanged(object sender,
System.EventArgs e)
{
string strState = "WHERE StateCode = '" +
ddListState.SelectedItem.Value + "' ";
BindData(strState);
}
Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shane O. Pinnell | last post by:
I am sure this has come up before, but I haven't been able to find an answer as of yet. That said, any help is definitely appreciated! I have a datagrid populated from a dataset. I have a...
1
by: Mark | last post by:
I'm using dropdownlist in a datagrid to allow a column in a table to be edited. The dropdownlist contains several the from "dictionary/ code-decode" table. These columns are NOT required in the...
2
by: Stephan Bour | last post by:
Hi, Ičve tried to implement a solution to populate a dropdownlist inside an editable datagrid. The code is simple enough and an example can be found here:...
4
by: Iain Kirk | last post by:
Being a bit of a novice any help or pointers would be appreciated. I am building a web page that populates a DropDowList from a table in SQL, for testing purposes i have a simple page that...
3
by: Tim::.. | last post by:
Can someone tell me how I get the correct selected office in my drop down list when I enter edit mode. At the moment it just defaults to the office at the top of the drop down list rather than...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
1
by: Michael Kolias | last post by:
Hi everybody, I am having a problem getting the selected value of a drop down list that is populated dynamically inside a datagrid control. When I try to access the selected item on the...
3
by: Tim::.. | last post by:
Hi... Can someone please tell me what I'm doing wrong here... I have a dropdownmenu populated from a database! The idea is that a user selects an option from the dropdownlist which then loads...
0
by: bh | last post by:
I'm trying to edit data in a datagrid through a dropdown list with different id/text values. The problem comes in when I click the edit button & nothing appears to be happening. Did I do...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.