ListItem cannot be converted to String

I have a Listbox that shows a list of applications which is being populated by the SQLdataReader:

<asp:ListBox ID="ApplicationList" runat="server" Rows="4" DataTextField="str_application_name" DataValueField="str_application_id" ></asp:ListBox>

sqlcmd  = "SELECT str_application_id, str_application_name FROM tbl_SRO_applications"
dbcomm  = New SQLCommand(sqlcmd, oSQLConn)
'Create/Populate the DataReader
dbReader = dbcomm.ExecuteReader()
ApplicationList.DataSource = dbReader
ApplicationList.DataBind()

The user selects the Application from the List and Clicks the Submit Button:

<asp:Button ID="button5" runat="server" OnClick="Submit"></asp:Button>

However I am recieving the error 'ListItem cannot be converted to type String':

Public Sub Submit(sender As Object, e As System.EventArgs)
Dim App as String
app = ApplicationList.SelectedItem

tester.Text="You selected " & ApplicationList.SelectedItem

End Sub

Ideally I would like the App variable to store the "str_application_id" field...Assistance would be greatly appreciated.

freethinker23Asked:
Who is Participating?
 
YZlatConnect With a Mentor Commented:
try this, it worked for me:

 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
        Dim sqlcmd As String
        Dim dbReader As SqlDataReader
               sqlcmd  = "SELECT str_application_id, str_application_name FROM tbl_SRO_applications"

        If Not IsPostBack Then
            'Create/Populate the DataReader
           dbcomm  = New SQLCommand(sqlcmd, oSQLConn)
'Create/Populate the DataReader
dbReader = dbcomm.ExecuteReader()
ApplicationList.DataSource = dbReader
ApplicationList.DataBind()
           ApplicationList.DataValueField = " str_application_id"
            ApplicationList.DataTextField = "str_application_name"
              End If

    End Sub
    Public Sub Submit(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim App As String
        App = ApplicationList.SelectedItem.Value

        tester.Text = "You selected " & ApplicationList.SelectedItem.Value

    End Sub
0
 
YZlatCommented:
tester.Text="You selected " & ApplicationList.SelectedItem.Value

or

tester.Text="You selected " & ApplicationList.SelectedItem.Text
0
 
freethinker23Author Commented:
On both of those I receive an 'object reference not set to instance of object" error.

If I change tester.Text="You selected " & ApplicationList.SelectedItem
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
freethinker23Author Commented:
tester.Text="You selected " & ApplicationList.SelectedIndex

It return a -1 when I click on the first entry.
0
 
S-TwilleyCommented:
I don't know anything about ASP, but I'd be tempted to try and check for null/nothing values... as well as trying to seperate each line out so errors are easier to trap

Public Sub Submit(sender As Object, e As System.EventArgs)
Dim selItem As Object
selItem = ApplicationList.SelectedItem

If selItem Is Nothing Then
     ' handle this how you'd want to
Else
    selItemStr = Cstr(selItem)                     ' if you get problems, try and get what type selItem is
    tester.Text="You selected " & selItemStr
End If

End Sub

==============

I'm use to writing VB.NET, so please scuse any differences
0
 
YZlatCommented:
try If I change tester.Text="You selected " & ApplicationList.SelectedValue
0
 
S-TwilleyCommented:
sorry forgot to declare selItemStr... it's a String if you hadn't guessed

    Dim selItemStr As String
0
 
YZlatCommented:
or try this:

 If ApplicationList.SelectedIndex <> -1 Then
          tester.Text="You selected " & ApplicationList.SelectedItem.Value
End If

since if it's -1 then no item was selected
0
 
freethinker23Author Commented:
It is returning nothing.  Not sure why, am I not declaring something in my ListBox correctly?
0
 
S-TwilleyCommented:
when you say it's returning nothing... do you mean it's going into the first branch of the If... Else statement

If selItem Is Nothing Then
     ' handle this how you'd want to      <=====  IN HERE
Else
    selItemStr = Cstr(selItem)                     ' if you get problems, try and get what type selItem is
    tester.Text="You selected " & selItemStr
End If

--------------------------

In VB.NET (assuming same in ASP)... it'd go in there if there is no item selected... or if somehow, you are adding "Nothing" to your listbox (I don't see it being this tho)...

