Friday, February 24, 2012

Error in connecting to SQL Server Express

Hello,

I'm a newbie who is trying to create a connection to the server in my website application and I keep getting the following error: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server). I enabled TCP/IP and Pipe in the configuration and restarted the engine and server but I still get this error and it's stressing me out. Below is the code for the app. Any help would be greatly appreciated. Thanks

Imports

System.Data

Imports

System.Data.SqlClient

Partial

Class _DefaultInherits System.Web.UI.PageProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load' Create the database ConnectionDim connAsNew System.Data.SqlClient.SqlConnection( _"Server=Main;" + _"Integrated Security=True;" + _"Database=ShoppingCartData;" + _"AttachDBFilename=" + _"C:\DOCUMENTS AND SETTINGS\TONYA\MY DOCUMENTS\VISUAL STUDIO 2005\WEBSITES\ADO\APP_DATA\ADO.MDF")

' Create the Data Adapter with the required SELECT statementDim AdaptAsNew System.Data.SqlClient.SqlDataAdapter("SELECT * FROM Products", conn)' Create the dataset with the required informationDim dsADOAsNew System.Data.DataSet()

Adapt.Fill(dsADO)

Dim DRAs DataRow()' Define the TableDim ProdTblAsNew Table()' Create content for the Web PageForEach DRIn dsADO.Tables(0).Rows'Create a new table row.Dim ProdRowAsNew TableRow()'Create a cell within the row.Dim ProdCellAsNew TableCell()'Define content for the CellDim ThisLinkAsNew HyperLink()

ThisLink.NavigateUrl =

"Products.aspx?ProdID=" + _

DR(

"ProductID").ToString()

ThisLink.Text = DR(

"ProductNme").ToString()

ThisLink.ID = DR(

"ProductID").ToString()'Add the content to the cell and the cell to the row. Place the row in the table

ProdCell.Controls.Add(ThisLink)

ProdRow.Controls.Add(ProdCell)

ProdTbl.Controls.Add(ProdRow)

Next'Add the table to the Place holder.

phProducts.Controls.Add(ProdTbl)

EndSub

End

Class

That SQL Server Express instance must be local to connect to it. If it is locally installed just try changingServer=(local) to your connection.

Thanks

|||

Thank you for your reply. I have changed this as you've instructed, but it is still not working.

Thanks

|||

Hi,

Is this SQL Server on your local machine? If not, can you connect from the local one?

Also, you can test with a .UDL file. Here are the steps to create one.

1. Create a .txt file on desktop and rename it to .UDL.
2. Double click on the file and a Data Link Properties dialog box will appear.
3. You can test with this dialog box to see if it works fine on both remote and local machine.

No comments:

Post a Comment