Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have take one table in SQl "floor" which columns like 'id' autogenerated and 'name'. And in that i insert 3 values like "one","two","three".

My question is ;
i want to read that 3 values from database and related to that three values disply 3 images.

My code like this.
C#
string str;
        con.Open();
        cmd = new SqlCommand("Select name from floor",con);
        SqlDataReader dr = cmd.ExecuteReader();


        while (dr.Read())
        {
            str = Convert.ToString(dr["name"].ToString());
            if (str == "one" && str=="two" && str=="three")
            {
                Image1.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_101_booked.png";
                Image2.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_102.png";
                Image3.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_103.png";
              
            }
        }
        con.Close();

But I am not understand how to use that if condition.

Pleases help. I am waiting.
Posted
Updated 15-May-13 4:27am
v2
Comments
[no name] 15-May-13 9:51am    
What do you mean that you "not understand how to use that if condition"? First thing is that you are converting a string to a string and that is completely unnecessary (dr["name"].ToString() is sufficient).

I am not sure if I understand what it is that you are doing. If you something in your database that is comma separated like, "one","two","three" then your if condition will never work. Your if is checking to see if str is "one" AND str is "two" AND str is "three" it can't be all three at the same time so it will fail every time.
RelicV 15-May-13 10:10am    
You're supposed to use Convert.ToString() and its what required here. Please never use .ToString() as it throws Null Exceptions.
ZurdoDev 15-May-13 10:23am    
What? You are saying if str equals "one" and it equals "two" and it equals "three". That's impossible. Do you mean OR instead of AND?
Bikash Prakash Dash 15-May-13 10:30am    
wots y'r question you want to know how to use an if condition or what ?

1 solution

your logical statement will never be true....
str variable can only be one of the values not all three of them.
C#
if (str == "one" && str=="two" && str=="three")

what you probably want is
C#
if (str == "one") 
    Image1.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_101_booked.png"; 
else if (str == "two")
    Image2.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_102.png";
else if (str == "three")
    Image3.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_103.png";


Or this
C#
if (str == "one" || str=="two" || str== "three")
{
    Image1.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_101_booked.png";
    Image2.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_102.png";
    Image3.ImageUrl = "~/images/book_flat_imgs/AWing/first_floor/A_103.png";
}

read up on c# operators and logical operators in general to understand what your code is doing.
http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.71).aspx[^]
 
Share this answer
 
v2

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