-2

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.

enter image description here

Flowerking
  • 2,551
  • 1
  • 20
  • 30
  • 4
    If it walks like a duck and talks like a duck, it must be a duck. Have you tried putting a debug break on the expression? It sounds like you're getting extra stuff in your param. – Joel Etherton Oct 11 '11 at 11:16
  • 2
    What happens if you put `Debug.Assert(param == null)` at the top of the method? – James Oct 11 '11 at 11:22
  • @JoelEtherton Agreed. It's not null. – Matt Oct 11 '11 at 11:32
  • @Flowerking Lemme guess. Are you debugging a Release build? – Ilian Oct 11 '11 at 11:48
  • Is it really null, so does `param.Equals("Hello World")` throw a null exception ? – V4Vendetta Oct 11 '11 at 11:48
  • Add screenshot of quickwatch with param variable. I see no one can understand whats wrong. – Renatas M. Oct 11 '11 at 11:53
  • @James , thanks for the tip, the value for param is null when i debug, but strangely debug.assert is failing. Lemme check my stack trace, thank you. – Flowerking Oct 11 '11 at 11:57
  • @Renuiz, please chk the screenshot. It shows that param is null and still stepping to true condition. – Flowerking Oct 11 '11 at 12:07
  • 3
    @Flowerking: The param is not `null`. You are passing the string `"null"`. Call `MyMethod(null)` not `MyMethod("null")`. – Ilian Oct 11 '11 at 12:10
  • I ammended my answer below with regards to the string "null" and also the fact your screenshot is != and your code sample is == (just in case). – AndrewC Oct 11 '11 at 12:16
  • @Ilian Pinzon Thank you so much. Dint notice that. Its working now. Mistake happened as the method is getting the parameter directly from object data source. Thank you so much. Its working when i compare the param == "null" instead of param == null. – Flowerking Oct 11 '11 at 12:17
  • @Flowerking Checking `param == "null"` is just exploiting a bug earlier in your code, because really an actual null value should be sent to the function in the first place. The problem is that if the string "null" is stored in your database, it will be treated the same as a null, which it shouldn't. – James Oct 11 '11 at 12:39

5 Answers5

2

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.

AksharRoop
  • 2,263
  • 1
  • 19
  • 29
  • 2
    You told that my answer doesn't help, but OP told that he already did what you're suggesting... so you're doing my same mistake, don't you think? So or we trust user or we don't and suggest everything... – Marco Oct 11 '11 at 11:26
  • It is a common mistake I have seen while mentoring new joiners that they see value is null, but looking at again it is "null". And I never said your answer doesn't help! – AksharRoop Oct 11 '11 at 11:31
  • Checking to see if the variable is equal to **null** is exactly what **IsNullOrEmpty** is doing. We need more information before the correct answer can be provided. – Security Hound Oct 11 '11 at 11:32
  • 2
    See the magic of stackoverflow, first correct answer is at the bottom with -1 vote! – AksharRoop Oct 11 '11 at 12:29
2

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.

AndrewC
  • 6,680
  • 13
  • 43
  • 71
  • Hi andy, thanks for reply. But i tried debugging and it is clearly getting null passed to it as parameter, I tried String.IsNullorEmpty(param), i even tried param.CompareTo(null) == 0, strangely nothing is working. – Flowerking Oct 11 '11 at 11:50
  • 1
    Can you show the code where you assign the variable that you pass to the method, and then the code where you actually call the method? – AndrewC Oct 11 '11 at 12:00
  • sorry but this is only part of my project. The method is in a different project which is using linq to sql to get data from db. And i am callin it from a web form from a different project of the same solution – Flowerking Oct 11 '11 at 12:06
  • see ammended answer Flowerking. – AndrewC Oct 11 '11 at 12:16
2

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);
}
Rob
  • 4,927
  • 12
  • 49
  • 54
  • If you change the **param == null** to **String.String.IsNullOrEmpty(param)** I will upvote your answer trying to provide the author cleaner code. Still does not address the underline problem, we simply do not know, what the author is tyring to do. – Security Hound Oct 11 '11 at 11:35
  • You're right, as always the 'problem' is probably because the object isn't what the author thinks it is.. Fixed anyway – Rob Oct 11 '11 at 11:37
  • 2
    @Ramhound: I don't understand. You first say you don't understand why people upvoted my answer and then suggest Rob to write the same thing? – Marco Oct 11 '11 at 11:40
  • Dude you gonna whine about me not writing List list = new List();?? QQ MOAR – Rob Oct 11 '11 at 11:40
2

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" />
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
1

Try:

if (String.IsNullOrEmpty(param))
{
}

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

Marco
  • 56,740
  • 14
  • 129
  • 152
  • 5
    Why did 3 people upvote this answer? The author of the question clearly says they already attempted to do this. – Security Hound Oct 11 '11 at 11:19
  • 2
    @Ramhound: probably because (read Joel Etherton comment) user is trying to do something wrong and my syntax is better than the other... Anyway I don't think downvoting my answer will give OP great help, given that my answer is not wrong. Thanks anyway. – Marco Oct 11 '11 at 11:23
  • @Marco - Your answer does not address his problem. While he might be doing something the wrong way we don't actually know what he is trying to do. So we cannot come to that conclusion. – Security Hound Oct 11 '11 at 11:31
  • String.IsNullOrEmpty is not a always better way. For all we know the OP might want different behaviour for "" to null. – Matt Oct 11 '11 at 11:36
  • @Matt: I agree with you. Anyway OP is telling us he already debugged his code and param is null.... so I think there must be something wrong in what he says... so I tried to give him a different solution, using an empty string in place of a null. In this case he could debug something different and tell us it it worked for him or not... – Marco Oct 11 '11 at 11:46
  • @Marco - The user was wrong. He was getting the value **"null"** which IS NOT **NULL**. – Security Hound Oct 11 '11 at 19:46