none
Encounter occurs when return NULL value to the textbox

    Question

  • Hi,

    i am retrieving data using data reader. Whenever a null value fetches from the column, the compiler encountering an error.  The error occurs when column value contains NULL. I am using Visual Basic 2005.

    Please look at the following error which is in red color:

    DEVNA11.Checked = IIf(IsDBNull(dr("dev_serv_desc_final_na")), 0, dr("dev_serv_desc_final_na"))

    txtPlanServDesFinal.Text = IIf(IsDate(dr("dev_serv_desc_final_planned")), Format(dr("dev_serv_desc_final_planned"), "dd/MM/yyy"), dr("dev_serv_desc_final_planned"))

    System.ArgumentException: Argument 'Expression' is not a valid value.



    Thanks.

    Regards
    Kashif


    Sunday, March 29, 2009 9:33 AM

Answers

  • Cant Help, since you have function call inside
    Note   The expressions in the argument list can include function calls. As part of preparing the argument list for the call to IIf, the Visual Basic compiler calls every function in every expression. This means that you cannot rely on a particular function not being called if the other argument is selected by Expression.
    Otherwise this would be a solution

    txtPlanServDesFinal.Text = IIf(IsDBNull(dr(

    "dev_serv_desc_final_planned")), "Date Null", IIf(IsDate(dr("dev_serv_desc_final_planned")), Format(CDate(dr("dev_serv_desc_final_planned")), "dd/MM/yyy"), "Wrong Format"))

    Still I say it loud, use the method I suggested above in a function, and format the same way as suggested, you wont get dd/MM/yyyy, just use string as a return value with appropriated calculation


    Arjun Paudel
    • Marked as answer by Xingwei Hu Thursday, April 02, 2009 5:34 AM
    Sunday, March 29, 2009 11:27 AM

All replies

  • You have to use nested IIF and its not  good to use multiple IIF, I can not remember but I had encountered some funny probelm. So in above case, you have to first check whether data is null, if not again check whether data is date, so better use if else statement, that with provide more readable and maintanable code

    If Not IsDBNull(dr("dev_serv_desc_final_planned")) Then
    
                Dim date1 As Date
    
                If Date.TryParse(dr("dev_serv_desc_final_planned").ToString, date1) Then
    
                    txtPlanServDesFinal.Text = Format(date1, "dd/MM/yyyy")
    
                Else
    
                    txtPlanServDesFinal.Text = "Format Wrong"
    
                End If
    
            Else
    
                txtPlanServDesFinal.Text = "Date Null"
    
            End If
    
    

    Arjun Paudel
    Sunday, March 29, 2009 10:40 AM
  • I have too many dates field(around 150) which i am going to retrieve in the textboxes.  I can use the above condition in some function then call each time by sending parameter(checkdatefunction(dr("fieldname")).

    I already tried the same kind of method, it works perfectly. But i want to retrieve using the IIF statment.

    i checked IIF statement when using with Format(dr("fieldname"),"dd/MM/yyyy") returns text as   dd/MM/yyyy. No date.

    But when i replace with Format(dr("fieldname"),"Short Date"), it returns short date.


    if possible please try it with IIF.

    Thanks.

    Regards
    Arshad
    Sunday, March 29, 2009 11:08 AM
  • Cant Help, since you have function call inside
    Note   The expressions in the argument list can include function calls. As part of preparing the argument list for the call to IIf, the Visual Basic compiler calls every function in every expression. This means that you cannot rely on a particular function not being called if the other argument is selected by Expression.
    Otherwise this would be a solution

    txtPlanServDesFinal.Text = IIf(IsDBNull(dr(

    "dev_serv_desc_final_planned")), "Date Null", IIf(IsDate(dr("dev_serv_desc_final_planned")), Format(CDate(dr("dev_serv_desc_final_planned")), "dd/MM/yyy"), "Wrong Format"))

    Still I say it loud, use the method I suggested above in a function, and format the same way as suggested, you wont get dd/MM/yyyy, just use string as a return value with appropriated calculation


    Arjun Paudel
    • Marked as answer by Xingwei Hu Thursday, April 02, 2009 5:34 AM
    Sunday, March 29, 2009 11:27 AM