Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with listbox not recognizing selection 1

Status
Not open for further replies.

DatabaseDude

Programmer
Nov 7, 2005
112
0
0
US
This is probably so simple that I'll beat myself repetitively after learning the answer :)

Problem: I have a listbox, lstUrls, that is populated by a dataset. It contains a list of web sites. And there is a button, btnRemove, that attempts to recognize the currently selected record in lstUrls. However ... it always returns a blank value.

Code for populating/binding lstUrls:

Code:
        Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("StiApps").ConnectionString)

        Try
            Conn.Open()

            Dim CommUrlsAssigned As New SqlCommand

            With CommUrlsAssigned
                .CommandText = "dbo.spGetAssignedUserUrls"
                .Connection = Conn
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@StrUserName", StrUserName)
            End With

            Dim DaUrlsAssigned As SqlDataAdapter = New SqlDataAdapter
            DaUrlsAssigned.SelectCommand = CommUrlsAssigned

            Dim DsUrlsAssigned As DataSet = New DataSet
            DaUrlsAssigned.Fill(DsUrlsAssigned, "AssignedUrls")

            With Me.lstUrls
                .DataSource = DsUrlsAssigned
                .DataMember = "AssignedUrls"
                .DataValueField = "UserModlueID"
                .DataTextField = "LinkName"
                .DataBind()
            End With

            Conn.Close()


        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally

        End Try

FWIW "UserModlueID" is the correct spelling of the database field, at least the way it's currently spelled.

And the OnClick code for btnRemove:

Code:
        Response.Write(Me.lstUrls.SelectedValue)

Thinking it's just a minor syntax thing - thanks in advance for taking a look at this for me!
 
Are you sure there are values for the datavaluefield "UserModuleID"?
 
Sure does. I ran the stored procedure separately, and I also bound the same dataset to a gridview. All fields were populated.
 
When you populate the dataset and bind it. ARe you doing it in the page load? and if so, are you placing an
If Not Page.IsPostBack around it?


 
Well, one problem solved! A big D'OH on me for that :)

Thanks to you as always my friend!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top