NEWBIE - Displaying Selection of Listbox Item

R

RockNRoll

Greetings again,

I have a Listbox that is populated from a SQL Statement. I am attempting to
get the value of the selected item with the following code:

Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
response.write(ListBox1.SelectedItem.value)
End Sub

I have the datatext and datavalue fields both set to the same field in my
listbox (Just a list of Job numbers) and I have autopostback set to "True."

The issue is that when I select an item from the listbox, I get an error
message stating:
Object reference not set to an instance of an object.
Below is my code in entirety for further reference if needed. Thank you.
<%@ Page Language="VB" %>
<%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI"
Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral,
PublicKeyToken=6f763c9966660626" %>
<%@ Register TagPrefix="n0" Namespace="MyListView" Assembly="MyListView" %>
<script runat="server">

Sub Page_Load (Src as Object, E As EventArgs)

Dim str_Connection as String = "server = 'XXXX'; user id = 'XX';
password = 'XX'; database = 'XXXX'"
Dim db_Connection as System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(str_Connection)

Dim str_SQLQuery as String = "SELECT [PA01101].[PACONTNUMBER] FROM
[PA01101]"
dim db_Command as System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
db_Command.CommandText = str_SQLQuery
db_Command.Connection = db_Connection

db_Connection.Open
Dim Rdr_DataReader as System.Data.IDataReader =
db_Command.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

ListBox1.DataSource = Rdr_Datareader
ListBox1.DataBind()


End Sub

Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)

response.write(ListBox1.SelectedItem.value)

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:ListBox id="ListBox1" runat="server"
DATATEXTFIELD="PACONTNUMBER" DATAVALUEFIELD="PACONTNUMBER" Height="159px"
Width="305px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AUTOPOSTBACK="True"></asp:ListBox>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
&nbsp;
</p>
</form>
</body>
</html>
 
M

Mike Newton

Wrap your databinding in Page_Load in an

If IsPostBack Then

End If

and it should work. You only need to bind the data for the first time
the page is loaded. Postbacks cause Page_Load to happen, but by then
your items are in the ViewState, and don't have to be bound again.

See, you're posting back, and re-assigning data to the object. This
wipes out anything that was posted back to the server, causing there to
be no item selected.
 
R

RockNRoll

Mike,

Thanks for your help. I'm starting to understand this stuff now.

Mike Newton said:
Wrap your databinding in Page_Load in an

If IsPostBack Then

End If

and it should work. You only need to bind the data for the first time
the page is loaded. Postbacks cause Page_Load to happen, but by then
your items are in the ViewState, and don't have to be bound again.

See, you're posting back, and re-assigning data to the object. This
wipes out anything that was posted back to the server, causing there to
be no item selected.
Greetings again,

I have a Listbox that is populated from a SQL Statement. I am attempting to
get the value of the selected item with the following code:

Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
response.write(ListBox1.SelectedItem.value)
End Sub

I have the datatext and datavalue fields both set to the same field in my
listbox (Just a list of Job numbers) and I have autopostback set to "True."

The issue is that when I select an item from the listbox, I get an error
message stating:
Object reference not set to an instance of an object.
Below is my code in entirety for further reference if needed. Thank you.
<%@ Page Language="VB" %>
<%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI"
Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral,
PublicKeyToken=6f763c9966660626" %>
<%@ Register TagPrefix="n0" Namespace="MyListView" Assembly="MyListView" %>
<script runat="server">

Sub Page_Load (Src as Object, E As EventArgs)

Dim str_Connection as String = "server = 'XXXX'; user id = 'XX';
password = 'XX'; database = 'XXXX'"
Dim db_Connection as System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(str_Connection)

Dim str_SQLQuery as String = "SELECT [PA01101].[PACONTNUMBER] FROM
[PA01101]"
dim db_Command as System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
db_Command.CommandText = str_SQLQuery
db_Command.Connection = db_Connection

db_Connection.Open
Dim Rdr_DataReader as System.Data.IDataReader =
db_Command.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

ListBox1.DataSource = Rdr_Datareader
ListBox1.DataBind()


End Sub

Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)

response.write(ListBox1.SelectedItem.value)

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:ListBox id="ListBox1" runat="server"
DATATEXTFIELD="PACONTNUMBER" DATAVALUEFIELD="PACONTNUMBER" Height="159px"
Width="305px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AUTOPOSTBACK="True"></asp:ListBox>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
&nbsp;
</p>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top