Sunday, February 26, 2012

error with connection with SQL SERVER

Hi

I am developing web application.

I have connected my app. with sql server using web.config as code given below using Named pipe connection

<appSettings>
<add key="constring" value="Server=ebserver;UID=sa;database=Airport-Clearance;Integrated Security=SSPI;network library=dbnmpntw;" />
</appSettings
After than i have added a new CLASS in my app.
in that, I have written as given below code.

public SqlConnection GetConnection()
{
System.Configuration.AppSettingsReader objApp = new System.Configuration.AppSettingsReader ();
SqlConnection sqlconn = new SqlConnection ();
sqlconn.ConnectionString = Convert.ToString(objApp.GetValue("constring", typeof(string)));
return sqlconn ;
}

Finally, In load event of form, I have written as below code.

Class1 objcs = new Class1() ;
SqlConnection sqlconn = objcs.GetConnection ();
SqlDataAdapter filling = new SqlDataAdapter("select * from Airport",sqlconn);
DataSet ds = new DataSet();
sqlconn.Open();
//filling.Fill(ds,"Airport");

//DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables["Airport"];
dg.DataBind();
//Do what ever
sqlconn.Close ();

When i run this application

It gives error like

provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

If i use try catch then it doesnt display any error.but Even it doesnt display datagrid on form.

Please give me solution.

Even how to run app. line by line as we run in VB using F8.

Reply please,

Regards,
ASIFTry this instead (assuming you're not using beta version):

public SqlConnection GetConnection()
{
SqlConnection sqlconn = new SqlConnection ();
sqlconn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
return sqlconn ;
}

No comments:

Post a Comment