[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ListItem cannot be converted to String

Posted on 2005-04-13
23
Medium Priority
?
385 Views
Last Modified: 2012-06-21
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.

0
Comment
Question by:freethinker23
  • 10
  • 7
  • 6
23 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 13774777
tester.Text="You selected " & ApplicationList.SelectedItem.Value

or

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

Author Comment

by:freethinker23
ID: 13774914
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
 

Author Comment

by:freethinker23
ID: 13774921
tester.Text="You selected " & ApplicationList.SelectedIndex

It return a -1 when I click on the first entry.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13775016
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
 
LVL 35

Expert Comment

by:YZlat
ID: 13775092
try If I change tester.Text="You selected " & ApplicationList.SelectedValue
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13775116
sorry forgot to declare selItemStr... it's a String if you hadn't guessed

    Dim selItemStr As String
0
 
LVL 35

Expert Comment

by:YZlat
ID: 13775134
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
 

Author Comment

by:freethinker23
ID: 13775175
It is returning nothing.  Not sure why, am I not declaring something in my ListBox correctly?
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13775202
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
 

Author Comment

by:freethinker23
ID: 13775255
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13775294
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
 
LVL 35

Expert Comment

by:YZlat
ID: 13775326
S-Twilley, I already suggested that before
0
 
LVL 35

Expert Comment

by:YZlat
ID: 13775350
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13775354
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13775362
Anyway.. I'll leave it in your capable hands YZlat
0
 
LVL 35

Expert Comment

by:YZlat
ID: 13775377
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
 
LVL 35

Expert Comment

by:YZlat
ID: 13775406
you could also try

ApplicationList.SelectedItem.ToString()

but it might not work
0
 

Author Comment

by:freethinker23
ID: 13775410
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
 
LVL 35

Expert Comment

by:YZlat
ID: 13775460
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
 

Author Comment

by:freethinker23
ID: 13775548
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
 
LVL 35

Accepted Solution

by:
YZlat earned 2000 total points
ID: 13775714
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
 

Author Comment

by:freethinker23
ID: 13775812
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
 
LVL 35

Expert Comment

by:YZlat
ID: 13775904
it probably had to do with where did you put your code.
Anyways, I'm glad it finally worked:)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question