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.
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;
}
}
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>
ASKER
yep the return keyword is now accepted but everything else no longer works
ASKER
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\A pp_Code\in sertPD.cs 126 9 C:\...\Foundation\
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\A
ASKER
the problems seem to stem from this
Error 31 A field initializer cannot reference the non-static field, method, or property 'centerlist.connectionStri ng' C:\Documents and Settings\Chris\My Documents\Visual Studio 2010\WebSites\Foundation\A pp_Code\in sertPD.cs 121 51 C:\...\Foundation\
Error 31 A field initializer cannot reference the non-static field, method, or property 'centerlist.connectionStri
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you make the method non static
ASKER
perfect thank you
Ya know what....technically i answered the posted question