not all code paths return a value

Posted by Agk484 under Regular Expressions on 12/1/2010 | Points: 10 | Views : 2966 | Status : [Member] | Replies : 11
public DataSet GetOffers()
{
try
{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
using (da)
{
da.Fill(ds);
}
return ds;
}
catch (Exception ex)
{
ex.ToString();
}
}

Appu


Responses

Posted by: T.saravanan on: 12/1/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi agk484,

what's your problem ? can you explain briefly ? nobody can not understand your problem...


Thanks,
T.Saravanan

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/1/2010 [Member] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Use this.

public DataSet GetOffers() 

{
try
{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
using (da)
{
da.Fill(ds);
}
return ds;
}
catch (Exception ex)
{
return null;
}
}


www.abhisheksur.com

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hetalkumar1980 on: 12/1/2010 [Member] Starter | Points: 25

Up
0
Down
Hi the problem in your code is that the method 'GetOffers' returns a dataset object. But from the catch block of your code you are not returning anything. That's the reason why you are getting this error. As per the .Net CLR, if any function is returning value then each code path should be returning some value (of the return type of the function, in your case DataSet) or null value. Please try the following code:

public DataSet GetOffers() 

{
DataSet ds =null;
try
{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
ds = new DataSet();
using (da)
{
da.Fill(ds);
}
}
catch (Exception ex)
{
throw ex;
}
return ds;
}


Hetalkumar M Kachhadiya

http://dotnetsqlinterview.blogspot.com

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/1/2010 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Abhi2434 your example code is incorrect Agk484 code intended to return a Dataset , and the code you sent wont compile. The code sent by Hetalkumar1980 is the Correct one. Please make sure next time.

Thank you for posting at Dotnetfunda

Vuyiswa Maseko



Thank you for posting at Dotnetfunda
[Administrator]

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gokul on: 12/1/2010 [Member] Starter | Points: 25

Up
0
Down
In out of the ry and catch block give as -return ds as shown :



public DataSet GetOffers()
{
try
{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
using (da)
{
da.Fill(ds);
}
return ds;
}
catch (Exception ex)
{
ex.ToString();
}
return ds;

}

Thanks and Regards,
GokulNath Nithy.

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 12/1/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Gokul,

In your code is small error.you will declare a DataSet in try section. Just declare in above try section similar to HetalKumar1980 answer.
Correct the changes...

Thanks,
T.Saravanan

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/1/2010 [Member] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
@Vuyiswamb

Well, why do you think the code will not compile. Dataset is Nullable so it can be cast to null without any problem. And I think if the developer does not want to handle the error, he should not be using try catch block anyway.

Catch block should return null if he wants to handle the exception.

and also, by the way, I have compiled the same code without any error.
Regards

www.abhisheksur.com

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/1/2010 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Dear Abhi2434
but the Main reason for him to post his problem was that the Error

not all code paths return a value


Now this means he created a Function and Forgot to return something, now if he forgot to return something, if you are the person to help , you need to find out what is it that he wanted to return. Looking at his function definition you can see he wanted to return a dataset , what Good will it do if he return "null"

I misjudged your code, it was going to compile, i am sorry about that, but it is not a good programming technique.

Thank you for posting at Dotnetfunda

Vuyiswa Maseko


Thank you for posting at Dotnetfunda
[Administrator]

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/5/2010 [Member] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Well, I got your point, but I do not agree with your point that my code is not good in programming point of view.
According to Microsoft, it says, you should only put try/catch when you can handle the exception. If you cannot handle the exception, you should not be handling the exception using try/catch, rather it should automatically throw the exception to the calling environment. So, as far this note is concerned anything as replied by Hetalkumar1980 or Gokul does not make any sense to me. So according to me, the code should look like either :

public DataSet GetOffers()

{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
using (da)
{
da.Fill(ds);
}
return ds;
}

without any try catch around, and letting the caller to handle the exception in the method or

public DataSet GetOffers() 

{
try
{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
using (da)
{
da.Fill(ds);
}
return ds;
}
catch (Exception ex)
{
// Handle your exception . (say log exception or something else)
return null;
}
}


which is my previous solution, which intentionally returns null after handling exception (rather than rethrow the exception when exception occurs, which doesnt makes sense as we could easily get rid of the try catch for that)

I hope you will also agree with me.

www.abhisheksur.com

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/5/2010 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
hi Abhi2434

i fully agree with you on that one, but what i am saying is that , posted his exception because he created a function and forget to return what the function was supposed to return.

Thank you for posting at Dotnetfunda
[Administrator]

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/5/2010 [Member] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi Vuyiswamb,

Thank you. But as far as the initial code is concerned, the function only returns from the try block, the person actually forget to return from the catch block. That is why the initial error occurred.

Anyways, I think the problem is solved now.
Cheers

www.abhisheksur.com

Agk484, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response