Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# list from ms sql db

Posted on 2013-10-22
9
Medium Priority
?
313 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

671 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