Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6006239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:30:38+00:00 2026-05-23T01:30:38+00:00

I have some sample webservice like below, <WebMethod()> _ Public Function ExecuteCMD() As Boolean

  • 0

I have some sample webservice like below,

<WebMethod()> _
    Public Function ExecuteCMD() As Boolean

        Dim cnn As New Data.SqlClient.SqlConnection        

        Try
        cnn.ConnectionString = "ConnectionString Here"
        cnn.Open()

        Dim cmd As New Data.SqlClient.SqlCommand("CommandText Here", cn)
        cmd.ExecuteNonQuery()

        Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

Normally,we always close the connection after using it,
but there has never call close method.
Some of my friends said that
webservice is stateless method and it doesn’t matter
whether we have
close it or not.Is it true?I also know that cnn object life time is in
only that method and visualstudio will dispose it end of that method.
I really want to know object lifetime,how they allocated on memory and
when they are disposed after using.

Best Regards,
Chong

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:30:39+00:00Added an answer on May 23, 2026 at 1:30 am

    It does matter if you close the connection or not. SQL Server is quite resilient against lingering unclosed connections, but if you for example used Access you would quickly run out of available connections and get an error message when you tried to connect.

    You have a connection object and a command object which both are disposable, so you should dispose them. Letting them go out of scope is not enough, the .NET memory management doesn’t work that way. The IDisposable interface is intended for objects with unmanaged resources that need to be cleared up.

    Most disposable objects has a finaliser as fallback if you fail to dispose them, so they will be cleaned up eventually, but you want to aviod that. You want to dispose the objects as soon as possible, otherwise they will remain in memory until the garbage collector comes around to clean them up.

    A Using block around the code where a disposable object is used is a good way to make sure that it’s disposed properly. It uses a Try...Finally block to ensure that the object is always disposed even if an error occurs.

    <WebMethod()> _
    Public Function ExecuteCMD() As Boolean
    
      Using connection As New Data.SqlClient.SqlConnection        
    
        Try
          connection.ConnectionString = "ConnectionString Here"
          connection.Open()
    
          Using command As New Data.SqlClient.SqlCommand("CommandText Here", connection)
            command.ExecuteNonQuery()
          End Using
    
          Return True
        Catch ex As Exception
            Return False
        End Try
    
      End Using
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple web service like this: @WebService public class MyWebService { @WebMethod
I have an .asmx webservice that returns some XML. A sample bit of XML
I get some data from a WebService, which looks like this Building Address ->
I have some code I've written in PHP for consuming our simple webservice, which
Does anyone have some sample code showing how to POST to a URL using
I have a simple form with some plain html input like bellow using ASP.NET
I ve created a sample REST web service which writes some data in a
I'm using Grails 1.3.7. I have some code that uses the built-in base64Encode function
I have the following json output when i call a sample webservice <string>[{Name:Ajay Singh,Company:Birlasoft
Does anyone have some sample code demonstrating how to make a file browser view?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.