473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

error 40 could not open a connection to sql server when passingselected values

TG
Hi!

Once again I have hit a brick wall here. I have a combobox in which
the user types the server name and then clicks on button 'CONNECT' to
populate the next combobox which contains all the databases in that
server. Then after the user selects a database, I have another button
that he/she click and I want to retrieve file groups from a specific
table. At this point when he/she clicks on that button I get an 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)"

The error happens at the open line (button8_click):
With cn
.ConnectionString = "Data Source=" &
comboServers.SelectedValue & ";Initial Catalog=" &
comboDatabases.SelectedValue & ";Integrated Security=SSPI"
.Open()
End With
Why am I getting this error? I also noticed that when i put the mouse
over the .connectionstring, the datasource and the initial catalog are
empty, which they shouldn't be, because I am passing the first
combobox selectedvalue and the second combobox selectedvalue.

How can I passed the values that the user typed in the first box and
selected on the second one? That'll probably solve the problem. I
tried selecteditem but did not work either and gave me the same error
message.
Thank you very much in advanced!

Tammy
Here is all my code from the beginning up to that button that is not
working as it should.
Imports System
Imports System.IO
Imports System.Collections
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Windows.Forms
Imports Microsoft.SqlServer
Imports Microsoft.SqlServer.Server
Imports Microsoft.SqlServer.Management
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function ShellEx Lib "shell32.dll" Alias
"ShellExecuteA" ( _
ByVal hWnd As Integer, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Integer) As
Integer
'Public Class frmSQLConnection
Private m_objServer As Server
Public Property SMOServer() As Server
Get
Return m_objServer
End Get
Private Set(ByVal value As Server)
m_objServer = value
End Set
End Property

Private m_objDatabase As Database
Public Property SMODatabase() As Database
Get
Return m_objDatabase
End Get
Private Set(ByVal value As Database)
m_objDatabase = value
End Set
End Property

Private Sub Mainform_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load

Dim objServers As DataTable
Dim strServer As String

'---- retrieve a list of SQL Server instances on the network
objServers = SmoApplication.EnumAvailableSqlServers(False)

For Each objRow As DataRow In objServers.Rows

strServer = CStr(objRow("Server"))
If Not TypeOf objRow("Instance") Is DBNull AndAlso
CStr(objRow("Instance")).Length 0 Then

strServer += "\" & CStr(objRow("Instance"))

End If

Me.comboServers.Items.Add(strServer)

Next

Me.comboDatabases.Enabled = False

End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
'Private Sub comboservers_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
comboServers.SelectedIndexChanged

Dim objConn As ServerConnection

'If Me.comboServers.Text.Trim.Length() = 0 Then
objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() 0 Then

objConn.ServerInstance = Me.comboServers.Text.Trim()

End If
Me.SMOServer = New Server(objConn)

'End If
'---- Note: the connection will open when we call our first
method on the Server object
Me.comboDatabases.Items.Clear()
For Each objDB As Database In Me.SMOServer.Databases
Me.comboDatabases.Items.Add(objDB.Name)
Next

Me.comboDatabases.Enabled = True

Me.comboDatabases.SelectedIndex = -1
End Sub
'Private Sub comboDatabases_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Private Sub button8_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button8.Click

comboFilesets.Items.Clear()

Dim cn As New SqlClient.SqlConnection()
Dim cm As New SqlClient.SqlCommand()
Dim dr As SqlClient.SqlDataReader

With cn
.ConnectionString = "Data Source=" &
comboServers.SelectedValue & ";Initial Catalog=" &
comboDatabases.SelectedValue & ";Integrated Security=SSPI"
.Open()
End With
With cm
.CommandText = "usp_DR_Spam_BB_Search_filesets"
.CommandType = CommandType.StoredProcedure
.Connection = cn
.Parameters.AddWithValue("@Matter",
comboDatabases.SelectedItem)
End With

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
comboFilesets.Items.Add(dr.Item(0))
End While
dr.Close()
End Sub

Jun 27 '08 #1
2 2430
TG
I have already checked the error message on google. Thank you very
much!

My question is how to pass the typed server from the first combobox
and the second selected database from the combobox to the
connectionstring!

If I use a different method of connecting to the server and database
and then pass the connectionstring it works fine....!
Thanks!

Tammy
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Matthew Louden | last post by:
The following ASP code yields the following error, but actually the new record is stored in database. The same error happens when the application deletes a record, such as sqlStmt ="delete from...
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
3
by: Steven Scaife | last post by:
Below is my ASP page, I have changed the update to read DRIAL which doesn't exist, shouldnt this throw an error, or if the connection cannot be made shouldnt it throw an error as well thanks in...
3
by: Gtbntgar | last post by:
Please can someone look at my SQL command it is not working. It is giving me a syntax error Act = request.form("Act") NT = request.form("NT") Set Conn =...
1
by: iam247 | last post by:
Hi I am a relative beginner with SQL and ASP. With some help after previous posts I have a page which successfully requests querystrings from another page and deletes a record from an access...
4
by: Troy | last post by:
We recently installed the .Net framework on a windows 2000 server. Shortly after that we experienced intermitant problems running a web based program that accesses an Access 2002 database. The...
0
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops...
4
by: Jack | last post by:
Hi, I am trying to run an example code from a book. However I am getting the following error message: Number: -2147217900 Description: Syntax error or access violation Source: Microsoft OLE...
3
by: Doug Durrett | last post by:
I'm having an issue and wanted to pass it by everyone to see what you think. Here is my code. //Code Start searchs = new...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.