locked
Condition after else is not read RRS feed

  • Question

  • Hello, I have a problem, why the condition after else is not read

    the value for label2.text=0 is not display when I runnning the program

    I have a script like this:

    System.Data.SqlClient.SqlConnection conn1 = konn.GetConn();
                SqlDataReader reader1 = null;
                DateTime x = DateTime.Now.Date;

                conn1.Open();
                SqlCommand command = new SqlCommand("Select max(No) As Nomor from Queue_system where Date >= @x and Date < @y ", conn1);
                command.Parameters.Add("@x", SqlDbType.DateTime2).Value = x;
                command.Parameters.Add("@y", SqlDbType.DateTime2).Value = x.AddDays(1.0);
                //Cek ada

                reader1 = command.ExecuteReader();

                if (reader1.Read())
                {
                    label2.Text = reader1["Nomor"].ToString(); //it will give a value from database (it works)
                }
                else  // This part is not read, so it doesn't display value 0 in label 2 !!!
                {
                     label2.Text ="0";
                }

    thank's for the answer

    Sunday, November 15, 2015 11:11 PM

Answers

  • Thanks All of My Friends

    I just found the problem, and get the solution for these

    I can't use MAX for the script (for SQL Server Query)

    then I have to change it to count (not max)

    and I can't use if...else...(it won't work)

    so I change if..else... with While ...(condition)....

    and it works !!!, it show value 0 (if no value in table)...and it will show the value (if there is a value in table)

    Thank you very much my friends for helping me to get solution for this problem :)

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:11 AM
    Tuesday, November 24, 2015 7:07 AM

