[SOLVED] ListItem.Selected returning false

Issue

I have a listbox with a list of items that get loaded when you navigate to a certain page. Then I have an on click that passes parameters to an AddProducts method. That method is what loops through all selected items and inserts values from the fields filled in as well as takes the values from the listItems and adds them as parameters to a stored procedure.

The problem I’m running into is that when looping through the listItems
if(selectedItem.Selected) is returning false, but in my method where I load the listbox I initialize a SelectedValue so I’m not sure why it’s giving me the error message I have for no selected items. I was able to get it to work yesterday before I moved LoadListBoxCategories outside of LoadAddData but unsure as to why that would have any effect to if(listItem.Selected).

I’m relatively new to asp.net so any help/ explanation as to why my code isn’t working is extremely appreciated. I’ve spent the last three days trying to figure this out and haven’t found a solution that works.

Code:

Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session[IS_LOGGED_IN] == null)
    {
        Response.Redirect("/utilities/companydata/login.aspx", true);
        return;
    }

    gvTextInputs.RowEditing += new GridViewEditEventHandler(gvTextInputs_RowEditing);

    if (!IsPostBack)
    {
        string pageId = Request.QueryString["pageid"];
        string productId = Request.QueryString["productid"];

        /* Add Mode */
        if (!string.IsNullOrEmpty(pageId))
        {
            SetMode(MODE_ADD, "");
            LoadAddData(pageId);
            LoadListBoxCategories(pageId);
        }
        else if (!string.IsNullOrEmpty(productId))
        {
            string imageServer;
            
            if (LoadProductData(productId, out imageServer))
            {
                InitImageGridview();
                InitTextGridview();
                InitStaticGridview();
                SetMode(MODE_EDIT, imageServer);
                SetImageServer(imageServer);
            }
            else
            {
                //TO DO - Return Error
            }
        }
    }
    else
    {
        InitImageGridview();
        InitTextGridview();
        InitStaticGridview();
    }
}

Load ListBox:

private void LoadListBoxCategories(string pageId)
{
    listBoxCategories.Visible = true;

    //This gets query is so I can store the CompanyId and the CombinedValue data from pageId
    string select = "SELECT companyName, cl.GbsCompanyId, cl.companyId, wpt.productTypeId, productName, (CAST(wp.pageId as varchar(200))  +'|'+ CAST(wp.productTypeId as varchar(200)) + '|' )  AS CombinedValue FROM CompanyList cl, WtpPages wp, WtpProductTypes wpt WHERE cl.companyId=wp.companyId AND wpt.productTypeId=wp.productTypeId AND [email protected]";

    SqlDataSource connectionId = new SqlDataSource(DB_CONNECT, select);
    connectionId.SelectParameters.Add("pageId", pageId);

    DataView dView = (DataView)connectionId.Select(DataSourceSelectArguments.Empty);
    if (dView.Table.Rows.Count == 1)
    {
        string companyId = dView.Table.Rows[0]["companyId"].ToString();
        string curCategoryProductTypeId = dView.Table.Rows[0]["CombinedValue"].ToString();
        // EXEC MCAdmin_GetAllCategoriesByCompanyId @companyId
        // Lists All Categories @companyId has Active
        string selectLoadData = "EXEC MCAdmin_GetAllCategoriesByCompanyId @companyId";

        SqlDataSource conn = new SqlDataSource(DB_CONNECT, selectLoadData);
        conn.SelectParameters.Add("companyId", companyId);

        lstCategoriesBox.Items.Clear();
        lstCategoriesBox.Items.Add(new ListItem("--Select--", null));
        lstCategoriesBox.DataTextField = "productName";
        lstCategoriesBox.DataValueField = "CombinedValue";
        // Pre-selects the value of the productTypeId you are trying to add a product for
        // to send later run against a foreach insert in AddProduct()
        lstCategoriesBox.SelectedValue = curCategoryProductTypeId;
        testOutcomeCategory.InnerText = curCategoryProductTypeId;

        
        lstCategoriesBox.DataSource = conn;
        lstCategoriesBox.DataBind();
    
        
    }
}

AddProduct:

