Solved

C# list from ms sql db

Posted on 2013-10-22
9
298 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
[X]
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
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

752 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