error not all code paths return a value

Posted by Klbaiju under Regular Expressions on 9/6/2009 | Views : 7878 | Status : [Member] | Replies : 6
when i try to build this file
this error is showing how to solve this


public Table<ClsCustomer> getcustomers(out string msg)
{
try
{
DataContext Db = new DataContext("Data Source=BAIJU;Initial Catalog=baiju;Integrated Security=True");
Table<ClsCustomer> customer = Db.GetTable<ClsCustomer>();
msg = "Success";
return customer;

}
catch (Exception ex)
{
msg= ex.Message;

}

}




Responses

Posted by: Vuyiswamb on: 9/6/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
This Simply means that you have declared a Function and a function always return something and at the end you did not return anything. Now this can be simple resloved like this

public Table<ClsCustomer> getcustomers(out string msg)

{
try
{
DataContext Db = new DataContext("Data Source=BAIJU;Initial Catalog=baiju;Integrated Security=True");
Table<ClsCustomer> customer = Db.GetTable<ClsCustomer>();
msg = "Success";
return customer;

}
catch (Exception ex)
{
msg= ex.Message;

}
return customer;
}


Thank you for posting at dotnetfunda

Vuyiswa Maseko


Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Klbaiju on: 9/7/2009 [Member] Starter

Up
0
Down
no this is not working
error is
the name customer does not exist in the current context
any solution

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

Posted by: Vuyiswamb on: 9/7/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Sorry Typo, your Code should look like this


public Table<ClsCustomer> getcustomers(out string msg)

{
Table<ClsCustomer> customer;

try
{
DataContext Db = new DataContext("Data Source=BAIJU;Initial Catalog=baiju;Integrated Security=True");
customer = Db.GetTable<ClsCustomer>();
msg = "Success";

}
catch (Exception ex)
{
msg= ex.Message;

}
return customer;

}


Thank you for posting at Dotnetfunda



Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Klbaiju on: 9/7/2009 [Member] Starter

Up
0
Down
i tried your code
but showing error
you didn't return value in try block.
i tried both methods



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

Posted by: Rtpharry on: 9/7/2009 [Member] [MVP] Bronze

Up
0
Down
Thats because if the try {} fails then you dont return a customer object.

Try putting either

return null;

or
return new Table<ClsCustomer>(); 

at the end so it looks like this:

public Table<ClsCustomer> getcustomers(out string msg)

{
try
{
DataContext Db = new DataContext("Data Source=BAIJU;Initial Catalog=baiju;Integrated Security=True");
Table<ClsCustomer> customer = Db.GetTable<ClsCustomer>();
msg = "Success";
return customer;

}
catch (Exception ex)
{
msg= ex.Message;
}

return new Table<ClsCustomer>();
}


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

Posted by: Klbaiju on: 9/7/2009 [Member] Starter

Up
0
Down
thanks it is now working

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

Login to post response