ConnectionStringa nd Web.config
-
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? -
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?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 :-)