All replies

  • I am not sure what you expect to happen.

    If reader1.Read() returns true, label2.Text will be the value from the database. If reader1.Read() returns false, label2.Text will be "0". That is, label2.Text will be one or the other - it cannot be both.

    What do you expect to happen?

    Sunday, November 15, 2015 11:29 PM
  • Obviously you will have a bug in your program.

    if (reader1.Read())

    meaning if the row exist. So as long as the row exist but the field is empty the code

    label2.Text = reader1["Nomor"].ToString();

    will still be running.

    Try this:

    if (reader1.Read())
      if (reader1["Nomor"].ToString() != "")
                {
                    label2.Text = reader1["Nomor"].ToString(); //it will give a value from database (it works)
                }
                else  // This part is not read, so it doesn't display value 0 in label 2 !!!
                {
                     label2.Text ="0";
                }

    chanmm

    Please mark an answer if this help.


    chanmm

    Monday, November 16, 2015 12:37 AM
  • HI.

    because the Max function will return DbNull value, even if there is not any record in the table.

    so. the Reader.Read() always return true at first calling because at least one row return by.

    I can do like chanmm said.

    or 

    do like below.

    object _ov = _cmd.ExecuteScalar();
    string_value = "0";
    if (!(_ov is System.DBNull)) _value = _ov.ToString();
    _label2.Text = _value;


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.




    Monday, November 16, 2015 2:20 AM
  • Hi Steve,

    It's because you are not handling the NULL value that is returning from your query. Unlike select query where there is natch in DB it won't return any row, aggregate function always returns a row if there is no match in the DB then then it will return a null row. So in your code you are checking

    if(reader1.Read())

    this means that it will execute if there is a row return and in you query

    ("Select max(No) As Nomor from Queue_system where Date >= @x and Date < @y ", conn1)

    its always returning a row even if there is no data it is returning null row so your if() condition is always true. It is best way to handle null value in aggregate function. You can modify you query by using NVL() below if you are using oracle,

    "Select nvl(max(No), 0) As Nomor from Queue_system where Date >= @x and Date < @y ", conn1)"

    if you are usinf SQL Server,

    "Select isnull(max(No), 0) As Nomor from Queue_system where Date >= @x and Date < @y ", conn1)"

    In this way if there is null value in the row it will replace by 0. Now you can rewrite your If() condition with this,

    if (reader1.Read())
    {
        if (reader1["Nomor"].ToString() == "0")
        {
            label2.Text = reader1["Nomor"].ToString(); //it will give a value from database (it works)
        }
        else  // This part is not read, so it doesn't display value 0 in label 2 !!!
        {
            label2.Text ="0";
        }
    }

    Hope this helps you.

    Thanks,

    Sabah Shariq

    • Proposed as answer by RJP1973 Monday, November 16, 2015 11:57 AM
    Monday, November 16, 2015 11:52 AM
  • Thank You Sabah Shariq, but it doesn'r works, it still show nothing in label2, not 0 but display nothing

    System.Data.SqlClient.SqlConnection conn1 = konn.GetConn();
                //SqlDataReader reader1 = null;
                DateTime x = DateTime.Now.Date;

                conn1.Open();
                SqlCommand command = new SqlCommand("Select isnull(max(No),0) As Nomor from Queue_system where Date >= @x and Date < @y ", conn1);
                command.Parameters.Add("@x", SqlDbType.DateTime2).Value = x;
                command.Parameters.Add("@y", SqlDbType.DateTime2).Value = x.AddDays(1.0);
                //Check if true
                reader1 = command.ExecuteReader();
                if (reader1.Read())
                {
                    if (reader1["Nomor"].ToString() == "0")
                    {
                        label2.Text = reader1["Nomor"].ToString(); //it will give a value from database (it works)
                    }
                    else  // This part is not read, so it doesn't display value 0 in label 2 !!!
                    {
                        label2.Text = "0";
                    }
                }

    Tuesday, November 17, 2015 2:13 AM
  • Hi Ante, thanks for the answer

    I wanna put value 0 in label 2 if the the database is empty

    so what should I do ?

    Tuesday, November 17, 2015 2:15 AM
  • Hi chanmm, thanks for the answer but the value still empty in label2, it doesn't display anything, I wanna put value 0 if the value from the table is empty

      System.Data.SqlClient.SqlConnection conn1 = konn.GetConn();
                //SqlDataReader reader1 = null;
                DateTime x = DateTime.Now.Date;

                conn1.Open();
                SqlCommand command = new SqlCommand("Select max(No) As Nomor from Queue_system where Date >= @x and Date < @y ", conn1);
                command.Parameters.Add("@x", SqlDbType.DateTime2).Value = x;
                command.Parameters.Add("@y", SqlDbType.DateTime2).Value = x.AddDays(1.0);
                //Check if true
                reader1 = command.ExecuteReader();
                if (reader1.Read())
                    if (reader1["Nomor"].ToString() != "")
                    {
                        label2.Text = reader1["Nomor"].ToString(); //it will give a value from database (it works)
                    }
                    else  // This part is not read, so it doesn't display value 0 in label 2 !!!
                    {
                        label2.Text = "0";
                    }

    Tuesday, November 17, 2015 2:18 AM
  • hi Matthew thank's for the answer

    but it doen't work, any other suggestion ??

    thanks for your help

    Tuesday, November 17, 2015 2:19 AM
  •             if (reader1.HasRows)
                {
                    reader.Read();
                    label2.Text = reader1["Nomor"].ToString();
                }
                else
                {
                     label2.Text ="0";
                }

    Tuesday, November 17, 2015 3:17 AM
  • It is not works!?

    the code i post before just is demo. 

    now  i copy your code and did somewhat modifying,

    you try again. please.

    System.Data.SqlClient.SqlConnection conn1 = konn.GetConn(); SqlDataReader reader1 = null; DateTime x = DateTime.Now.Date; conn1.Open(); SqlCommand command = new SqlCommand("Select max(No) As Nomor from Queue_system where Date >= @x and Date < @y ", conn1); command.Parameters.Add("@x", SqlDbType.DateTime2).Value = x; command.Parameters.Add("@y", SqlDbType.DateTime2).Value = x.AddDays(1.0); //Cek ada

    // i am modifying from here... object _ov = command.ExecuteScalar(); string_value = "0"; if (!(_ov is System.DBNull)) _value = _ov.ToString(); label2.Text = _value;



    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Tuesday, November 17, 2015 10:03 AM
  • Thanks All of My Friends

    I just found the problem, and get the solution for these

    I can't use MAX for the script (for SQL Server Query)

    then I have to change it to count (not max)

    and I can't use if...else...(it won't work)

    so I change if..else... with While ...(condition)....

    and it works !!!, it show value 0 (if no value in table)...and it will show the value (if there is a value in table)

    Thank you very much my friends for helping me to get solution for this problem :)

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:11 AM
    Tuesday, November 24, 2015 7:07 AM