Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "myModalEditStandType", "$('#myModalEditStandType').modal();", true);
        using (con)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select formid,formName from FormDetails where Edition_Id=" + Session["Edition_ID"].ToString() + " ";
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    chkFormNames.Items.Clear();
                    while (sdr.Read())
                    {
                       
                        ListItem item = new ListItem();
                        //string StandTypeID =sdr["StandTypeID"].ToString();

                        item.Text = sdr["formName"].ToString();
                        item.Value = sdr["FormId"].ToString();

                        // item.Selected = Convert.ToBoolean(sdr["IsSelected"]);

                        standName = e.CommandName.ToString();
                        txtStandType.Text = standName.ToString();
                        txtStandTypeEdit_ModalBox.Text = standName.ToString();
                        
                        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
                        
                        chkFormNames.Items.Add(item);
                        foreach (var ca in commandArgs)
                        {
                            if(ca != null || ca != string.Empty ||ca != "")
                            {
                                FormDetails_ID =ca;
                                if (item.Value == FormDetails_ID)
                                {
                                    item.Selected = true;
                                }
                            }
                        }
                    }
                }
                con.Close();
            }
        }
        grdManageStandType.DataBind();


error in below line:-
if (item.Value == FormDetails_ID)
{
  item.Selected = true;
}



on the above if condition there is logical error.

the issue is in this code only
i had debugged the code and checked. let me explain with the example of the error
Eg:-
StandTypeName         Forms      Edit Stand Type                                               
Co-Exhibitor         1, 2, 4         Edit

There are multiple forms. User click on edit button then a modal popup opens and user write the name of StandTypeName and select few checkbox form of multiple ones and then save it. After doing this when User again open modal popup for one of StandTypeName let say here user checks for Co-Exhibitor, there are 3 values given in the Form column for Co-Exhibitor but when user clicks on edit button modal popup opens and only first value is checked the other 2 value is not checked. When i am debugging the code for first form which here is 1 is working fine but for second and third form which here is 2, 4 is giving issue

Here
if (item.Value == FormDetails_ID)
 {
  item.Selected = true;
 }


at the time of debugging when it checks for Forms 2, the item.value and FormDetails.ID both contains value =2 but doesn't go inside if loop it directly goes out of loop

What I have tried:

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "myModalEditStandType", "$('#myModalEditStandType').modal();", true);
        using (con)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select formid,formName from FormDetails where Edition_Id=" + Session["Edition_ID"].ToString() + " ";
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    
                    while (sdr.Read())
                    {
                       
                        ListItem item = new ListItem();
                        //string StandTypeID =sdr["StandTypeID"].ToString();

                        item.Text = sdr["formName"].ToString();
                        item.Value = sdr["FormId"].ToString();

                        // item.Selected = Convert.ToBoolean(sdr["IsSelected"]);

                        
                    }
                }
                con.Close();
            }
        }

chkFormNames.Items.Clear();
standName = e.CommandName.ToString();
                        txtStandType.Text = standName.ToString();
                        txtStandTypeEdit_ModalBox.Text = standName.ToString();
                        
                        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
                        
                        chkFormNames.Items.Add(item);
                        foreach (var ca in commandArgs)
                        {
                            if(ca != null || ca != string.Empty ||ca != "")
                            {
                                FormDetails_ID =ca;
                                if (item.Value == FormDetails_ID)
                                {
                                    item.Selected = true;
                                }
                            }
                        }


        grdManageStandType.DataBind();
Posted
Updated 9-Aug-17 23:05pm
v4
Comments
Kornfeld Eliyahu Peter 10-Aug-17 2:14am    
Are you saying that while debugging the line 'if (item.Value == FormDetails_ID)', you see that both sides has the value '2', but you never enter the true part of the 'if'?
Are you sure the values are displayed correctly? Sometimes (thread dependent) the debugger displays values in gray to indicate that what you see is old, and you have manually refresh them...
Member 13041059 10-Aug-17 2:18am    
yes i am sure i have also taken a snapshot of error but i don't know how to put it here so that u can easily understand
Kornfeld Eliyahu Peter 10-Aug-17 2:21am    
Copy the error message here? With the line indicator in your code, like // <- Error is here: Error message
Member 13041059 10-Aug-17 2:33am    
It is not displaying any error Message

in the below code item.Value == FormDetails_ID is showing true for form 1 when these 2 are equal but for form 2,4 when the item.Value == FormDetails_ID is equal then also item.selected is showing false

if (item.Value == FormDetails_ID) // error is here
{
item.Selected = true;
}

1 solution

From your description and code it looks like you have a space in one value you're comparing but not the other - this whitespace may not be obvious when viewing it in the debugger which is why you think it's not working.

You show an example of using "1, 2, 4" and you are splitting your command argument string by a comma deliminator. This will lead to values "1", " 2" and " 4" in ca (and therefore FormDetails_ID). The comparison against "1" will work for the first form but comparing " 2" against "2" will not work as intended.

If this is the case then a potential solution is quite simple - just trim the string when you assign it to FormDetails_ID:
C#
FormDetails_ID = ca.Trim();

Note - I would also suggest you may want to check the logical comparison you're making ca. i.e. "if(ca != null || ca != string.Empty || ca != "")" will allow an empty string to be checked since it won't be null. A simple change would be to use logical AND rather than logical OR:
C#
if(ca != null & ca != string.Empty & ca != "")
{
   FormDetails_ID = ca.Trim();
   if (item.Value == FormDetails_ID)
   {
      item.Selected = true;
   }
}

You could also use the in-built static string check function IsNullOrEmpty for this instead:
C#
foreach (var ca in commandArgs)
{
    if(false == string.IsNullOrEmpty(ca))
    {
        FormDetails_ID = ca.Trim();
        if (item.Value == FormDetails_ID)
        {
            item.Selected = true;
        }
    }
}

If you want to go the whole way then you could simplify it further and not even have empty elements returned by the split function and ensure each item is selected or un-selected explicitly by an assignment:
chkFormNames.Items.Add(item);

string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var ca in commandArgs)
{
   item.Selected = (item.Value == ca.Trim());
}
 
Share this answer
 
Comments
Member 13041059 10-Aug-17 5:28am    
FormDetails_ID = ca.Trim();


Above code works for me thanks a lot.
Al_Brown 10-Aug-17 6:00am    
You're very welcome!

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