Solved

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

Posted on 2004-08-26
7
370 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to adjust sheet 1 82
scoresClump  challenge 31 108
wordlen challenge 3 49
Program to display an alert on Windows Toolbar 2 65
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now