Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

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

        }
Posted
Updated 16-Jun-16 1:15am
v3

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)[^]).
 
Share this answer
 
Comments
Member 10259403 16-Jun-16 4:14am    
Both the strings are same and in same case also
CPallini 16-Jun-16 5:31am    
How did yuou check it?
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.
 
Share this answer
 

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