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 14 4580
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.
Connections have the "state" property, which you can check.
-tom
martin1 ha scritto:
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
Thank you. Tom
it works
"to**************@uniroma1.it" wrote:
Connections have the "state" property, which you can check.
-tom
martin1 ha scritto:
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
Martin,
In this construction you don't need to open and close the connection. The
dataadapter does that intrensic.
Cor
"martin1" <ma*****@discussions.microsoft.comschreef in bericht
news:0F**********************************@microsof t.com...
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
Hello martin1,
Three points:
1. The user doesn't give a flying rats ass whether or not your connection
is already open or not. So there's no point in telling them.
2. The connection.State property will tell you about the connections current
state (imagine that). So in accordance with point #1.. check the state and
if it's doing something naughty.. then correct it and move on. You should
only bother the user if the world is coming to an end.
3. You should always call connection.Open and .Close even if the DataAdapter
does this work for you. It mokes the code more readable and you are less
likely to forget when you use other methods for reading the data which do
not perform this functionality for you.
-Boo
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
GhostInAk
>
3. You should always call connection.Open and .Close even if the
DataAdapter does this work for you. It mokes the code more readable and
you are less likely to forget when you use other methods for reading the
data which do not perform this functionality for you.
I assume that this is a very personal opinion from you. I find this a
beginners approach, that should be avoided as soon as possible.
Cor
Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't automatically
open the connection.. you al will be cryin and I'll be sittin in Maui sippin
Rum n Coke.
-Boo
GhostInAk
>3. You should always call connection.Open and .Close even if the DataAdapter does this work for you. It mokes the code more readable and you are less likely to forget when you use other methods for reading the data which do not perform this functionality for you.
I assume that this is a very personal opinion from you. I find this a
beginners approach, that should be avoided as soon as possible.
Cor
Hi GhostInAK
but... if we wanted to foresee all the things that ms might
change in the future, our programs would definitely grow without
limits and eventually get stack :)
I do use in several occasion a boolean function a call
"OpenConnectionIfNecessary" (opening an open connection
would rise error). But I apply it to cases where it is expected
the connection to be open (not here for instance).
Anyway, the idea of Rum n Coke at Maui it's not bad :)
-t
GhostInAK ha scritto:
Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't automatically
open the connection.. you al will be cryin and I'll be sittin in Maui sippin
Rum n Coke.
-Boo
GhostInAk
3. You should always call connection.Open and .Close even if the
DataAdapter does this work for you. It mokes the code more readable
and you are less likely to forget when you use other methods for
reading the data which do not perform this functionality for you.
I assume that this is a very personal opinion from you. I find this a
beginners approach, that should be avoided as soon as possible.
Cor
GhostInAk,
See the answer from Thomasso, Microsoft will never create these breaking
changes if they know this, and this is an obvious one.
However, but let us take the not imaginable situation as well, that
Microsoft would stop checking in the dataadapter if the connection is
already open. Who is than sittin in Maui sipping Rum n Coke. Definitely not
you.
:-)
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht
news:c7**************************@news.microsoft.c om...
Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't
automatically open the connection.. you al will be cryin and I'll be
sittin in Maui sippin Rum n Coke.
-Boo
>GhostInAk
>>3. You should always call connection.Open and .Close even if the DataAdapter does this work for you. It mokes the code more readable and you are less likely to forget when you use other methods for reading the data which do not perform this functionality for you.
I assume that this is a very personal opinion from you. I find this a beginners approach, that should be avoided as soon as possible.
Cor
Hello Cor Ligthert [MVP],
You are most likely correct in that MS will not change the behaviour of the
DataAdapter.Fill method. However.. Seeing code that does something with
a connection whose state you are not in direct control of at design time
leaves one to guess at state. Whereas if instead you implicitly set the
state (both open and close) before doing something with the connection, the
guessing (at least as far as connection state is concerned) is eliminated.
It's just good technique to make sure you are in complete control of your
environment.
-Boo
GhostInAk,
See the answer from Thomasso, Microsoft will never create these
breaking changes if they know this, and this is an obvious one.
However, but let us take the not imaginable situation as well, that
Microsoft would stop checking in the dataadapter if the connection is
already open. Who is than sittin in Maui sipping Rum n Coke.
Definitely not you.
:-)
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht
news:c7**************************@news.microsoft.c om...
>Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't automatically open the connection.. you al will be cryin and I'll be sittin in Maui sippin Rum n Coke.
-Boo
>>GhostInAk
3. You should always call connection.Open and .Close even if the DataAdapter does this work for you. It mokes the code more readable and you are less likely to forget when you use other methods for reading the data which do not perform this functionality for you.
I assume that this is a very personal opinion from you. I find this a beginners approach, that should be avoided as soon as possible.
Cor
Boo,
In my opinion can there be in a good written program be not more guessing
than that
dim C as integer = 1 + 1' C will be 2.
It is not something as this
dim c as string = 1 + 1 'while some users thinks that this will give 11
while it will give used in this way still forever be 2.
The dataadapter has a very fine mechanisme that opens and closes if that is
not done implicitly.
But just my opinion.
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht
news:be*************************@news.microsoft.co m...
Hello Cor Ligthert [MVP],
You are most likely correct in that MS will not change the behaviour of
the DataAdapter.Fill method. However.. Seeing code that does something
with a connection whose state you are not in direct control of at design
time leaves one to guess at state. Whereas if instead you implicitly set
the state (both open and close) before doing something with the
connection, the guessing (at least as far as connection state is
concerned) is eliminated. It's just good technique to make sure you are in
complete control of your environment.
-Boo
>GhostInAk,
See the answer from Thomasso, Microsoft will never create these breaking changes if they know this, and this is an obvious one.
However, but let us take the not imaginable situation as well, that Microsoft would stop checking in the dataadapter if the connection is already open. Who is than sittin in Maui sipping Rum n Coke. Definitely not you.
:-)
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht news:c7**************************@news.microsoft. com...
>>Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't automatically open the connection.. you al will be cryin and I'll be sittin in Maui sippin Rum n Coke.
-Boo
GhostInAk
3. You should always call connection.Open and .Close even if the DataAdapter does this work for you. It mokes the code more readable and you are less likely to forget when you use other methods for reading the data which do not perform this functionality for you. > I assume that this is a very personal opinion from you. I find this a beginners approach, that should be avoided as soon as possible.
Cor
Cor and Boo ... ,
Thank you, this is very good discussion.
I tried to check connection property, if it is not 1 ( not sure) which means
connection failed, then close application. I use try-catch and put
connection.open() in the try block and if conenction failed and close app
using me.close in catch block, but the app window did Not colse and still
point to someline in debug mode. any more advice?
"Cor Ligthert [MVP]" wrote:
Boo,
In my opinion can there be in a good written program be not more guessing
than that
dim C as integer = 1 + 1' C will be 2.
It is not something as this
dim c as string = 1 + 1 'while some users thinks that this will give 11
while it will give used in this way still forever be 2.
The dataadapter has a very fine mechanisme that opens and closes if that is
not done implicitly.
But just my opinion.
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht
news:be*************************@news.microsoft.co m...
Hello Cor Ligthert [MVP],
You are most likely correct in that MS will not change the behaviour of
the DataAdapter.Fill method. However.. Seeing code that does something
with a connection whose state you are not in direct control of at design
time leaves one to guess at state. Whereas if instead you implicitly set
the state (both open and close) before doing something with the
connection, the guessing (at least as far as connection state is
concerned) is eliminated. It's just good technique to make sure you are in
complete control of your environment.
-Boo
GhostInAk,
See the answer from Thomasso, Microsoft will never create these
breaking changes if they know this, and this is an obvious one.
However, but let us take the not imaginable situation as well, that
Microsoft would stop checking in the dataadapter if the connection is
already open. Who is than sittin in Maui sipping Rum n Coke.
Definitely not you.
:-)
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht
news:c7**************************@news.microsoft.c om...
Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't automatically open the connection.. you al will be cryin and I'll be sittin in Maui sippin Rum n Coke.
-Boo
GhostInAk
3. You should always call connection.Open and .Close even if the DataAdapter does this work for you. It mokes the code more readable and you are less likely to forget when you use other methods for reading the data which do not perform this functionality for you.
I assume that this is a very personal opinion from you. I find this a beginners approach, that should be avoided as soon as possible.
Cor
Martin,
In my idea should your code so be written that you don't have to check if a
connection is open.
Very thight opening and closing the connection will help for that (as long
as it is not Jet).
You can even do
Using myconnection
do whatever you need with that
End using
(Be aware that this dispose the connection what is in some framework 2.0
situation not always wanted.)
Or you can do
Try
Conn.open
Try
do what you want
Catch execptions
'what is an handling error
end Try
Catch execptions
'what is an connection error
Finally
Conn.close
End try
I hope this helps,
Cor
"martin1" <ma*****@discussions.microsoft.comschreef in bericht
news:93**********************************@microsof t.com...
Cor and Boo ... ,
Thank you, this is very good discussion.
I tried to check connection property, if it is not 1 ( not sure) which
means
connection failed, then close application. I use try-catch and put
connection.open() in the try block and if conenction failed and close app
using me.close in catch block, but the app window did Not colse and still
point to someline in debug mode. any more advice?
"Cor Ligthert [MVP]" wrote:
>Boo,
In my opinion can there be in a good written program be not more guessing than that
dim C as integer = 1 + 1' C will be 2.
It is not something as this
dim c as string = 1 + 1 'while some users thinks that this will give 11 while it will give used in this way still forever be 2.
The dataadapter has a very fine mechanisme that opens and closes if that is not done implicitly.
But just my opinion.
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht news:be*************************@news.microsoft.c om...
Hello Cor Ligthert [MVP],
You are most likely correct in that MS will not change the behaviour of
the DataAdapter.Fill method. However.. Seeing code that does something
with a connection whose state you are not in direct control of at
design
time leaves one to guess at state. Whereas if instead you implicitly
set
the state (both open and close) before doing something with the
connection, the guessing (at least as far as connection state is
concerned) is eliminated. It's just good technique to make sure you are
in
complete control of your environment.
-Boo
GhostInAk,
See the answer from Thomasso, Microsoft will never create these breaking changes if they know this, and this is an obvious one.
However, but let us take the not imaginable situation as well, that Microsoft would stop checking in the dataadapter if the connection is already open. Who is than sittin in Maui sipping Rum n Coke. Definitely not you.
:-)
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht news:c7**************************@news.microsoft. com...
Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't automatically open the connection.. you al will be cryin and I'll be sittin in Maui sippin Rum n Coke.
-Boo
GhostInAk
3. You should always call connection.Open and .Close even if the DataAdapter does this work for you. It mokes the code more readable and you are less likely to forget when you use other methods for reading the data which do not perform this functionality for you. > I assume that this is a very personal opinion from you. I find this a beginners approach, that should be avoided as soon as possible.
Cor
doh,
Sometimes my fingers are typing something else than I mean.
Read for Framework 2.0 -generated datasources in winform VS 2005
Cor
"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:ea**************@TK2MSFTNGP05.phx.gbl...
Martin,
In my idea should your code so be written that you don't have to check if
a connection is open.
Very thight opening and closing the connection will help for that (as long
as it is not Jet).
You can even do
Using myconnection
do whatever you need with that
End using
(Be aware that this dispose the connection what is in some framework 2.0
situation not always wanted.)
Or you can do
Try
Conn.open
Try
do what you want
Catch execptions
'what is an handling error
end Try
Catch execptions
'what is an connection error
Finally
Conn.close
End try
I hope this helps,
Cor
"martin1" <ma*****@discussions.microsoft.comschreef in bericht
news:93**********************************@microsof t.com...
>Cor and Boo ... ,
Thank you, this is very good discussion.
I tried to check connection property, if it is not 1 ( not sure) which means connection failed, then close application. I use try-catch and put connection.open() in the try block and if conenction failed and close app using me.close in catch block, but the app window did Not colse and still point to someline in debug mode. any more advice?
"Cor Ligthert [MVP]" wrote:
>>Boo,
In my opinion can there be in a good written program be not more guessing than that
dim C as integer = 1 + 1' C will be 2.
It is not something as this
dim c as string = 1 + 1 'while some users thinks that this will give 11 while it will give used in this way still forever be 2.
The dataadapter has a very fine mechanisme that opens and closes if that is not done implicitly.
But just my opinion.
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht news:be*************************@news.microsoft. com... Hello Cor Ligthert [MVP],
You are most likely correct in that MS will not change the behaviour of the DataAdapter.Fill method. However.. Seeing code that does something with a connection whose state you are not in direct control of at design time leaves one to guess at state. Whereas if instead you implicitly set the state (both open and close) before doing something with the connection, the guessing (at least as far as connection state is concerned) is eliminated. It's just good technique to make sure you are in complete control of your environment.
-Boo
GhostInAk,
See the answer from Thomasso, Microsoft will never create these breaking changes if they know this, and this is an obvious one.
However, but let us take the not imaginable situation as well, that Microsoft would stop checking in the dataadapter if the connection is already open. Who is than sittin in Maui sipping Rum n Coke. Definitely not you.
:-)
Cor
"GhostInAK" <gh*******@gmail.comschreef in bericht news:c7**************************@news.microsoft .com...
Hello Cor Ligthert [MVP],
Well, when MS changes the way the .Fill method works so it doesn't automatically open the connection.. you al will be cryin and I'll be sittin in Maui sippin Rum n Coke.
-Boo
GhostInAk > >3. You should always call connection.Open and .Close even if the >DataAdapter does this work for you. It mokes the code more >readable and you are less likely to forget when you use other >methods for reading the data which do not perform this >functionality for you. >> I assume that this is a very personal opinion from you. I find this a beginners approach, that should be avoided as soon as possible. > Cor >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Wolfgang Kaml |
last post by:
Hello All,
I have been working on this for almost a week now and I haven't anything up
my sleeves anymore that I could test in addition or change....
Since I am not sure, if this is a Windows...
|
by: pradeep_TP |
last post by:
Hi all,
I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.
1) If I have opened a connection to a...
|
by: Bryan |
last post by:
Hello,
I'm just starting to develop in asp.net and i have a question about
using a database connection globally in my app. I have set up the
procedures for getting all my connection string info...
|
by: Nick Gilbert |
last post by:
Hi,
I have an asp.net application which runs from a CD-ROM using Cassini. As
such, it is single user only.
The application connects to an Access database when it is loaded, and
keeps the same...
|
by: Lau Lei Cheong |
last post by:
Hello,
Actually I think I should have had asked it long before, but somehow
I haven't.
Here's the scenerio: Say we have a few pages in an ASP.NET project,
each of them needs to connect to...
|
by: Martin B |
last post by:
Hallo!
I'm working with C# .NET 2.0, implementing Client/Server Applications which
are connecting via Network to SQL-Server or Oracle Databases.
To stay independent from the underlaying Database...
|
by: V_S_H_Satish |
last post by:
Dear Friends
I am working as oracle and ms sql dba from last 4 years. My company
recently migrated to DB2 databases.
So i am very much new to db2 database
Can any one pls provide script to...
|
by: Terry Jolly |
last post by:
Web Solution
Goal: Have a global database connection
Why: (There will be 30+ tables, represented by 30+ classes) I only want to
reference the database connection once.
I put the connection...
|
by: fniles |
last post by:
In the Windows application (using VB.NET 2005) I use connection pooling like
the following:
In the main form load I open a connection using a connection string that I
stored in a global variable...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |