Good afternoon and Happy New Year to all...
I have just started the process of learning ASP.NET, and along with it
VB.NET. As a classic ASP programmer with only VBScript knowledge, I am at a loss for even the most basic of error messages. I am receiving the error "Object reference not set to an instance of an object." from the following line of code:
lblDB.Text = lstBox.SelectedItem.Value
I am trying a basic example of populating a list box with information from a MySQL DB Table. From that list I am selecting an item and having the value of that item populate a label control.
The remaining code is following. Thank you in advance for any and all help.
<%@ Page Language="
VB" Debug="true" %>
<%@ import Namespace="ByteFX.Data.MySQLClient" %>
<script runat="server">
Sub button_click (s As Object, e As EventArgs)
lblDB.Text = lstBox.SelectedItem.Value
End Sub
</script>
<html>
<head>
</head>
<body>
<%
Dim MyConString As String = "DATA SOURCE=xxx.xxx.xxx.xxx; DATABASE=aspx; UID=xxx; PASSWORD=xxx"
Dim MyConnection As New MySQLConnection(MyConString)
Dim MyCommand As New MySQLCommand("SELECT id, FName, LName FROM test")
myCommand.Connection = myConnection
MyConnection.Open()
Dim rs As MySQLDataReader
rs = MyCommand.ExecuteReader()
While rs.Read()
Dim myListItem As New ListItem()
myListItem.Value = rs("LName")
myListItem.Text = (rs("LName") & " ," & rs("FName"))
lstBox.Items.Add(myListItem)
End While
MyConnection.Close()
rs.Close()
%>
<form runat="server">
<table height="350" width="500">
<tbody>
<tr>
<td valign="top" width="50%">
<p>
<asp:ListBox id="lstBox" runat="server"></asp:ListBox>
</p>
<p>
<asp:Button id="btnSubmit" OnClick = "button_click" runat="server" Text="Submit"></asp:Button>
</p>
</td>
<td>
<asp:Label id="lblDB" runat="server">Label</asp:Label></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>