Click here to Skip to main content
15,881,678 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello here my code
C#
public List<bank> getall()
{
   return db.banks.ToString();
}


and i m getting error like
Cannot implicitly convert type 'string' to 'System.Collections.Generic.List

please help me ...?
Posted
Updated 6-Nov-12 7:18am
v2
Comments
Sergey Alexandrovich Kryukov 6-Nov-12 18:17pm    
Well, and what's unclear here? ToString() returns string, getall() should return List...
--SA

Erm...Can you seriously not see the problem here? If not, I suspect you should go back to basics and learn to program properly instead of guessing... The following should be obvious:

Your method is declared as returning a List (public List getall()) yet you are returning a string (return db.banks.ToString()). This is not only impossible (hence your error) but also complete nonsense code! Your return type is different from what you are trying to return! Also, ToString on the database's banks table will just return you a string (piece of text) telling you the C# type is a DataTable - it won't return you a massive string with all the records in it!

What were you trying to do? Return all the records from that database table? A somewhat questionable operation (since if the table is very big it could take a very long time to complete) but the code you probably want is:

C#
public List<bank> GetAll()
{
     return db.banks.ToList();
}
</bank>


Note: Method declaration now includes List<bank>. The word "bank" is the name of the type contained within your list. I have assumed (from your table name "banks") that the C# type (that is auto-generated by C# for you) that represents the data table types is called "bank".

Hope this helps,
Ed

P.s. If you still don't get it / have errors, I suggest you go back, learn how methods and types work (read MSDN) then search Google/MSDN/CodeProject for how to use Lists and how to use the database classes properly. Ideally you would be using an IQueryable not a List.
 
Share this answer
 
Comments
ridoy 6-Nov-12 13:27pm    
+5
Ed Nutting 6-Nov-12 13:28pm    
Thank you :)
RaisKazi 6-Nov-12 14:26pm    
Good answer. 5ed.
Ed Nutting 7-Nov-12 7:24am    
Thank you :)
Sergey Alexandrovich Kryukov 6-Nov-12 18:17pm    
Sure, a 5.
--SA
 
Share this answer
 
if you want to Retrun Generic List It have to be like this
XML
public List<Bank> getall()
       {
           return db.banks.ToList();
       }
 
Share this answer
 
Comments
Ed Nutting 6-Nov-12 13:23pm    
My 3. No explanation and not particularly helpful to the OP. Also, probably incorrect, given that the name of the table is "banks" with a lower case "b" it is likely the autogenerated type also has a lower case "b" i.e. "bank".

Ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900