开发者

Strange Behaviour when comparing to null

开发者 https://www.devze.com 2023-04-12 09:34 出处:网络
I have a gridview taking data from object data source. The object data source is taking data from a method which takes one parameter (string). The parameter is supplied from page url using querystring

I have a gridview taking data from object data source. The object data source is taking data from a method which takes one parameter (string). The parameter is supplied from page url using querystring, and the default value is set to null. In the method taking parameter, I am trying to check if the parameter is null return all data else return data with id got from the parameter - eg. code.

public list<string> MyMethod (string param)
    {
        if(param == null)
    {
       return // all
    }
    else
    {
       return // id with param
    }
    }

I tried to debug the program and the param is actually null 开发者_运维技巧but the if () statement is always going to "false" condition. i.e. param == null always evaluating to false. I tried String.IsNullorEmpty(param), and it still is evaluating to false. I don't understand what the problem is. Please help. Many Thanks.

Strange Behaviour when comparing to null


Have you tried stepping through and debugging (assuming you're using visual studio)?

Put a breakpoint on your method signature and then hover over with your mouse on the param variable to see what it's value is after each step.

Debugging will help you identify the source of issues like this pretty fast.

Edit

The screenshot you added shows your param variable is equal to the string "null", not null.

You can change your if statement to if(param == "null") and that should work, but the real fix for this is most likely to not use the string "null" at all so that'll require editing wherever you assign the variable you pass to the function.

Also, your code sample is param == null yet your screenshot is param != null. The != means not equal, I'm not sure if that's just a typo or you haven't noticed.


Let me guess. It could be the case that your input parameter param has "null" and not null. For example, in your case

if (param == null) // you're comparing "null" == null which will always be false.

Edit

Suggesstion: add param to watch window and check what's the value. I'm pretty sure in your case it is something else than null.


Are you absolutely sure the object is null, there is a big difference between an empty string and a null object!!

if(String.IsNullorEmpty(param))
{
return list;
}
else
{
return list.findIndex(param);
}


After you added screenshot now we can see that you passing to method "null" parameter not null.

I'am not familiar with QueryString but maybe this helps:

If you using QueryStringParameter you can set these properties to get null instead "null":

<asp:QueryStringParameter DefaultValue="" ConvertEmptyStringToNull="True" />

also you need to set

<asp:SqlDataSource CancelSelectOnNullParameter="False" />


Try:

if (String.IsNullOrEmpty(param))
{
}

With this syntax you can also set your string to "".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号