skip to Main Content

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 wp.pageId=@pageId";

    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 productTypeId=@productTypeId AND pageId=@pageId 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 wp.pageId=@pageId";

    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);
            }
        }
    } 
}

4

Answers


  1. Chosen as BEST ANSWER

    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


  2. The list box instance is recreated on the server-side during the postback (as well as entire page). You do not set the selected items in the Page_Load event handler – if (!IsPostBack) goes to else branch. This is why you don’t see them.

    Login or Signup to reply.
  3. Ok,

    lstCategoriesBox.Items.Clear();
    

    Ok, above goes nuclear – blows out the list, blows out the selection.

    Ok, that’s fine

        lstCategoriesBox.Items.Add(new ListItem("--Select--", null));
        lstCategoriesBox.DataTextField = "productName";
        lstCategoriesBox.DataValueField = "CombinedValue";
    

    Ok, above adds a new item. Should be ok, but one should setup the lb BEFORE adding any data – including that "–select–" row.

    It just makes sense to "setup" the lb and THEN start adding data, right?
    However, But, if above works – ok, then lets keep going, but I would setup the lb before adding any rows of data. Say like this:

        lstCategoriesBox.DataTextField = "productName";
        lstCategoriesBox.DataValueField = "CombinedValue";
        lstCategoriesBox.Items.Add(new ListItem("--Select--", null));
    

    Now, the next line:

        lstCategoriesBox.SelectedValue = curCategoryProductTypeId;
    

    Ouch! – we just cleared the lb, and now we trying to set it to a value? You can’t do that – the lb just been cleared, right? You would need to load up the lb first, and THEN you can set it to the selected value, right?

    I might be missing something here, but that lb needs to be loaded up with valid data BEFORE you attempt to set the selected value?

    Login or Signup to reply.
  4. To test first, change following

    Outcome Category.InnerText = list CategoriesBox.SelectedValue;
    

    to

    Category.InnerText = Request.Form["CategoriesBox-ClientID"]
    

    If the data is coming in correctly, the list view cleared or reloaded when between reload and click events the page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search