Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
hello here my code
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 6-Nov-12 6:58am
Edited 6-Nov-12 7:18am
v2
Comments
Sergey Alexandrovich Kryukov at 6-Nov-12 18:17pm
   
Well, and what's unclear here? ToString() returns string, getall() should return List...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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:
 
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.
  Permalink  
Comments
ridoy at 6-Nov-12 13:27pm
   
+5
Ed Nutting at 6-Nov-12 13:28pm
   
Thank you :)
RaisKazi at 6-Nov-12 14:26pm
   
Good answer. 5ed.
Ed Nutting at 7-Nov-12 7:24am
   
Thank you :)
Sergey Alexandrovich Kryukov at 6-Nov-12 18:17pm
   
Sure, a 5.
--SA
Ed Nutting at 7-Nov-12 7:24am
   
Thank you :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

if you want to Retrun Generic List It have to be like this
public List<Bank> getall()
       {
           return db.banks.ToList();
       }
  Permalink  
Comments
Ed Nutting at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  

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

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 CPallini 189
3 Sergey Alexandrovich Kryukov 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,958
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100