private string AddProduct(string companyId, out string errMsg)
{
    foreach (ListItem selectedItem in lstCategoriesBox.Items)
    {
        if (selectedItem.Selected)
        {
            // assign current productTypeId & pageId from selected Categories new CombinedValue column
            string[] splitColumnValue = selectedItem.Value.Split('|');

            string selectedPageId = splitColumnValue[0].ToString();
            string selectedProductTypeId = splitColumnValue[1].ToString();

            SqlDataSource connnection = new SqlDataSource(DB_CONNECT, "");
            connnection.InsertCommand = "EXEC MCAdmin_AddProductFromClassic @pageId, @productTypeId, @productCode, @imgDirectory, @numSides, @sortOrder, @isActive, @template, @template2, @template3, @EditorJson, @MockupTemplateBase, @MockupTemplateTreatment, @BorderDefault ";

            connnection.InsertParameters.Add("pageId", selectedPageId);
            connnection.InsertParameters.Add("productTypeId", selectedProductTypeId);
            connnection.InsertParameters.Add("productCode", txtProductCode.Text);
            connnection.InsertParameters.Add("numSides", ddlNumSides.SelectedValue);
            connnection.InsertParameters.Add("sortOrder", txtSortOrder.Text);
            connnection.InsertParameters.Add("isActive", ddlActive.SelectedValue);
            connnection.InsertParameters.Add("template", txtTemplate1.Text);
            connnection.InsertParameters.Add("template2", txtTemplate2.Text);
            connnection.InsertParameters.Add("template3", txtTemplate3.Text);
            connnection.InsertParameters.Add("EditorJson", txtJson.Text);
            connnection.InsertParameters.Add("MockupTemplateBase", txtMockupTemplateBase.Text);
            connnection.InsertParameters.Add("MockupTemplateTreatment", txtMockupTemplateTreatment.Text);
            connnection.InsertParameters.Add("BorderDefault", txtBorderDefault.Text);

            /* Special Product Code for Upload Artwork Business Card */
            if (txtProductCode.Text.ToUpper() == "BPFAH1-001-100")
            {
                connnection.InsertParameters.Add("imgDirectory", "/images/business-cards/general/");
            }
            else
            {
                connnection.InsertParameters.Add("imgDirectory", ddlImgDir.SelectedValue);
            }

            int result = connnection.Insert();

            if (result > 0)
            {
                SqlDataSource connect = new SqlDataSource(DB_CONNECT, "");
                connect.SelectCommand = "SELECT TOP 1 wtpProductId FROM WtpProducts ";
                connect.SelectCommand = "WHERE [email protected] AND [email protected] DESC ";

                connect.SelectParameters.Add("pageId", selectedPageId);  //
                connect.SelectParameters.Add("productTypeId", selectedProductTypeId); //

                DataView dView = (DataView)connect.Select(DataSourceSelectArguments.Empty);
                if (dView.Table.Rows.Count == 1)
                {
                    string wtpProductId = dView.Table.Rows[0]["wtpProductId"].ToString();
                    errMsg = "";
                    return wtpProductId;
                }
                else
                {
                    errMsg = "ERROR: Could not get productId of newly created Product.";
                    return "0";
                }
            }
            else
            {
                errMsg = "ERROR: Could not add WtpProduct record to DB";
                return "0";
            }
        }
        else
        {
            errMsg = "ERROR: You must select a Category";
            return "0";
        }
    }
    errMsg = "ERROR: Did not make it into the foreach loop";
    return "0";
}

OnClick method:

protected void OnClick_btnAddProduct(object sender, EventArgs e)
{
    string pageId = Request.QueryString["pageid"];

    testOutcomeCategory.InnerText = lstCategoriesBox.SelectedValue; // This proves that I have something selected!!!

    string select = "SELECT companyName, cl.GbsCompanyId, cl.companyId, wpt.productTypeId, productName, baseImgDirectory, templateDirectory, wp.imageServer FROM CompanyList cl, WtpPages wp, WtpProductTypes wpt WHERE cl.companyId=wp.companyId AND wpt.productTypeId=wp.productTypeId AND [email protected]";

    SqlDataSource conn = new SqlDataSource(DB_CONNECT, select);
    conn.SelectParameters.Add("pageId", pageId);

    DataView dView = (DataView)conn.Select(DataSourceSelectArguments.Empty);
    if(dView.Table.Rows.Count == 1)
    {
        string companyId = dView.Table.Rows[0]["companyId"].ToString();
        if (!string.IsNullOrEmpty(pageId))
        {
            string errMsg;
            string productId = AddProduct(companyId, out errMsg);
            if(productId != "0")
            {
                Response.Redirect("/utilities/companydata/add-edit-wtp-product.aspx?productid=" + productId, true);
                SetStatusMsg("Success", false);
            }
            else
            {
                SetStatusMsg(errMsg, true);
            }
        }
    } 
}

Solution

UPDATE: I just figured it out! So stupid but when I check if(selectedItem.Selected) since I’m looping through the ListBox items it starts at the first index of the ListBox and since the first isn’t selected then that if goes to the else block. Remove the else and it works fine

Answered By – Joshua Huezo

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *