Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form for a web application that selects categories using a listbox control. I have no problem populating the values in the listbox with data from a table and extracting that to save. The problem is when I go to edit, I cannot find a way to mark the associated records as selected.

The first sub below is the sub that populates the listbox in the form. The second sub attempts to load values from a table that associates records from a primary table to records in the categories table.

The second sub only marks the last record in the reader as selected. I have searched extensively for a solution to what should be a simple task and have found nothing. I could do this blindfolded with inline code; this is frustrating.

VB
Protected Sub LoadCat()
    Dim myCommand As New SqlCommand
    Dim myReader As SqlDataReader = Nothing
    Dim strConnection As String
    strConnection = ConfigurationManager.AppSettings("ConnectionString")

    Dim sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath
    Dim oInfo = New System.IO.FileInfo(sPath)
    Dim sRet = oInfo.Name

    Try
        myConnection = New SqlConnection(strConnection)
        myConnection.Open()
        'opening the connection
        With myCommand
            .CommandType = CommandType.StoredProcedure
            .CommandText = "MCC_LoadRecCat"
            .Connection = myConnection
            myReader = .ExecuteReader()
        End With
    Catch ex As Exception
        Dim EmailClass As New EmailClass
        EmailClass.EmailError(ex.Message.ToString, sRet, Session("ErrorEmail"), Session("EmailUser"), Session("EmailPassword"), Session("MailServer"))
        Response.Redirect("Error.aspx")
        Exit Sub
    End Try

    reccatID.DataSource = myReader
    reccatID.DataTextField = "reccatName"
    reccatID.DataValueField = "reccatID"
    reccatID.DataBind()

    myReader.Close()
    myCommand.Dispose()
    myConnection.Close()

End Sub



VB
Protected Sub PopulateCat()
    Dim myCommand As New SqlCommand
    Dim myReader As SqlDataReader = Nothing
    Dim strConnection As String
    strConnection = ConfigurationManager.AppSettings("ConnectionString")

    Dim sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath
    Dim oInfo = New System.IO.FileInfo(sPath)
    Dim sRet = oInfo.Name

    Dim MyID = Request("ID")
    ViewState("recipeID") = Request("ID")

    Try
        myConnection = New SqlConnection(strConnection)
        myConnection.Open()
        'opening the connection
        With myCommand
            .CommandType = CommandType.StoredProcedure
            .CommandText = "MCC_GetRecipeCat2"
            .Parameters.Add("@recipeID", SqlDbType.Int).Value = MyID
            .Connection = myConnection
            myReader = .ExecuteReader()
        End With
    Catch
    End Try

    While myReader.Read
        reccatID.SelectedValue = myReader("catID")
    End While


    myReader.Close()
    myCommand.Dispose()
    myConnection.Close()
End Sub
Posted
Updated 28-Jul-10 19:35pm
v2

1 solution

You're setting the selectedvalue over and over. Thus, each time, you unselect the last one selected. I believe there's a SelectedValues property, a collection you can add to. If there's not, then you can't select more than one item,
 
Share this answer
 
Comments
WesleyGroves 28-Jul-10 20:33pm    
I found one solution online that used SelectedValues as an array; but it does not work in .net. The property is not supported. You get 'SelectedValues' is not a member of 'System.Web.UI.WebControls.Listbox'

Again, this is a very basic function. You load a multiselect listbox to associate multiple selections via an array and insert into the database. There should be a way to load the values back into a web form to edit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900