Re: check database connection/open

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




martin1 wrote:
All, I want to check wether db connection/open or not, if not it will post db
connection error to user in message box

The code is like:

Dim objConnection As New SqlConnection _
("Server=server1; database=db1;user
id=user1;password=whatever")
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet

objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
cmdtext = "Select parameter from table1"

objDataAdapter.SelectCommand.CommandText = cmdtext
objDataAdapter.SelectCommand.CommandType = CommandType.Text

objConnection.Open()
objDataAdapter.Fill(objDataSet, "table1")
objConnection.Close()

So if connection problem, it will post error masseage like below

MessageBox.Show("Ths is Database connenction issue", "DataBase Connection
problem")

My question is which code line is used to check if db connection/open or
not, it is objConnection.Open()? if it is, how to check? Thanks

Seems like all you need to do is put the .Fill method call inside of a
Try/Catch block.

.



Relevant Pages

  • Re: check database connection/open
    ... connection error to user in message box ... Dim objConnection As New SqlConnection _ ... Dim objDataAdapter As New SqlDataAdapter ... So if connection problem, it will post error masseage like below ...
    (microsoft.public.dotnet.languages.vb)
  • Re: check database connection/open
    ... The user doesn't give a flying rats ass whether or not your connection is already open or not. ... Dim objDataAdapter As New SqlDataAdapter ... So if connection problem, it will post error masseage like below ... MessageBox.Show("Ths is Database connenction issue", ...
    (microsoft.public.dotnet.languages.vb)
  • Re: check database connection/open
    ... In this construction you don't need to open and close the connection. ... connection error to user in message box ... Dim objDataAdapter As New SqlDataAdapter ... MessageBox.Show("Ths is Database connenction issue", ...
    (microsoft.public.dotnet.languages.vb)
  • Re: problem with connecting to sql server via a wireless application
    ... I have created a SQL Server 2005 database in VS 2005 and I am using windows ... I use Vista and Windows Mobile device centre for the connection between the ... So I am very confused whether this is a connection error or not... ...
    (microsoft.public.sqlserver.clients)
  • ASP / SQLServer connection
    ... dim objConnection, ObjRecorSet, strConnection, strOut, objCmd, objParameters ... Set objConnection = Server.CreateObject ... I'm getting this error on the line that opens the connection. ...
    (microsoft.public.inetserver.asp.general)