listbox value not captured:

Seakannorie

New Member
I am new to asp.net, and I am trying to accomplish the simple task of capturing a selected value from a listbox and using it in a sql query. However, the value of the listbox resets itself to null each time the page is posted. I get this error message:<BR>"Value null was found where an instance of an object was required."<BR><BR>Does anyone know why this is happening? I'm sure it's something simple, but I haven't found any answers online yet.<BR>Thanks,<BR>Deborah<BR><BR>Here is my code:<BR><%@ Import Namespace = "System.Data" %><BR><%@ Import Namespace = "System.Data.SqlClient" %><BR><BR><BR><%@ Page Language="vb" Debug="true" %><BR><BR><script runat="server"><BR><BR><BR><BR> 'Create State dropdown picklist<BR><BR> Sub page_load(sender as object, e as eventargs)<BR> <BR> Dim MaestroConnection as SqlConnection<BR><BR> MaestroConnection = New SqlConnection( myconnectioninfohere ) <BR> <BR><BR> dim selectpicklistcommand as sqlcommand<BR> DIM LBLSQLTEXT AS OBJECT<BR> LBLSQLTEXT = SELECTPICKLISTCOMMAND<BR> <BR> MaestroConnection.Open() <BR><BR> selectpicklistcommand = new sqlcommand("select StateName,statecode from states", MaestroConnection)<BR><BR> statevalue.datasource = selectpicklistcommand.executereader()<BR> statevalue.datatextfield= "statename"<BR> statevalue.datavaluefield= "statecode"<BR> statevalue.databind()<BR> <BR> 'statevalue.Items.Insert(0, new ListItem("TEST"))<BR><BR> MaestroConnection.Close()<BR> <BR> End Sub<BR><BR> Sub cmdSearch_Click(sender as object, e as eventargs)<BR> ' Create an SQL statement that uses search criteria entered by user<BR> <BR> If statevalue.SelectedIndex > - 1 Then<BR> Label1.Text = "You chose: " & statevalue.SelectedItem.Text<BR><BR> end if<BR> <BR> Dim MaestroConnection as SqlConnection<BR><BR> if Page.IsPostBack<BR><BR> MaestroConnection = New SqlConnection( myconnection infohere ) <BR> <BR> Dim namecriteria as string<BR> namecriteria = namevalue.text<BR> <BR> <BR> Dim statecriteria as string<BR> statecriteria = statevalue.selecteditem.value<BR> <BR><BR> Dim SelectResults as string<BR> <BR> selectresults = "select distinct presentr.propername, presentr.associatedcity, presentr.associatedstate, presentr.mainphoneno from presentr where presentr.searchname like '" & chr(37) & namecriteria & chr(37) & "' and presentr.associatedstate ='"& statecriteria & "'"<BR><BR> Dim selectresultscommand as sqlcommand<BR><BR> selectresultscommand = new sqlcommand( selectresults,MaestroConnection)<BR><BR> MaestroConnection.Open()<BR><BR> resultsrepeater.datasource = selectresultscommand.executereader()<BR> resultsrepeater.databind()<BR> <BR><BR> MaestroConnection.Close()<BR><BR> <BR> end if<BR> End Sub<BR><BR><BR> <BR></script><BR><BR><BR><html><BR><body><BR> <center> <BR><BR> Contact Database<p><BR> <BR> <asp:label id="Label1" runat="server"/><BR><BR> <form runat="server" id="frmSearch"><BR> <b>Association Name</b> <BR> <asp:textbox runat="server" id="namevalue" /><BR> <b> State</b> <asp:listbox runat="server" id="statevalue" rows="1" selectionmode="single"/><BR> <BR><BR> <asp:button runat="server" id="btnsubmit" text="Search"<BR> OnClick="cmdSearch_Click" /><BR><BR> </center><BR><BR><BR> <p><BR> <asp:repeater id="resultsrepeater" runat="server"><BR> <itemtemplate><BR> <hr><BR> <%# container.dataitem("propername") %> <BR><BR> <%# container.dataitem("associatedcity") %>, <BR> <%# container.dataitem("associatedstate") %> <BR> <%# container.dataitem("mainphoneno") %><BR> <BR> </itemtemplate><BR> </asp:repeater><BR><BR> </form><BR><BR></body><BR></html><BR><BR>you have too much code for me to read..<BR>my guess is that you must only bind the data in you select box once. <BR><BR>Sub Page_Load<BR> If Not IsPostBack Then<BR> myselect_DataBind()<BR> End If<BR>end sub<BR><BR>also, <BR>try this -> SelectedItem.ToString <BR>instead of this -> SelectedItem.Text<BR><BR><BR><BR>Oops - I was re-posting data to the select box before capturing the info.<BR><BR>Thanks very much!
 
Back
Top