Solved

How do you set a textbox equal to a listbox selected item?

Posted on 2004-08-26
7
382 Views
Last Modified: 2010-04-17
How do you set a textbox equal to a listbox selected item?

Have tried the following;

lblDisplay.Text = lstQuestions.SelectedItem.Value
lblDisplay.Text = lstQuestions.SelectedItem.Text

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

I dont know what the problem is here.

Here is the listbox control properties;

<asp:listbox AutoPostBack="true" DataTextField="QuestionID" DataValueField="QuestionID" Height="500" ID="lstQuestions" runat="server" SelectionMode="single" Width="70" OnSelectedIndexChanged="Testing"></asp:listbox>

Sub Testing(ByVal sender As System.Object, ByVal e As System.EventArgs)
lblDisplay.Text = lstQuestions.SelectedItem.Value
End Sub

The listbox just contains a list of numbers.

Cheers

PG
0
Comment
Question by:pgilfeather
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:FarhanaJabbar
ID: 11900800
make a web form
 declare controls as follow  in HTML view of form.

<asp:listbox AutoPostBack="true" Height="500" ID="lstQuestions" runat="server"  SelectionMode="single" Width="248px"></asp:listbox>
<asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 278px; POSITION: absolute; TOP: 21px"
 runat="server" Width="310px">Label</asp:Label>


and write this code in your code behind class as

Imports System.Data.SqlClient  

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        objConnection = New SqlClient.SqlConnection
        objConnection.ConnectionString = "data source=et12;initial catalog=TestDB;user id=sa; password="
        objConnection.Open()
        Dim sql = "select * from Questions"
        Dim tbl As DataTable = GetDataFromQuery(sql)

        If (Not Page.IsPostBack) Then
            Me.lstQuestions.DataMember = "Table1"
            Me.lstQuestions.DataTextField = "Question"
            Me.lstQuestions.DataValueField = "Question_ID"
            Me.lstQuestions.DataSource = tbl
            Me.lstQuestions.DataBind()

            If (Me.lstQuestions.Items.Count > 0) Then
                Me.lstQuestions.SelectedIndex = 0
                Me.lblDisplay.Text = Me.lstQuestions.SelectedItem.Value
            End If
        End If
    End Sub
    Public Function GetDataFromQuery(ByVal SQLQuery As String) As System.Data.DataTable
        Dim objCommand As SqlCommand
        Dim objDataAdapter As New SqlDataAdapter

        Dim Query As String
        Query = SQLQuery
        objCommand = New SqlCommand(Query, objConnection)
        objCommand.CommandType = CommandType.Text

        objDataAdapter.SelectCommand = objCommand

        Dim dtTemp As New DataTable("Table1")
        objDataAdapter.Fill(dsTemp)

        Return dtTemp
    End Function

    Private Sub lstQuestions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstQuestions.SelectedIndexChanged
        Me.lblDisplay.Text = Me.lstQuestions.SelectedItem.Value
    End Sub



got it?

0
 

Author Comment

by:pgilfeather
ID: 11901611
So where was my error?

I am working in dreamweaver, not visual studio
0
 
LVL 2

Expert Comment

by:dimitry_dimitry
ID: 11910472
use SelectedValue!
That helped me in better days;)
0
 
LVL 2

Accepted Solution

by:
dhananjayad earned 500 total points
ID: 12013020
Hi,

As you may be aware that there are two values associated with a list control. They are:
1. Text -  this is the value that is shown to the user.
2. Value - this value is associated with the element, but it is not shown. But can be referred to from within the program.

For example, if you want to show a list of users and want to select thier email address on selecting a particular user, you can assign the names to the text nd emails to value. So the given two options - SelectedItem.Value and SelectedItem.Text are to select these two components. That's the difference.

your problem is one of invalid object instatiation. One of the referenced objects are not instatiated properly. So the problem could be either:
* The label does not exist.
* The list box does not have values.
* The method is not configured to handle the event.

I suggest you try the following:
*Make sure that all elements are there
*The list box is populated.
*The sub is configured to handle the item selection event as in Private Sub lstQuestions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstQuestions.SelectedIndexChanged


Hope this helps you.

Cheers,
DhananjayaD
0
 
LVL 3

Expert Comment

by:Statick001
ID: 12073044
dont use selecteditem.value or selecteditem.text

Listbox.Text will give you the text of the selected item
Listbox.SelectedValue will give you its underlying value (if it has one)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question