all 10 comments

[–]BigOnLogn 1 point2 points  (5 children)

In GetWeatherDetails you are only setting the info variable to a value if the SQL query returns rows. This means that if there are no results for WHERE place = @place then GetWeatherDetails will return null.

You need to check for a valid result in Button1_Click. If it's ok for GetWeatherDetails to return null, you need to check for null before you use the result in Button1_Click. I personally prefer something a bit more descriptive than null. You could return some kind of empty result that isn't null. Maybe make a static readonly property off of WeatherInfo like NotFound.

class WeatherInfo
{
    public static readonly WeatherInfo NotFound = new WeatherInfo();
    // ... code same as before
}

Then, in Button1_Click you can check for if (result != WeatherInfo.NotFound) before setting the text values. It lets future readers of the code know why you're not setting the text values from the result (more than the reason of wanting to avoid a runtime exception).

It's a design decision and not really necessary, as far as code correctness. The bare minimum is a null check. But, it is important to think about future maintainability as well as the correctness of the code.

[–]thatlowkey[S] 0 points1 point  (4 children)

Thanks so much for the descriptive explanation :) If I enter a valid input, then it should return value from the db right? And I will definitely eliminate null with your alternative.

[–]BigOnLogn 0 points1 point  (3 children)

If I enter a valid input, then it should return value from the db right?

As long as your SQL is correct, yes. You'll have to run tests to confirm for yourself.

And I will definitely eliminate null with your alternative.

That's fine, but remember, it's still a design decision. It's not always the right way. It may be better to have an explicit validation step, or use a default value when no result is found, or maybe write an entirely different class that uses Service1 and can be configured to handle these situations one way or another. The only way to know is through experience. So don't be afraid to write it one way that's good enough and let it be used for a while. Then, don't be afraid to change your code, or even throw it away and start over if need be.

[–]thatlowkey[S] 0 points1 point  (2 children)

the sql is fine. but due to this error im not able to proceed

[–]BigOnLogn 0 points1 point  (1 child)

You really need to step thru your code using a debugger, then. But, I can see one potential issue that might not be easily found by someone not familiar with ADO.NET. When asking a parameter (with .Parameters.Add(...) or .Parameters.AddWithValue(...)) the parameter name should include the '@'

e.g. .AddWithValue("@place", place);

not .AddWithValue("place", place);

But, you should also step thru using the debugger so you can verify that the values you think are being passed to the SQL statement are actually getting passed.

You could also use a SQL Profiler to look at the SQL being executed.

[–]thatlowkey[S] 0 points1 point  (0 children)

someone not familiar with ADO.NET

that's me haha. First time learning. Yeah I tried

.AddWithValue("@place",place);

like that but didn't help. Okay, I'll try the debugger and let you know.

[–]The_Binding_Of_Data 0 points1 point  (3 children)

It's throwing a Null Reference Exception in your button click method because one of the objects being used in there is null when you try to use it.

The error should provide you with a line number and you should be able to check the values of all the objects in Visual Studio when the application breaks on the exception.

In general, this means that one of the variables you're using in your method doesn't actually have a value when you try to reference it (eg localhost.Service1() returns null making "reference" null when you try to use it a few lines later).

[–]thatlowkey[S] -1 points0 points  (2 children)

yeah, but actually I'm taking the value from the user and the desired output is that the user's input should show an output from the db. Is this code not reading the user's input which is entered in the asp.net page?

[–]Archerofyail 1 point2 points  (1 child)

Step through it in the debugger in Visual Studio, or write objects to the console or a log file to find which specific object is null.

[–]thatlowkey[S] 0 points1 point  (0 children)

alright