Why does this simple IF statement not work?

Last post 11-08-2009 11:59 AM by Mr^B. 16 replies.

Sort Posts:

  • Why does this simple IF statement not work?

    11-06-2009, 11:18 AM
    • Member
      162 point Member
    • billy_111
    • Member since 11-23-2008, 11:57 PM
    • England, UK
    • Posts 631

    Hey,

    I am trying to check to see if a row in my database is empty, if so do something..

    See the code below:-

            ///////////////////// L  E  F  T //////////////////////////////

    string connStr = "Data Source=tcp:esql2k801.discountasp.net;Initial Catalog=SQL2008_673062_kids;User ID=SQL2008_673062_kids_user;Password=*******;";
    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "SELECT page_id, main_id, breadcrumb, header_image, main_title, body_text, tab_1, tab_1_info, tab_2, tab_2_info, tab_3, tab_3_info, tab_4, tab_4_info, right_image " +
    "FROM tbl_tabbed_pages WHERE page_id = 3";

    dbConn.Open();
    SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);

    SqlDataReader dbReader = dbCommand.ExecuteReader();

    while (dbReader.Read())
    {
    lbl_breadcrumb.Text = dbReader["breadcrumb"].ToString();
    lbl_title.Text = dbReader["main_title"].ToString();

    if (dbReader["tab_1"].ToString() != "")
    {
    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>";
    }
    else
    {
    Label1.Visible = false;
    }

    if (dbReader["tab_2"].ToString() != "")
    {
    Label2.Text = "<li><a href=#fragment-11><span>" + dbReader["tab_2"].ToString() + "</span></a></li>";
    }
    else
    {
    Label2.Visible = false;
    }


    if (dbReader["tab_3"].ToString() != "")
    {
    Label3.Text = "<li><a href=#fragment-12><span>" + dbReader["tab_3"].ToString() + "</span></a></li>";
    }
    else
    {
    Label3.Visible = false;
    }

    if (dbReader["tab_4"].ToString() != "")
    {
    Label4.Text = "<li><a href=#fragment-13><span>" + dbReader["tab_4"].ToString() + "</span></a></li>";
    }
    else
    {
    Label4.Visible = false;
    }

    Label5.Text = dbReader["tab_1_info"].ToString();
    Label6.Text = dbReader["tab_2_info"].ToString();
    Label7.Text = dbReader["tab_3_info"].ToString();
    Label8.Text = dbReader["tab_4_info"].ToString();

    Image1.ImageUrl = "~/inner_pages/" + dbReader["right_image"].ToString();
    Image2.ImageUrl = "~/main_pages/" + dbReader["header_image"].ToString();
    lbl_content.Text = dbReader["body_text"].ToString() + "<BR>";
    }


    If you look at the bold bits i shows the IF statement, but it does not check how it should.

    Why does it not work..?


    Regards

  • Re: Why does this simple IF statement not work?

    11-06-2009, 12:53 PM
    • Star
      10,719 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,955

    Please repost your code using the code formatting tool, which is the right-most icon above the text entry area.  (Looks like a piece of paper with " {} " ) 

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Why does this simple IF statement not work?

    11-06-2009, 12:56 PM
    • Member
      162 point Member
    • billy_111
    • Member since 11-23-2008, 11:57 PM
    • England, UK
    • Posts 631

    Hey,

    Thanks, heres the code:-

    ///////////////////// L  E  F  T //////////////////////////////

    string connStr = "Data Source=tcp:esql2k801.discountasp.net;Initial Catalog=SQL2008_673062_kids;User ID=SQL2008_673062_kids_user;Password=*******;";
    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "SELECT page_id, main_id, breadcrumb, header_image, main_title, body_text, tab_1, tab_1_info, tab_2, tab_2_info, tab_3, tab_3_info, tab_4, tab_4_info, right_image " +
    "FROM tbl_tabbed_pages WHERE page_id = 3";

    dbConn.Open();
    SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);

    SqlDataReader dbReader = dbCommand.ExecuteReader();

    while (dbReader.Read())
    {
    lbl_breadcrumb.Text = dbReader["breadcrumb"].ToString();
    lbl_title.Text = dbReader["main_title"].ToString();

    if (dbReader["tab_1"].ToString() != "")
    {
    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>";
    }
    else
    {
    Label1.Visible = false;
    }

    if (dbReader["tab_2"].ToString() != "")
    {
    Label2.Text = "<li><a href=#fragment-11><span>" + dbReader["tab_2"].ToString() + "</span></a></li>";
    }
    else
    {
    Label2.Visible = false;
    }


    if (dbReader["tab_3"].ToString() != "")
    {
    Label3.Text = "<li><a href=#fragment-12><span>" + dbReader["tab_3"].ToString() + "</span></a></li>";
    }
    else
    {
    Label3.Visible = false;
    }

    if (dbReader["tab_4"].ToString() != "")
    {
    Label4.Text = "<li><a href=#fragment-13><span>" + dbReader["tab_4"].ToString() + "</span></a></li>";
    }
    else
    {
    Label4.Visible = false;
    }

    Label5.Text = dbReader["tab_1_info"].ToString();
    Label6.Text = dbReader["tab_2_info"].ToString();
    Label7.Text = dbReader["tab_3_info"].ToString();
    Label8.Text = dbReader["tab_4_info"].ToString();

    Image1.ImageUrl = "~/inner_pages/" + dbReader["right_image"].ToString();
    Image2.ImageUrl = "~/main_pages/" + dbReader["header_image"].ToString();
    lbl_content.Text = dbReader["body_text"].ToString() + "<BR>";
    }
  • Re: Why does this simple IF statement not work?

    11-06-2009, 3:23 PM
    • Star
      12,350 point Star
    • Mr^B
    • Member since 02-12-2006, 6:38 AM
    • Posts 2,190

    Hi,

    It works just fine - however you aren't checking for an empty row, you are checking a column in a row.  And you are checking that a column in a row is set to "" (which is an empty string).

    If there is no row, then:

    while (dbReader.Read())

    won't return a row, and your IF statement is never hit.

    To see what is happening, insert a breakpoint and debug the code, you'll see that if the row doesn't exist none of that code after dbReader.Read() is executed.
    MCSD.Net
  • Re: Why does this simple IF statement not work?

    11-06-2009, 6:39 PM
    • Member
      162 point Member
    • billy_111
    • Member since 11-23-2008, 11:57 PM
    • England, UK
    • Posts 631

    Hey,

    So how can i check for an empty row?

    Regards

  • Re: Why does this simple IF statement not work?

    11-06-2009, 9:50 PM
    • Member
      282 point Member
    • rakesh2kv
    • Member since 10-27-2008, 6:41 PM
    • Posts 241

    you should check for both null or empty

    if (!String.isnullorempty(dbReader["tab_1"].ToString() ))
    {
    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>";
    }

    hit a break point and see what happens..

    Note: empty and null values are different

  • Re: Why does this simple IF statement not work?

    11-07-2009, 5:31 AM
    • Member
      162 point Member
    • billy_111
    • Member since 11-23-2008, 11:57 PM
    • England, UK
    • Posts 631

    I think i'm going mad here. It still does not work!

    I set a breakpoint and what i am checking for is correct. I have tested with the code below:-

    if (String.IsNullOrEmpty(dbReader["tab_3"].ToString()))
                {
                    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>";
                }
                else
                {
                    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>" +
                      "<li><a href=#fragment-11><span>" + dbReader["tab_3"].ToString() + "</span></a></li>";
                }

    What happens is that the IF is totally ignored and the ELSE is executed. Therefore i am left with 2 tabs one of them which empty. Let me explain the logic. This is what my label will look like if all the database tab field have a value:-

    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>" +
                  "<li><a href=#fragment-11><span>" + dbReader["tab_2"].ToString() + "</span></a></li>" +
                    "<li><a href=#fragment-12><span>" + dbReader["tab_3"].ToString() + "</span></a></li>" +
                "<li><a href=#fragment-13><span>" + dbReader["tab_4"].ToString() + "</span></a></li>";

    Now the IF statement should simply be checking if tab_1 contains nothing then write tab_2, tab_3, and tab_4 to the label and vice versa.

    I have done this before many times and have never had a problem. However i did once try writing &nbsp; and it worked but that does not work either.

    What i want to do with the first bit of code is say if the database row tab_3 is empty then ONLY write tab_1 to the label OTHERWISE write tab_1 AND tab_3..

    Maybe i'm missing something out, but can anyone offer any advice..

    Thanks

  • Re: Why does this simple IF statement not work?

    11-07-2009, 6:01 AM

    what problem you facing..?

    Always check for HasRows.

    if(dr1.HasRows)
    {
      while (dr1.Read())
      {
         if(dr1["d"].ToString() == "")
         {
           ///     
         }
         else
         {
           ///
         }
       }
    }


     


    Mark as Answer
    on the posts replys that helped you.
    My Blog -: MSChart

  • Re: Why does this simple IF statement not work?

    11-07-2009, 8:17 AM
    • Star
      12,350 point Star
    • Mr^B
    • Member since 02-12-2006, 6:38 AM
    • Posts 2,190

    Now the IF statement should simply be checking if tab_1 contains nothing then write tab_2, tab_3, and tab_4 to the label and vice versa.

    But the IF statement isn't checking tab_1, it's checking tab_3:

    if (String.IsNullOrEmpty(dbReader["tab_3"].ToString()))



    MCSD.Net
  • Re: Why does this simple IF statement not work?

    11-07-2009, 9:17 AM
    • Member
      162 point Member
    • billy_111
    • Member since 11-23-2008, 11:57 PM
    • England, UK
    • Posts 631

    Ok, well here is my code now..

                if (String.IsNullOrEmpty(dbReader["tab_4"].ToString()) && String.IsNullOrEmpty(dbReader["tab_3"].ToString()) && String.IsNullOrEmpty(dbReader["tab_2"].ToString()))
                {
                    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>";
                }
                else if (String.IsNullOrEmpty(dbReader["tab_4"].ToString()) && String.IsNullOrEmpty(dbReader["tab_3"].ToString()))
                {
                    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>" +
                      "<li><a href=#fragment-11><span>" + dbReader["tab_2"].ToString() + "</span></a></li>";
                }
                else if (String.IsNullOrEmpty(dbReader["tab_4"].ToString()))
                {
                    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>" +
                      "<li><a href=#fragment-11><span>" + dbReader["tab_2"].ToString() + "</span></a></li>" +
                    "<li><a href=#fragment-12><span>" + dbReader["tab_3"].ToString() + "</span></a></li>";
                }
                else
                {
                    Label1.Text = "<li><a href=#fragment-10><span>" + dbReader["tab_1"].ToString() + "</span></a></li>" +
                    "<li><a href=#fragment-11><span>" + dbReader["tab_2"].ToString() + "</span></a></li>" +
                        "<li><a href=#fragment-12><span>" + dbReader["tab_3"].ToString() + "</span></a></li>" +
                    "<li><a href=#fragment-13><span>" + dbReader["tab_4"].ToString() + "</span></a></li>";
                }


    Now i dont need to check if tab_1 is emtpy because that will never happen. But am i right in saying that the above code should check tab_2, 3 and 4?

    What i am trying to say is:-
    • IF tab_4, 3 and 2 is emtpy then write tab_1
    • IF tab_4 and 3 is emtpy then write tab_1 and tab_2
    • IF tab_4 is emtpy then write tab_1, tab_2 and tab_3
    • ELSE write tab_1, tab_2, tab_3, and tab_4

    Now the code i have written goes straight to the else, so it means that it thinks there is something in the database..

    I have chekcked in my table and there should only be 2 tabs showing.

    You can see the link here, you can see that all 4 of the tabs are showing when there should only be 2..

    http://kidsunlimi2.eweb102.discountasp.net/settling-in.aspx

    Regards

  • Re: Why does this simple IF statement not work?

    11-07-2009, 10:46 AM
    • Star
      12,350 point Star
    • Mr^B
    • Member since 02-12-2006, 6:38 AM
    • Posts 2,190

    So, stepping through that, what are the values in 

    dbReader["tab_1"].ToString();

    dbReader["tab_2"].ToString();

    dbReader["tab_3"].ToString();

    dbReader["tab_4"].ToString();


    if statement don't make mistakes or lie...whatever is in the dbReader is causing the behaviour.

    MCSD.Net
  • Re: Why does this simple IF statement not work?

    11-07-2009, 10:59 AM
    • Participant
      1,641 point Participant
    • vishwaraj1
    • Member since 11-07-2008, 7:44 PM
    • India
    • Posts 424

    Check like this...
                SqlDataReader obj_Dr;
                obj_Dr = obj_cmd.ExecuteReader();
                if (obj_Dr.HasRows)
                {
                    while (obj_Dr.Read())
                    {
                        /// your bind code here... 
    
                    }
                }
                else
                {
                    response.write("No record found");
                }



    Regards:

    Vishwaraj Malik

    VB to C# Converter


    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Why does this simple IF statement not work?

    11-07-2009, 11:28 AM
    • Contributor
      4,351 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 10:15 AM
    • Hyderabad
    • Posts 889

    I wonder your code is not throwing any exceptions. DataReader is Forward only. And you are checling in backward order

    billy_111:
    String.IsNullOrEmpty(dbReader["tab_4"].ToString()) && String.IsNullOrEmpty(dbReader["tab_3"].ToString()) && String.IsNullOrEmpty(dbReader["tab_2"].ToString())

    Better assign all tabs to strings and chek for nullorempty. And check all string varaiable values and compare with your if.

    Fayaz
  • Re: Why does this simple IF statement not work?

    11-07-2009, 2:02 PM
    • Member
      380 point Member
    • ather.naeem
    • Member since 11-05-2009, 11:48 AM
    • Posts 75

    Please note that SqlDataReader is Forward Only and any attempt to check that data exist or not will make it empty until and unless you use (.HasRows) to check data exist or not.


    Please Mark ANSWER, To This Post If It's HELPFUL For Your ...

    Regards,
    Athar Naeem
  • Re: Why does this simple IF statement not work?

    11-08-2009, 1:20 AM
    Answer
    • Participant
      1,644 point Participant
    • jitendramcu
    • Member since 08-28-2009, 8:58 AM
    • Mumbai
    • Posts 291

    your u using while loop variable with lable control so it always gives one result i,e last row result.

    Try to this,

    string ConnectionString = ConfigurationManager.ConnectionStrings["TimeSheetConnectionString"].ConnectionString;
     

      SqlConnection dbConn = new SqlConnection(ConnectionString);

      string sqlStr = "SELECT tab1,tab2,tab3,tab4 from tab_info";

      dbConn.Open();
      SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);

      SqlDataReader dbReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
      string str;
      int index = 1;
      while (dbReader.Read())
      {
      // lbl_breadcrumb.Text = dbReader["breadcrumb"].ToString();
      //lbl_title.Text = dbReader["main_title"].ToString();
       
      Response.Write("Row No:-" + index.ToString()+"<br>");

    //4,3,2 null then print 1.
      if (string.IsNullOrEmpty(dbReader["tab4"].ToString()) && string.IsNullOrEmpty(dbReader["tab3"].ToString()) && string.IsNullOrEmpty(dbReader["tab2"].ToString()))
      {
       
      str= "<li><a href=#fragment-10><span>" + dbReader["tab1"].ToString() + "</span></a></li>";
      Response.Write(str + "<br>");
      }
      else  //4,3 null then print 1,2
      if (string.IsNullOrEmpty(dbReader["tab4"].ToString()) && string.IsNullOrEmpty(dbReader["tab3"].ToString()))
      {

      str = "<li><a href=#fragment-10><span>" + dbReader["tab1"].ToString() + "</span></a></li>" +"<li><a href=#fragment-10><span>" + dbReader["tab2"].ToString() + "</span></a></li>";
      Response.Write(str + "<br>");
      }
      else //4 null then print 1,2,3
      if(string.IsNullOrEmpty(dbReader["tab4"].ToString())) 
      {
      str = "<li><a href=#fragment-10><span>" + dbReader["tab1"].ToString() + "</span></a></li>" +"<li><a href=#fragment-10><span>" + dbReader["tab2"].ToString() + "</span></a></li>" + "<li><a href=#fragment-10><span>" + dbReader["tab3"].ToString() + "</span></a></li>";
      Response.Write(str + "<br>");
      }

      else//print 1,2,3,4 when none is null.
      {
      str = "<li><a href=#fragment-10><span>" + dbReader["tab1"].ToString() + "</span></a></li>" + "<li><a href=#fragment-10><span>" + dbReader["tab2"].ToString() + "</span></a></li>" + "<li><a href=#fragment-10><span>" + dbReader["tab3"].ToString() + "</span></a></li>" + "<li><a href=#fragment-10><span>" + dbReader["tab4"].ToString() + "</span></a></li>";
      Response.Write(str + "<br>");

      }
      Response.Write("End of Row:-" + index.ToString() + "<br>");
      index++;


       
      }
      dbReader.Close();
      }

    and database field we test,

    ID   tab1   tab2   tab3   tab4

    1     t1      t2       t3       t4

    2    tb2     null    null     null

    3   tb1      tb2     null    null

    4  tab1    tab2    tab3   null

    it will solve you problem. 

     

     

     

    Thanks,if it helps you then mark as "Answered"
    -----------------------------------------------
    Jitendra Kr.
    http://jitendra-aspnet.blogspot.com/


Page 1 of 2 (17 items) 1 2 Next >