Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ConnectionStringa nd Web.config

ConnectionStringa nd Web.config

Scheduled Pinned Locked Moved ASP.NET
csharpdatabaselinqhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    future3839
    wrote on last edited by
    #1

    HI, I write the following code to connect to my database

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.ProviderBase;
    using System.Configuration;

    namespace Hostel.Models.DataAccessLayer
    {
    public class PersonDataSet
    {
    DataSet DS = new DataSet();
    SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HostelConnectionString"].ToString());

        // Create the DataTable "Person\_prs" in the Dataset and the its DataAdapter
        SqlDataAdapter PersonDataAdapter = new
        SqlDataAdapter(new SqlCommand("SELECT \* FROM Person\_prs", Conn));          
    }
    

    }

    but after running get this error: Error 1 A field initializer cannot reference the non-static field, method, or property 'Hostel.Models.DataAccessLayer.PersonDataSet.Conn' does any one has any idea?

    T 1 Reply Last reply
    0
    • F future3839

      HI, I write the following code to connect to my database

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Data;
      using System.Data.SqlClient;
      using System.Data.ProviderBase;
      using System.Configuration;

      namespace Hostel.Models.DataAccessLayer
      {
      public class PersonDataSet
      {
      DataSet DS = new DataSet();
      SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HostelConnectionString"].ToString());

          // Create the DataTable "Person\_prs" in the Dataset and the its DataAdapter
          SqlDataAdapter PersonDataAdapter = new
          SqlDataAdapter(new SqlCommand("SELECT \* FROM Person\_prs", Conn));          
      }
      

      }

      but after running get this error: Error 1 A field initializer cannot reference the non-static field, method, or property 'Hostel.Models.DataAccessLayer.PersonDataSet.Conn' does any one has any idea?

      T Offline
      T Offline
      thatraja
      wrote on last edited by
      #2

      Initialize the constructor. Also didn't contain any methods. Try like this

      public class PersonDataSet
      {

          SqlConnection Conn = new SqlConnection();  
      public PersonDataSet
      {
          Conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["HostelConnectionString"\].ToString());
      }
      
      public MethodPersonDataSet
      {
          DataSet DS = new DataSet();
            // Create the DataTable "Person\_prs" in the Dataset and the its DataAdapter
          SqlDataAdapter PersonDataAdapter = new
          SqlDataAdapter(new SqlCommand("SELECT \* FROM Person\_prs", Conn));    
      }      
      

      }

      thatraja |Chennai|India|


      Tips/Tricks|Brainbench certifications

      Do what you want quickly because the Doomsday on 2012 :-)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups