代码中的逻辑错误 [英] Logical error in the code

查看:129
本文介绍了代码中的逻辑错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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






如果条件有逻辑错误,上面的




问题仅在此代码中

i调试了代码并进行了检查。让我用错误的例子来解释

例如: -




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



有多种形式。用户单击编辑按钮,然后打开模态弹出窗口,用户写入StandTypeName的名称,并选择多个复选框形式,然后保存。执行此操作后,当用户再次为StandTypeName打开模态弹出窗口时,请在此处说明用户检查Co-Exhibitor,Co-Exhibitor的Form列中给出了3个值,但是当用户单击编辑按钮时,模态弹出窗口打开并且只有第一个值检查其他2值是否未选中。当我调试第一个表单的代码时,这里1是工作正常,但是第二个和第三个表单,这里是2,4是给出问题



这里


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





在调试时检查表单2,item.value和FormDetails.ID都包含值= 2,但如果循环直接循环,则不进入内部



我尝试过:





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

推荐答案

('#myModalEditStandType')。modal();,true);
使用(con)
{
using(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText =从FormDetails中选择formid,formName,其中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();
('#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;
}






如果条件有逻辑错误,上面的




问题仅在此代码中

i调试了代码并进行了检查。让我用错误的例子来解释

例如: -




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



有多种形式。用户单击编辑按钮,然后打开模态弹出窗口,用户写入StandTypeName的名称,并选择多个复选框形式,然后保存。执行此操作后,当用户再次为StandTypeName打开模态弹出窗口时,请在此处说明用户检查Co-Exhibitor,Co-Exhibitor的Form列中给出了3个值,但是当用户单击编辑按钮时,模态弹出窗口打开并且只有第一个值检查其他2值是否未选中。当我调试第一个表单的代码时,这里1是工作正常,但是第二个和第三个表单,这里是2,4是给出问题



这里


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





在调试时检查表单2,item.value和FormDetails.ID都包含值= 2,但如果循环直接循环,则不进入内部



我尝试过:





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);
使用(con)
{
使用(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText =select formid,formName from FormDetails,其中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();
('#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();


从您的描述和代码中看,您在一个值中有一个空间,而不是另一个 - 在调试器中查看时,这个空格可能并不明显,这就是为什么您认为它不起作用。



您显示使用1,2,4的示例,并且您将命令参数字符串除以逗号分隔符。这将导致ca中的值1,2和4(因此FormDetails_ID)。与1的比较将适用于第一种形式,但将2与2进行比较将无法按预期工作。



如果是这种情况,那么a潜在的解决方案非常简单 - 只需在将字符串分配给FormDetails_ID时修剪字符串:

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:
FormDetails_ID = ca.Trim();



注意 - 我还建议您查看正在进行的逻辑比较。即if(ca!= null || ca!= string.Empty || ca!=)将允许检查空字符串,因为它赢了不要。一个简单的改变是使用逻辑AND而不是逻辑OR:


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:

if(ca != null & ca != string.Empty & ca != "")
{
   FormDetails_ID = ca.Trim();
   if (item.Value == FormDetails_ID)
   {
      item.Selected = true;
   }
}



您也可以使用内置的静态字符串检查功能IsNullOrEmpty:


You could also use the in-built static string check function IsNullOrEmpty for this instead:

foreach (var ca in commandArgs)
{
    if(false == string.IsNullOrEmpty(ca))
    {
        FormDetails_ID = ca.Trim();
        if (item.Value == FormDetails_ID)
        {
            item.Selected = true;
        }
    }
}



如果你想全力以赴,那么你可以进一步简化它而不是甚至包含split函数返回的空元素,并确保通过赋值明确选择或取消选择每个项目:


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


这篇关于代码中的逻辑错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