473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DropDownList selected values

Hi

I'm really confused about the behaviour of a drop down list that is
populated from a database table.
The problem that i was getting was the value (selected from my dropdown
list) was always being added to the database as '0' no matter what item was
selected.
To try and debug i wrote the selected value to a label at the onclick event
but i just get the 'object reference not set to an instance of an object'
error message. What does this error mean in this case?

My code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then

SqlDataAdapter1.Fill(DataSet1)

DropDownList1.DataSource = DataSet1

DropDownList1.DataTextField = "typeDescription"

DropDownList1.DataValueField = "typeCode"

DropDownList1.DataBind()

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Label1.Text = DropDownList1.SelectedItem.Text

Label2.Text = DropDownList1.SelectedItem.Value

End Sub

The error occurs at the point where it attempts to write to the first label.

This code is as simple as it gets but i still get the error. What is going
on?

Can you also tell me why this error occurs so often and without any
explanation? I find it really frustrating because i see this error as
meaning 'there is an error and i'm not going to tell you what it is'. Any
help in how to diagnose this common error would be much appreciated.
Thanks,

Dylan
Nov 19 '05 #1
1 1055
If i remove the 'if not postback' condition then the top item in the list is
always selected.

1. it is not maintaining viewstate
2. it is attempting to read the selected value on my onclick event after
posting back

what am i doing wrong?

"Dylan Mines" <dy********@thecrmbusiness.com> wrote in message
news:Os*************@TK2MSFTNGP10.phx.gbl...
Hi

I'm really confused about the behaviour of a drop down list that is
populated from a database table.
The problem that i was getting was the value (selected from my dropdown
list) was always being added to the database as '0' no matter what item
was selected.
To try and debug i wrote the selected value to a label at the onclick
event but i just get the 'object reference not set to an instance of an
object' error message. What does this error mean in this case?

My code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then

SqlDataAdapter1.Fill(DataSet1)

DropDownList1.DataSource = DataSet1

DropDownList1.DataTextField = "typeDescription"

DropDownList1.DataValueField = "typeCode"

DropDownList1.DataBind()

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Label1.Text = DropDownList1.SelectedItem.Text

Label2.Text = DropDownList1.SelectedItem.Value

End Sub

The error occurs at the point where it attempts to write to the first
label.

This code is as simple as it gets but i still get the error. What is
going on?

Can you also tell me why this error occurs so often and without any
explanation? I find it really frustrating because i see this error as
meaning 'there is an error and i'm not going to tell you what it is'. Any
help in how to diagnose this common error would be much appreciated.
Thanks,

Dylan

Nov 19 '05 #2

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

Similar topics

1
by: DC Gringo | last post by:
I'm having such a problem with this DropDownList in a user control that is posting back and throwing an error: System.Web.HttpException: A DropDownList cannot have multiple items selected ...
5
by: DC Gringo | last post by:
I have a dropdownlist that, upon form submission, I'd like to maintain the selected value when I get my result...how do I do that? <asp:dropdownlist Font-Size="8" id="ddlCommunities"...
6
by: Julius Fenata | last post by:
Dear all, I have created client-side scripting to trigger event onChange from code-behind, like this: DropDownList1.Attributes = "GenerateArticleID()"; At the script on Windows Form, I...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
1
by: Frank | last post by:
Hello, I have some text boxes and 2 drop down lists (DDL) within a panel like so: Everything updates fine but the values from the DDLs are always the items selected by default. In other words,...
4
by: ^MisterJingo^ | last post by:
Hi all, I have a web page which has a single dropdownlist containing 3 items. Below the dropdownlist are two listboxes. Depending on the option selected from the dropdownlist, the left most...
2
by: =?Utf-8?B?VmljdG9yaW91czE=?= | last post by:
When the editcommand is execured for my datagrid the dropdown list appears for the bound item status. However, the item selected by default does not match the unedited data. How do I make the...
5
by: =?Utf-8?B?Q2hyaXMgRy4=?= | last post by:
I'm trying to put conditional code in a dropdown that would check the value of the current ListItem and set Selected=true depending on that value, and Selected=false if it's any other value. Due...
1
by: kageyone | last post by:
I have a gridview in .net 2.0 with a dropdownlist of two items - that being "active" and "inactive". <EditItemTemplate> <asp:DropDownList ID="ddlStatus" runat="server"...
1
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have a page that displays a DropDownList control that allows a Manager Name to be selected from a listing of Managers for a User. So, on my main user.aspx page, I include a DropDownList control...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.