Not all code returns a value error in asp.net

Posted by Prabu_Spark under ASP.NET on 11/5/2012 | Points: 10 | Views : 925 | Status : [Member] | Replies : 7
Hi sir,
I am getting error "Not all code returns a value error in asp.net". I attached the code below.
Kindly give me the solution for this.


public DateTime get_lastsearch()
{
try
{
DataSet lastsearch_ds = new DataSet();
db.executequery("SELECT max(searchdate) FROM tbl_search_log", out lastsearch_ds, out count);
DateTime search_date = Convert.ToDateTime(lastsearch_ds.Tables[0].Rows[0]["max(searchdate)"].ToString());
return search_date;
}
catch (Exception ex)
{
return null;
}
}

With regards,
J.Prabu
[Email:prbspark@gmail.com]

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Saratvaddilli on: 11/5/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
remove null in the catch block

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

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

Posted by: Pooraniidhayabalama on: 11/5/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

"DateTime" is a non-nullable datatype. So you cannot return null value in the catch section. Instead of this u can return DateTime.MinValue or new instance of DateTime.

Code :

catch (Exception ex)
{
return DateTime.MinValue;
//return new DateTime();
// return null;
}

Regards,
Poorani

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

Posted by: Saratvaddilli on: 11/5/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
if you resolved your problem then please mark as answer

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

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

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

Up
0
Down
Every Function must return something , i always say that code like this is not good
What you did here will result into an Error, if there is no exception thrown it means your funtion will not return anything and it defeat the purpose of your function , change your code to look like this

public DateTime get_lastsearch() 

{
try
{
DataSet lastsearch_ds = new DataSet();
db.executequery("SELECT max(searchdate) FROM tbl_search_log", out lastsearch_ds, out count);
DateTime search_date = Convert.ToDateTime(lastsearch_ds.Tables[0].Rows[0]["max(searchdate)"].ToString());
return search_date;
}
catch (Exception ex)
{
throw ex;
}
return return search_date;
}


Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Kalaikathiravan on: 11/5/2012 [Member] Starter | Points: 25

Up
0
Down
I think between the Try...Catch Block don't use return type Its not return any values

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

Posted by: Pooraniidhayabalama on: 11/5/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try this code.

public DateTime get_lastsearch()
{
DateTime search_date;
try
{
DataSet lastsearch_ds = new DataSet();
db.executequery("SELECT max(searchdate) FROM tbl_search_log", out lastsearch_ds, out count);
search_date = Convert.ToDateTime(lastsearch_ds.Tables[0].Rows[0]["max(searchdate)"].ToString());
//return search_date;
}
catch (Exception ex)
{
throw ex;
}
return search_date;

}

Regards,
Poorani

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

Posted by: Rajesh081725 on: 11/5/2012 [Member] Bronze | Points: 25

Up
0
Down
public DateTime get_lastsearch()
{
try
{
DataSet lastsearch_ds = new DataSet();
db.executequery("SELECT max(searchdate) FROM tbl_search_log", out lastsearch_ds, out count);
DateTime search_date = Convert.ToDateTime(lastsearch_ds.Tables[0].Rows[0]["max(searchdate)"].ToString());
return search_date;
}
catch (Exception ex)
{
msgbox(ex.message.toString); }
}



Time is Gold
Thanks & Regards,
Rajesh Kumar,
9962038582.

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

Login to post response