0

How handle null value using object in C# ASP.NET C#.

Below I am doing visible true and false base on condition using hyperlink.

<asp:Label ID="Label3" runat="server"  Text="Date not confirm" 
     Visible='<%#GetVisible2(Eval("DateofEvent").ToString()=="")%>'></asp:Label>

below is my c# code.

public bool GetVisible2(object value)
{
    if (value=="")
    {
        //return value.ToString() == "Visible";
        return true;
    }
    return false;
}

Every time returning false only. I dont know if condition is correct or wrong.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
MMK
  • 109
  • 9
  • 1
    try searching in Google -- https://www.google.co.in/search?q=C%23+null+value+check&oq=C%23+null+value+check&aqs=chrome..69i57j69i58.4608j0j7&sourceid=chrome&ie=UTF-8 – Karthik AMR Jan 04 '17 at 07:57
  • I searched thats why came here for help, please help. @KarthikAMR – MMK Jan 04 '17 at 07:58
  • 1. http://stackoverflow.com/questions/12000228/how-to-determine-whether-object-reference-is-null 2. http://stackoverflow.com/questions/6417902/checking-if-an-object-is-null-in-c-sharp – Karthik AMR Jan 04 '17 at 08:02
  • Learn how to use the debugger. Then set a breakpoint in the method and check the type and value ob the passed value. – Tim Schmelter Jan 04 '17 at 08:03

5 Answers5

0

By default DateTime variable takes DateTime.MinValue so you should compare it with this as below:

<asp:Label ID="Label3" runat="server"  Text="Date not confirm" 
         Visible='<%#GetVisible2(Eval("DateofEvent"))%>'></asp:Label>

    public bool GetVisible2(DateTime? value)
    {
        if (value == null || value == DateTime.MinValue)
        {
            return true;
        }
        return false;
    }
Ankit Sahrawat
  • 1,306
  • 1
  • 11
  • 24
0

Because you passing your function a boolean value which never equals to empty string:

Eval("DateofEvent").ToString()=="" // this will generate true or false

this will pass boolean to GetVisible2. Try this:

<asp:Label ID="Label3" runat="server"  Text="Date not confirm" 
     Visible='<%#GetVisible2(Eval("DateofEvent").ToString())%>'></asp:Label>

and you can change function as:

public bool GetVisible2(string value)
{
   return String.IsNullOrEmpty(value);
}

or much cleaner approach as suggested by Tim Schmelter, without any validation method:

<asp:Label ID="Label3" runat="server"  Text="Date not confirm" 
     Visible='<%#String.IsNullOrEmpty(Eval("DateofEvent").ToStrin‌​g())%>'></asp:Label>
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • getting this error: Argument 1: cannot convert from object to string. @Zaheer Ahmed – MMK Jan 04 '17 at 08:04
  • 1
    @ZaheerAhmed: fine, but now this begs the question why he needs the method at all. `Visible='<%#String.IsNullOrEmpty(Eval("DateofEvent").ToString())%>'` – Tim Schmelter Jan 04 '17 at 08:15
  • @TimSchmelter I wanted to suggest but right now I wasn't able to confirm the syntax. So thanks for putting it here :) – Zaheer Ahmed Jan 04 '17 at 08:17
0

You are evaluating a function inside ASPX page and passing a Boolean value to your GetVisible2 function.

This is why you are returning false everytime, since the object value is never a string, but a boolean.

Should be

Eval("DateofEvent")

insteadof

Eval("DateofEvent").ToString() == ""

And the function will be

public bool GetVisible2(object value)
{
    return value!=null;
}

Or, if you want to consider empty strings "null" also, you can do the following

public bool GetVisible2(object value)
{
    var s = value as string;
    return value!=null || (s is string && !string.IsNullOrEmpty(value as string));
}


gettting error: object doest not contain a definiation for to string and no extension method tostring acception a first argument of type object could be found. @OrcusZ

It's not

toString()

but it's

ToString()

C# is case sensitive.

0

I think its better to pass the DateofEvent before converting it to string, because; if DateofEvent is null then the .ToString() will raise NullReference, So the changes would be like the following:

 <asp:Label ID="Label3" runat="server"  Text="Date not confirm" 
 Visible='<%#IsVisible(Eval("DateofEvent"))%>'></asp:Label>

So the parameter passed to the method will be an object, we have to check for null and then for Empty string, So the method will be like this:

public bool IsVisible(object value)
{
   return value != null && String.IsNullOrEmpty(value.ToString());
}

Or even something like this: return value != null && value.ToString()!="";

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • I think there is no need to use object as a parameter, your value will always be either null or a datetime so you should use DateTime? which can accept both null and datetime. Please check my answer also – Ankit Sahrawat Jan 04 '17 at 08:26
-1

A method exist to check is a string is null :

  if (String.IsNullOrEmpty(value))
    {
        return true;
    }
    return false;
OrcusZ
  • 3,555
  • 2
  • 31
  • 48
  • gettting error: object doest not contain a definiation for to string and no extension method tostring acception a first argument of type object could be found. @OrcusZ – MMK Jan 04 '17 at 08:01
  • if `value` is `null` then `.ToString()` will give you NullReferenceException` – sujith karivelil Jan 04 '17 at 08:02
  • 1
    `IsNullOrEmpty` is a static method in the string class, so you have to use `String.IsNullOrEmpty(value.ToString())`. But that doesn't help OP, because if this would work OP's code would also work. – Tim Schmelter Jan 04 '17 at 08:05
  • Your right, sorry is the morning in France, I need to take a coffe to wake up ^^ – OrcusZ Jan 04 '17 at 08:09
  • @OrcusZ: well, it's still not compiling because `value` is an object but `IsNullOrEmpty` takes a `string`. Also, you have to check if `value` is null otherwise you might get a `NullReferenceException` at `value.ToString`. Finally, this is the same as OP's `== ""`-check. But OP compares the return value of the method with `==""` which cannot work because the method returns a bool. – Tim Schmelter Jan 04 '17 at 08:12
  • if value is null, is still getting the issue, he should change the input param to string. if he need to stay with object he can just check if the object is null – OrcusZ Jan 04 '17 at 08:14