locked
Else not firing within if Statement RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I have an if statement that checks if a value from my repo is null but the else statement does not fire even though there is a value within the database. Please see below my code.

    if (!IsPostBack)
    {
    var SLDS = repo.Find(Int32.Parse(Request.QueryString["id"]));
    ID = Request.QueryString["id"];

    if (string.IsNullOrEmpty(AppBkdTextBox.Text)) { AppBkdTextBox.Text = null; } else { AppBkdTextBox.Text = SLDS.AppBkdDate.ToString("d"); }

    All other values pulled from the repo are pulled into my textboxes with no issue.

    When amending my code and adding an ! the value appears from the database but when the value is blank with no value in the database the Textbox is populated with "0001-01-01"

     if (!(string.IsNullOrEmpty(AppBkdTextBox.Text)))
                    {
                        AppBkdTextBox.Text = null;
                    }
                    else
                    {
                        AppBkdTextBox.Text = SalesLeads.AppBkdDate.ToString("d");
                    }

    Any help would be greatly appreciated.

    Many thanks

    Jonny

    Thursday, August 1, 2019 10:46 AM

Answers

  • User-719153870 posted

    Hi Jonny,

    As we can see in your code, you are trying to check if the textbox's text is null or empty in the pageload event before you actually put a value into the textbox?

    My best guess is that what you really want to check is the value of SLDS.AppBkdDate.

    If so, you can modify your code as below:

    if (SLDS.AppBkdDate== DateTime.MinValue)
    {
    AppBkdTextBox.Text = null;
    }
    else
    {
    AppBkdTextBox.Text = SLDS.AppBkdDate.ToString("d");
    }

    For the value '0001-01-01', this is a default minvalue for datetime type value. If your SLDS.AppBkdDate is 'null', .ToString("d") method will make it '0001-01-01'.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 3:18 AM

All replies

  • User475983607 posted

    I have an if statement that checks if a value from my repo is null

    Your code checks if the AppBkdTextBox.Text is empty not if there is a value returned by the repo.

    if (!IsPostBack)
    {
    	var SLDS = repo.Find(Int32.Parse(Request.QueryString["id"]));
    	ID = Request.QueryString["id"];
    
    	if (string.IsNullOrEmpty(AppBkdTextBox.Text))
    	{
    		AppBkdTextBox.Text = null;
    	}
    	else
    	{
    		AppBkdTextBox.Text = SLDS.AppBkdDate.ToString("d");
    	}

    Please use the Visual Studio debugger to verify the logic you write matches your expectation.

    Thursday, August 1, 2019 11:00 AM
  • User753101303 posted

    Hi,

    And AppBkDate is a DateTime or a nullable DateTime ie "DateTime?".

    If AppBkDate is a non nullable DateTime and the db side value is NULL, I quite expect that it will be just left to its default value which is 01/01/0001.

    BTW "I have an if statement that checks if a value from my repo is null" is not really what you are doing. You are testing the value for the textbox to which you are about to write. Could it be that you want to test SLDS.AppBkDate instead ?

    Thursday, August 1, 2019 11:21 AM
  • User-719153870 posted

    Hi Jonny,

    As we can see in your code, you are trying to check if the textbox's text is null or empty in the pageload event before you actually put a value into the textbox?

    My best guess is that what you really want to check is the value of SLDS.AppBkdDate.

    If so, you can modify your code as below:

    if (SLDS.AppBkdDate== DateTime.MinValue)
    {
    AppBkdTextBox.Text = null;
    }
    else
    {
    AppBkdTextBox.Text = SLDS.AppBkdDate.ToString("d");
    }

    For the value '0001-01-01', this is a default minvalue for datetime type value. If your SLDS.AppBkdDate is 'null', .ToString("d") method will make it '0001-01-01'.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 3:18 AM