Solved

C# list from ms sql db

Posted on 2013-10-22
9
271 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 35
SQL JOIN 6 32
Hidden Field Value 10 34
sql calculate averages 18 25
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now