Like i said, i don't know anything about ASP so at this point I may have to pass it over to the other experts!
0
 
freethinker23Author Commented:
Yes sorry,

If selItem Is Nothing Then
       ***event that is firing
Else
    selItemStr = Cstr(selItem)                    
    tester.Text="You selected " & selItemStr
End If

I have currently have two entries in the list box and that is happening when I selected either of them and click the submit button.
0
 
S-TwilleyCommented:
try this instead...

selItem = ApplicationList.SelectedValue

====

also, maybe after that line, try and get a prompt (some sort of msgbox... of what the selectedindex is)

Msgbox(ApplicationList.SelectedIndex)

and see if that is correct at least
0
 
YZlatCommented:
S-Twilley, I already suggested that before
0
 
YZlatCommented:
freethinker23, open your page with a ListBox, then on the toolbar select View->Source. Copy the html source code for your listbox and post it here. I think you must be building your listbox the wrong way
0
 
S-TwilleyCommented:
sorry, i saw that you had suggest ApplicationList.SelectedItem.Value ... but didnt see the other one, don't worry, not tryin to steal your points... effort has been put in by both parties :P
0
 
S-TwilleyCommented:
Anyway.. I'll leave it in your capable hands YZlat
0
 
YZlatCommented:
It's not that, I just think it's useless to repeat what has been said before and that already did not work for him
0
 
YZlatCommented:
you could also try

ApplicationList.SelectedItem.ToString()

but it might not work
0
 
freethinker23Author Commented:
From the Source:

<form name="_ctl0" method="post" action="new_bug.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE" value="dDwxODg0MjQ0NjA5O3Q8O2w8aTwxPjtpPDM+Oz47bDx0PHA8cDxsPFRleHQ7PjtsPE1BU1RfU1dEQ1xccmNvb2I7Pj47Pjs7Pjt0PDtsPGk8MT47PjtsPHQ8dDw7dDxpPDI+O0A8TWVldGluZyBDYWxlbmRhcjtPTUxFVDs+O0A8d2VicmVnY2FsZW5kYXI7d2VicmVnb21sZXQ7Pj47Pjs7Pjs+Pjs+Pjs+" />

<select name="ApplicationList" size="4" id="ApplicationList">
      <option value="webregcalendar">Meeting Calendar</option>
      <option value="webregomlet">OMLET</option>

</select>
<br>
<br>
<input type="submit" name="button5" value="" id="button5" />
 <span id="tester"></span>  
</body>
</form>
0
 
YZlatCommented:
try adding one more option - a blank:


ApplicationList.Items.Insert(0,"")

sqlcmd  = "SELECT str_application_id, str_application_name FROM tbl_SRO_applications"
dbcomm  = New SQLCommand(sqlcmd, oSQLConn)
'Create/Populate the DataReader
dbReader = dbcomm.ExecuteReader()
ApplicationList.DataSource = dbReader
ApplicationList.DataBind()
0
 
freethinker23Author Commented:
Still having the same issue...

Source now reads:
<select name="ApplicationList" size="4" id="ApplicationList">
      <option value=""></option>
      <option value="webregcalendar">Meeting Calendar</option>
      <option value="webregomlet">OMLET</option>

</select>
0
 
freethinker23Author Commented:
That worked!  Thank you very much for your patience.  
I am not really sure what was different other than declaring the text and value in the code, but I am not going to complain.  
Thanks again.
0
 
YZlatCommented:
it probably had to do with where did you put your code.
Anyways, I'm glad it finally worked:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.