"Using" or "With"


Hi all
Please can someone enlighten to me as regards the difference with the "Using" and "With" statement when accessing data - which is better, what are the limitations and/or any pointers. Many thanks.
Regards
Dave

David Winchester

Please mark as answer if this is the solution.
0
Dave_Winchester
7/31/2008 10:18:03 AM
📁 asp.net.object-datasource
📃 16182 articles.
⭐ 0 followers.

💬 8 Replies
👁️‍🗨️ 4571 Views



 using gives you the ability to use the connection and it closes the connection directlly after you finish using it. and there is no need to try- cach - finaly. there is no limitation on using USING keyword

Muhanad YOUNIS
MCSD.NET
My Blog || My Photos || LinkedIn

0
mohi88
7/31/2008 10:45:41 AM

I have a dataobject the returns a sqldatareader using the "Using cn As New SqlConnection..." but I get and unhandled exception error --> Invalid Attempt to call FieldCount when reader is closed.
But this works when using the "With" statement - Any ideas why? Thanks again.

David Winchester

Please mark as answer if this is the solution.
0
Dave_Winchester
7/31/2008 11:14:56 AM

 hi, can you post your code here,
thanks

Bryian Tan
MCP, MCAD
0
BryianTan
7/31/2008 11:21:57 AM

are you using cn.close()???
when using  USING statement you just cn.open() an dleave the the USING to take care of the rest.
but if you post your code you can take more help

Muhanad YOUNIS
MCSD.NET
My Blog || My Photos || LinkedIn

0
mohi88
7/31/2008 11:38:05 AM

Here you go. 
 
==================================================================Using cn As New SqlConnection(AppConfiguration.ConnectionString)Dim cmd As New SqlCommand("proc_Folder_GetFoldersForUser", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userName
cn.Open()
Return cmd.ExecuteReader() End Using
==================================================================
Next Example:
================================================================== Dim conn As New SqlConnection(AppConfiguration.ConnectionString)
Dim cmd As New SqlCommand("proc_Folder_GetFoldersForUser", conn)With cmd
.CommandType = CommandType.StoredProcedure
.Connection.Open()
.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userNameReturn cmd.ExecuteReader()
.Dispose()
.Connection.Close()
End With

David Winchester

Please mark as answer if this is the solution.
0
Dave_Winchester
7/31/2008 11:56:41 AM

i'm not VB coder; but on example one there is no problem as i can see. on example 2 you are disposing before closing the connection.
first close the connection after that dispose. and you may have a look at this article too
http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx

Muhanad YOUNIS
MCSD.NET
My Blog || My Photos || LinkedIn

0
mohi88
7/31/2008 12:04:19 PM

 Just can't seem to get the 1st one to work - Keep getting that error. Think I will just use the 2nd option with the .Dispose Method (as this closes the connection at the same time according MSDN) Pity I prefer the code from example 1.
I'm gonna leave the topic open for another hour or two and see if anyone has anyother advice for me. 
 
Thanks for all the help mohi88
Dave 

David Winchester

Please mark as answer if this is the solution.
0
Dave_Winchester
7/31/2008 12:53:53 PM

 try to use the first line like this
Using Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("cn_name_inwebconfig").ConnectionString)

            
and  after that open the connetion and but what else code after open including Dim cmd as new sqlCommand(.....)

            
 and you may check here to in VB

            
<a rel="nofollow" href="http://www.pluralsight.com/community/blogs/fritz/archive/2005/04/28/7834.aspx" target="_blank">http://www.pluralsight.com/community/blogs/fritz/archive/2005/04/28/7834.aspx </a>

            
that might work!!

            
hope this helps 

 

Muhanad YOUNIS
MCSD.NET
My Blog || My Photos || LinkedIn

0
mohi88
7/31/2008 1:05:26 PM
Reply: