473,883 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does SqlDataAdapter. Fill() always close the connection?

M
Hi,

Does SqlDataAdapter always close the connection (assuming connection was
closed before calling Fill()), even if an exception occurs while calling
Fill()?

Example:
try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}

or should I do this instead

try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
finally
{
if (conn.State == ConnectionState .Open)
{
conn.Close();
}
}

Thanks.
Nov 19 '05 #1
6 10999
Yes You should close it.

Nov 19 '05 #2
SqlDataAdapter manages the connection itself, meaning it opens the connection
and it closes the connection. If, OTOH, you tell it the connection and you
open the connection yourself, it detects this and it will not close the connection.
So since you opened it you're responsible for closing it.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi,

Does SqlDataAdapter always close the connection (assuming connection
was closed before calling Fill()), even if an exception occurs while
calling Fill()?

Example:
try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
or should I do this instead

try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
finally
{
if (conn.State == ConnectionState .Open)
{
conn.Close();
}
}
Thanks.


Nov 19 '05 #3
The dataAdapter manages the connection so you don't have to close it. The
Adapter does all the work

--
TDAVISJR
aka - Tampa.NET Koder
"M" <so******@somew here.com> wrote in message
news:Oj******** ******@TK2MSFTN GP14.phx.gbl...
Hi,

Does SqlDataAdapter always close the connection (assuming connection was
closed before calling Fill()), even if an exception occurs while calling
Fill()?

Example:
try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}

or should I do this instead

try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
finally
{
if (conn.State == ConnectionState .Open)
{
conn.Close();
}
}

Thanks.

Nov 19 '05 #4
M
Hi Brock,

In my example, I did not open the connection. I let SqlDataAdapter open the
connection. What I'm not sure though, is what happens if .Fill() generates
an exception. Will the adapter still close the connection or not? Maybe Fill
has its own try/catch/finally block and therefore will close the connection
in its own finally even if an error occurred; but that is what I'm not sure
about.

Thanks.
"Brock Allen" <ba****@NOSPAMd evelop.com> wrote in message
news:37******** **************@ msnews.microsof t.com...
SqlDataAdapter manages the connection itself, meaning it opens the
connection and it closes the connection. If, OTOH, you tell it the
connection and you open the connection yourself, it detects this and it
will not close the connection. So since you opened it you're responsible
for closing it.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi,

Does SqlDataAdapter always close the connection (assuming connection
was closed before calling Fill()), even if an exception occurs while
calling Fill()?

Example:
try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
or should I do this instead

try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
finally
{
if (conn.State == ConnectionState .Open)
{
conn.Close();
}
}
Thanks.


Nov 19 '05 #5
M
Even if Fill causes an exception? Thanks.
"TDAVISJR" <an*******@micr osoft.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
The dataAdapter manages the connection so you don't have to close it. The
Adapter does all the work

--
TDAVISJR
aka - Tampa.NET Koder
"M" <so******@somew here.com> wrote in message
news:Oj******** ******@TK2MSFTN GP14.phx.gbl...
Hi,

Does SqlDataAdapter always close the connection (assuming connection was
closed before calling Fill()), even if an exception occurs while calling
Fill()?

Example:
try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}

or should I do this instead

try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
finally
{
if (conn.State == ConnectionState .Open)
{
conn.Close();
}
}

Thanks.


Nov 19 '05 #6
Here's the code in the DataAdapter. QuietOpen and QuietClose open and close
the connection as long as it wasn't already open when you call Fill:

try
{
try
{
DbDataAdapter.Q uietOpen(connec tion1, out state1);
using (IDataReader reader1 = command.Execute Reader(behavior
| CommandBehavior .SequentialAcce ss))
{
if (data is DataTable)
{
return this.Fill((Data Table) data, reader1);
}
return this.Fill((Data Set) data, srcTable, reader1,
startRecord, maxRecords);
}
}
finally
{
DbDataAdapter.Q uietClose(conne ction1, state1);
}
}
catch
{
throw;
}

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi Brock,

In my example, I did not open the connection. I let SqlDataAdapter
open the connection. What I'm not sure though, is what happens if
.Fill() generates an exception. Will the adapter still close the
connection or not? Maybe Fill has its own try/catch/finally block and
therefore will close the connection in its own finally even if an
error occurred; but that is what I'm not sure about.

Thanks.

"Brock Allen" <ba****@NOSPAMd evelop.com> wrote in message
news:37******** **************@ msnews.microsof t.com...
SqlDataAdapter manages the connection itself, meaning it opens the
connection and it closes the connection. If, OTOH, you tell it the
connection and you open the connection yourself, it detects this and
it will not close the connection. So since you opened it you're
responsible for closing it.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi,

Does SqlDataAdapter always close the connection (assuming connection
was closed before calling Fill()), even if an exception occurs while
calling Fill()?

Example:
try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
or should I do this instead
try
{
myDataAdapter.F ill(myDataTable );
}
catch
{
// display some error message or something
}
finally
{
if (conn.State == ConnectionState .Open)
{
conn.Close();
}
}
Thanks.


Nov 19 '05 #7

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

Similar topics

0
1780
by: Seiche V via DotNetMonster.com | last post by:
hy. i want to fill a data table from a dataset using SqlDataAdapter.fill(myTable), but i get the error: System.NullReferenceException: Object reference not set to an instance of an object. more of the error i get: at System.Data.DataTable.RecordStateChanged(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRowState oldState2, DataViewRowState newState2)
2
1273
by: Dan | last post by:
I've created a web form which fills a DataGrid with a DataSet generated from the SqlDataAdapter.Fill method. The adapter's query takes about 30 seconds to complete when I run it in the SQL Server Query Analyzer, but when I run it from my web page I kept getting a TimeOut exception. So then, as an experiment, I changed the way I created the adapter to the following: SqlConnection conn = new SqlConnection(m_Conn); SqlCommand sqlCommand =...
3
3816
by: alex | last post by:
Hi, I placed a listbox on the left hand side, of an mdi parent, splitter next to it. Docking of the splitter is Left. At the bottom I have a status bar. The list box does not fill the entire space. how come ? There are always 10 pixels or so left at the bottom. Anyone had the same problem ?
13
2364
by: david ullua | last post by:
Hi, In Expand.c of BSD system, I met the following codes, the expression (column & 07) if used to compare variable column and 7, if column<=7, it returns true, else false. It use (column & 07) rather than (column<=7), thus it brings me a question, does bit operation always work more efficiently than math operation? How about plus without carry? and minus without borrow? I have searched through google web and newsgroups, haven't found any...
1
1166
by: Papa.Coen | last post by:
Hi, I have a problem with my website (aspx/c#); I get a 'Server Application Unavailable' when the host (127.0.0.1/localhost) does not have an network connection (tcp cable unplugged, on purpose ) I use: - dotNET 2.0 (!!, Google searches place this behaviour with dotNET 1.0) - IIS 5,1 - WinXP pro (activated & unactivated) - I have the problem in both a VMWare demo environment and my dev desktop
2
16919
by: Adrien Reboisson | last post by:
I'm trying to build a basic DB explorer using C# & Visual Studio 2005. I installed SQL Server 2005 Express, created a blank project, dropped a TreeView, a ListView and a DataGridView : DB objects (Databases, tables, SPs, and so on) are displayed in the tree, table colums definitions in the list and I want to display the selected table content in the DataGridView. So far so good. Currently, I succedded in loading the treeview and the...
2
2030
by: active | last post by:
I have another post asking how to fill in the structure but I now think GetDIBBits is the way, I just can't get it to work! I can make the following run but GetDIBBits does not fill the array to correctly reflect the data from the bitmap. Any helpful suggestions would be appreciated. Dim hInBitmap As IntPtr = InBitmap.GetHbitmap()
2
1960
by: Sophy | last post by:
Hi, I do not know why my IE always close when I open the sites that use Prototype framework? Any helps is always appreciate.
0
1757
by: dprjessie | last post by:
Hello, I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out what is wrong. I have code that reads an excel file and fills a datagridview and it works just fine. It is also supposed to fill a database though and that is not working. The executeNonQuery command is returning a 1 which as I understand, means it has...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7980
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.