Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# list from ms sql db

Posted on 2013-10-22
9
289 Views
Last Modified: 2013-10-22
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

0
Comment
Question by:QuinnDex
  • 4
  • 4
9 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 39592591
It is a static void method to you nee to change the voi keyword to the type you are trying to return
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39592592
Which is List<center>
0
 
LVL 9

Author Comment

by:QuinnDex
ID: 39592637
yep the return keyword is now accepted but everything else no longer works
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 9

Author Comment

by:QuinnDex
ID: 39592643
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\
0
 
LVL 9

Author Comment

by:QuinnDex
ID: 39592655
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\
0
 
LVL 9

Accepted Solution

by:
sachinpatil10d earned 500 total points
ID: 39592664
Try this

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 List<centers> 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();

        List<centers> 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

0
 
LVL 22

Expert Comment

by:p_davis
ID: 39592671
Can you make the method non static
0
 
LVL 9

Author Comment

by:QuinnDex
ID: 39592677
perfect thank you
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39592681
Ya know what....technically i answered the posted question
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question