如果语句返回false,则不进入内部 [英] Not going inside if statement to return false

查看:70
本文介绍了如果语句返回false,则不进入内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从datatable中检索值,但我想将该值与我在方法中传递的值进行比较,但在if条件下,如果两个值都相同,则不会进入该条件。两个值都在字符串中。我已经尝试过以下代码请帮助需要 - 全部...谢谢



我尝试了什么:



I am retrieving value from datatable but i want to compare that value with the value which i have passed in method but in the if condition it does not go inside that condition if both the values are same also.Both values are in string. I have tried the following code please help the need-full... thank you

What I have tried:

public bool changeSession(string sess)
        {

            try
            {
                int i, j;
                string session = sess;
                string qrstr1;// to check whether session which is selected was previous selected or not?
                qrstr1 = "select session from session";
                SqlCommand cmd1 = new SqlCommand(qrstr1, Gencon);
                SqlDataAdapter da = new SqlDataAdapter(cmd1);
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (i = 0; i < dt.Rows.Count; i++)
                    for (j = 0; j < dt.Columns.Count; j++)
                    {
                        object o = dt.Rows[i].ItemArray[j];
                        //string s = dt.Rows[i].ItemArray[j].ToString();
                        string s = o.ToString();
                        if (s.Equals(session))
                        {

                            return false;
                            
                            
                        }
                            

                    }
                            
                    string qrstr;
                    qrstr = "update session set session=@sess where id=@id";
                    SqlCommand cmd = new SqlCommand(qrstr, Gencon);
                    cmd.Parameters.AddWithValue("@id", 1);
                    cmd.Parameters.AddWithValue("@sess", sess);
                    Gencon.Open();
                    cmd.ExecuteNonQuery();
                    Gencon.Close();
                    return true;
                
            }
            catch (Exception ex)
            {
                return false;
            }

        }

推荐答案

你必须使用调试器检查是否有内容两个字符串实际上是相同的(例如,比较区分大小写,请参阅 String.Equals方法(字符串)(系统) [ ^ ])。
You have to use the debugger to check if the content of the two strings is actually the same (for instance, the comparison is case sensitive, see String.Equals Method (String) (System)[^]).


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



根据会话检查变量 s code>了解它为什么不参加测试。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Inspect variable s against session to see why it don't go in the test.


这篇关于如果语句返回false,则不进入内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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