473,289 Members | 1,810 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,289 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 2428
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.