473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

asp:DropDownList problem

Hi everyone,

I am trying to select an item in my DropDownList (Everything on 1 .aspx
page) and it gives me the following error message. Has anyone encountered
this problem before? or any idea how I can resolve it? (In this case I want
to select the Canada from the country list)

SqlDataAdapter daCountry = new SqlDataAdapter("SELECT * FROM Country ORDER
BY Value", conn);
DataSet dsCountry = new DataSet();
daCountry.Fill(dsCountry, "Country");
Country.DataSource = dsCountry.Tables["Country"];
Country.DataTextField = "Value";
Country.DataValueField = "Key";
//Country.ClearSelection();
Country.Items.FindByText("Canada").Selected = true;
Country.DataBind();
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:
Line 27: Country.DataValueField = "Key";
Line 28: //Country.ClearSelection();
Line 29: Country.Items.FindByText("Canada").Selected = true;
Line 30:
Line 31: Country.DataBind();


Thank you
Maziar A.
Nov 17 '05 #1
2 1357
Try this:
Country.SelectedIndex =
Country.Items.IndexOf(Country.Items.FindByText("Ca nada"));

HTH,
Bill P.

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:wg******************@news01.bloor.is.net.cabl e.rogers.com...
Hi everyone,

I am trying to select an item in my DropDownList (Everything on 1 .aspx
page) and it gives me the following error message. Has anyone encountered
this problem before? or any idea how I can resolve it? (In this case I want to select the Canada from the country list)

SqlDataAdapter daCountry = new SqlDataAdapter("SELECT * FROM Country ORDER
BY Value", conn);
DataSet dsCountry = new DataSet();
daCountry.Fill(dsCountry, "Country");
Country.DataSource = dsCountry.Tables["Country"];
Country.DataTextField = "Value";
Country.DataValueField = "Key";
//Country.ClearSelection();
Country.Items.FindByText("Canada").Selected = true;
Country.DataBind();
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:
Line 27: Country.DataValueField = "Key";
Line 28: //Country.ClearSelection();
Line 29: Country.Items.FindByText("Canada").Selected = true;
Line 30:
Line 31: Country.DataBind();


Thank you
Maziar A.

Nov 17 '05 #2
Thanks worked :)

Maz.

"Bill Priess" <no*****@nospam.com> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
Try this:
Country.SelectedIndex =
Country.Items.IndexOf(Country.Items.FindByText("Ca nada"));

HTH,
Bill P.

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:wg******************@news01.bloor.is.net.cabl e.rogers.com...
Hi everyone,

I am trying to select an item in my DropDownList (Everything on 1 .aspx
page) and it gives me the following error message. Has anyone encountered this problem before? or any idea how I can resolve it? (In this case I

want
to select the Canada from the country list)

SqlDataAdapter daCountry = new SqlDataAdapter("SELECT * FROM Country ORDER BY Value", conn);
DataSet dsCountry = new DataSet();
daCountry.Fill(dsCountry, "Country");
Country.DataSource = dsCountry.Tables["Country"];
Country.DataTextField = "Value";
Country.DataValueField = "Key";
//Country.ClearSelection();
Country.Items.FindByText("Canada").Selected = true;
Country.DataBind();
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:
Line 27: Country.DataValueField = "Key";
Line 28: //Country.ClearSelection();
Line 29: Country.Items.FindByText("Canada").Selected = true;
Line 30:
Line 31: Country.DataBind();


Thank you
Maziar A.


Nov 17 '05 #3

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

Similar topics

1
by: Marius | last post by:
Hi all, I need an asp:dropdownlist on a webform that is databound to a dataset. This works fine and returns the data to populate my dropdownlist. In the DB I only have valid data, but I need a...
1
by: Harry | last post by:
Hi, Just seeing if anyone can help me with a problem I am having with drop down lists. - I have a asp:dropdownlist that gets its values from a database. - The user then selects a value then...
2
by: amessimon | last post by:
I need to display a drop down list which holds up to 250 listitems. I'd like to create this programmatically rather than have to hardcode it into the page. For example <asp:DropDownList...
1
by: Urmal Patel via .NET 247 | last post by:
I have problem with asp:Dropdownlist. I have a dropdownlist control in Page1.aspx and I want to assign value and Text to dropdown list form Page2.aspx from java script function. I am able to assign...
7
by: Lastie | last post by:
Hi all, I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem. I can also bind data from an SQL database fine. My problem is that I want to do both at the same...
1
by: Xuanly ly via .NET 247 | last post by:
asp:dropdownlist always display infront of everything and I can't make it display behind HELP HELP help!!!. <form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1"...
2
by: HH | last post by:
Hi, I have a dropdown list that is datafilled via a SQL table. The text part is always unique. (A list of countries) Each country is assigned one of three numbers. (This being the 'value' of...
1
by: Miguel Dias Moura | last post by:
Hello, I have two questions concerning Asp:DropDownList 1. How to fill a DropDownList from runtime and set its default item? 2. How do use a DropDownList in each datagrid record? All...
2
by: jeffmagill | last post by:
Ive stumbled into one of those infamous problems where something stopped working for apparently no reason - in this case MyDropDownList.SelectedItem.Value. Im not sure exactly which group this...
2
by: John Smith | last post by:
I have this DataGrid: <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="SM"> <ItemTemplate> <asp:Label id="Label2" runat="server"...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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 ...

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.