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>
</p>
</form>
</body>
</html>
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>
</p>
</form>
</body>
</html>