Link to home
Start Free TrialLog in
Avatar of QuinnDex
QuinnDex

asked on

C# list from ms sql db

I cant get this class to accept a return parameter

it tells me it returns void

can someone please explain where i am going wrong.


public class centers
{
     public int ID { get;set; }
     public string CenterName { get;set; }
     public string Adress1 { get;set; }
     public string Adress2 { get;set; }
     public string Town { get;set; }
     public string County { get;set; }
     public string Postcode { get;set; }
     public string Phone { get;set; }
     public string Fax { get;set; }
}


       
public class centerlist
{
    public static void ReadCenters(string TableName) 
    {
        string sql = "select * from @TableName";
        String connectionString = ConfigurationManager.ConnectionStrings["foundationConnectionString"].ConnectionString;
        SqlConnection SqlConn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(sql, SqlConn);



        cmd.Parameters.Add(new SqlParameter("@TableName", "centers"));

        SqlConn.Open();
       
        cmd.ExecuteReader();
        SqlDataReader dr = cmd.ExecuteReader();

        var  centers = new List<centers>();

                   while (dr.Read())
           {
               centers.Add(new centers
                          {
                              ID = dr.GetInt32(0), 
                              CenterName = dr.GetString(1),
                              Adress1 = dr.GetString(2),
                              Adress2 = dr.GetString(3) ,
                              Town = dr.GetString(4),
                              County = dr.GetString(5),
                              Postcode = dr.GetString(6),
                              Phone = dr.GetString(7) ,
                              Fax = dr.GetString(8)
                          });
           }



        cmd.Dispose();
        SqlConn.Close();
        SqlConn.Dispose();

        return centers;
    }



}

Open in new window

Avatar of p_davis
p_davis

It is a static void method to you nee to change the voi keyword to the type you are trying to return
Which is List<center>
Avatar of QuinnDex

ASKER

yep the return keyword is now accepted but everything else no longer works
example

Error      34      'centerlist.cmd' is a 'field' but is used like a 'type'      C:\Documents and Settings\Chris\My Documents\Visual Studio 2010\WebSites\Foundation\App_Code\insertPD.cs      126      9      C:\...\Foundation\
the problems seem to stem from this


Error      31      A field initializer cannot reference the non-static field, method, or property 'centerlist.connectionString'      C:\Documents and Settings\Chris\My Documents\Visual Studio 2010\WebSites\Foundation\App_Code\insertPD.cs      121      51      C:\...\Foundation\
ASKER CERTIFIED SOLUTION
Avatar of sachinpatil10d
sachinpatil10d
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you make the method non static
perfect thank you
Ya know what....technically i answered the posted question