.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

An error has occurred while establishing a connection to the server

Posted By: siri.k     Posted Date: January 12, 2012    Points:5   Category :VB.Net
hai all as iam doing vb application .by using sqlserver 2005 after debuging its shwoing me this error can any one plz tell me how to provide connection ????
as i paste coding that i wrote for sample application

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)

coding ..............

Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim dr As SqlClient.SqlDataReader
Dim connectionstring As String = System.Configuration.ConfigurationSettings.AppSettings("DBCon")
Dim cn As New SqlConnection(connectionstring)
Dim dt As New DataTable
Dim ds As New DataSet
Dim adap As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim sqlquery As String = ""
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
If cn.State = ConnectionState.Closed Then cn.Open()
' cn.Open()
sqlquery = "insert into tbl_siri(sname,saddress) values('" & txtname.Text & "','" & txtadd.Text & ")"
cmd = New SqlCommand(sqlquery, cn)
MsgBox("successfully saved...")
End Sub

Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
txtadd.Text = ""
txtname.Text = ""
End Sub
End Class

in app config------------

<?xml version="1.0" encoding="utf-8" ?>
<configSections >
<appSettings >
<add key ="DBCon" value ="Data source=SU86;user id=;password=;database=siri"/>

Author: Deco             
Posted Date: January 17, 2012     Points: 20   

Please enter the username and password also give the instancename of the server if required to avoid the errors.
<add key ="DBCon" value ="Data source=SU86;user id=;password=;database=siri"/>

After giving all the details if the error occurs means you have to enable the remote connection to the server in which sql server is installed


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend