Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to make the connection globel

How to make the connection globel

Scheduled Pinned Locked Moved ASP.NET
tutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Dear Friends, I have a small web application with the below code for the pageload event i would like optimise this in such a way that the code behind page_load event should be minimum i tried to use class but i can not access the variables defined in class Please take a look on my code and give me some nice idea to optimise it. Dim cn As New SqlClient.SqlConnection Dim Cm As New SqlClient.SqlCommand Dim CmTel As New SqlClient.SqlCommand Dim CmPP As New SqlClient.SqlCommand Dim CmOD As New SqlClient.SqlCommand Dim Dr As SqlClient.SqlDataReader Dim DrTel As SqlClient.SqlDataReader Dim DrPP As SqlClient.SqlDataReader Dim DrOD As SqlClient.SqlDataReader Dim SaleManNo As String = Session("SaleManNo") Dim TotalCus As Long Dim TotalBytel As Long Dim TotalByPP As Long Dim TotalByOD As Long Try cn.ConnectionString = ("Initial Catalog=SalesMgt;data source=Murtuza;uid=sa;pwd=sumayya;") cn.Open() Cm.Connection = cn Cm.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleManNo & "'" Dr = Cm.ExecuteReader Dr.Read() If Dr.HasRows Then TotalCus = Dr(0) TxtTotalCus.Text = CType(TotalCus, String) End If Dr.Close() CmTel.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleManNo & "' and WayOfBooking like '%Telephone%'" CmTel.Connection = cn DrTel = CmTel.ExecuteReader DrTel.Read() If DrTel.HasRows Then TotalBytel = DrTel(0) TxtTotTel.Text = CType(TotalBytel, String) End If DrTel.Close() CmPP.Connection = cn CmPP.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleManNo & "' and WayOfBooking like '%Presentation%'" DrPP = CmPP.ExecuteReader DrPP.Read() If DrPP.HasRows Then TotalByPP = DrPP(0) TxtTotalPP.Text = CType(TotalByPP, String) End If DrPP.Close() CmOD.Connection = cn CmOD.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleMa

    S 1 Reply Last reply
    0
    • L Lost User

      Dear Friends, I have a small web application with the below code for the pageload event i would like optimise this in such a way that the code behind page_load event should be minimum i tried to use class but i can not access the variables defined in class Please take a look on my code and give me some nice idea to optimise it. Dim cn As New SqlClient.SqlConnection Dim Cm As New SqlClient.SqlCommand Dim CmTel As New SqlClient.SqlCommand Dim CmPP As New SqlClient.SqlCommand Dim CmOD As New SqlClient.SqlCommand Dim Dr As SqlClient.SqlDataReader Dim DrTel As SqlClient.SqlDataReader Dim DrPP As SqlClient.SqlDataReader Dim DrOD As SqlClient.SqlDataReader Dim SaleManNo As String = Session("SaleManNo") Dim TotalCus As Long Dim TotalBytel As Long Dim TotalByPP As Long Dim TotalByOD As Long Try cn.ConnectionString = ("Initial Catalog=SalesMgt;data source=Murtuza;uid=sa;pwd=sumayya;") cn.Open() Cm.Connection = cn Cm.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleManNo & "'" Dr = Cm.ExecuteReader Dr.Read() If Dr.HasRows Then TotalCus = Dr(0) TxtTotalCus.Text = CType(TotalCus, String) End If Dr.Close() CmTel.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleManNo & "' and WayOfBooking like '%Telephone%'" CmTel.Connection = cn DrTel = CmTel.ExecuteReader DrTel.Read() If DrTel.HasRows Then TotalBytel = DrTel(0) TxtTotTel.Text = CType(TotalBytel, String) End If DrTel.Close() CmPP.Connection = cn CmPP.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleManNo & "' and WayOfBooking like '%Presentation%'" DrPP = CmPP.ExecuteReader DrPP.Read() If DrPP.HasRows Then TotalByPP = DrPP(0) TxtTotalPP.Text = CType(TotalByPP, String) End If DrPP.Close() CmOD.Connection = cn CmOD.CommandText = "Select Count(cuscode) From CusDetail where SalesManNo='" _ & SaleMa

      S Offline
      S Offline
      StylezHouse
      wrote on last edited by
      #2

      This is a little cleaner...

      Public Sub Page_Load()
      TxtTotalCus.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "'")
      TxtTotTel.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "' and WayOfBooking like '%Telephone%'")
      TxtTotalPP.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "' and WayOfBooking like '%Presentation%'")
      TxtTotOutDoor.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "' and WayOfBooking like '%Out door%'")
      End Sub

      Private Function GetValue(ByVal Query) As String
          Dim cn As New SqlClient.SqlConnection("Initial Catalog=SalesMgt;data source=Murtuza;uid=sa;pwd=sumayya;")
          Dim Cm As New SqlClient.SqlCommand(Query, cn)
          Dim strReturn As String
          Try
              cn.Open()
              strReturn = Cm.ExecuteScalar
              If strReturn Is Nothing Then
                  strReturn = String.Empty
              End If
          Catch ex As Exception
              'Handle Error
          Finally
              Cm.Dispose()
              cn.Dispose()
          End Try
          Return strReturn
      End Function
      
      L 1 Reply Last reply
      0
      • S StylezHouse

        This is a little cleaner...

        Public Sub Page_Load()
        TxtTotalCus.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "'")
        TxtTotTel.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "' and WayOfBooking like '%Telephone%'")
        TxtTotalPP.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "' and WayOfBooking like '%Presentation%'")
        TxtTotOutDoor.Text = GetValue("Select Count(cuscode) From CusDetail where SalesManNo='" & SaleManNo & "' and WayOfBooking like '%Out door%'")
        End Sub

        Private Function GetValue(ByVal Query) As String
            Dim cn As New SqlClient.SqlConnection("Initial Catalog=SalesMgt;data source=Murtuza;uid=sa;pwd=sumayya;")
            Dim Cm As New SqlClient.SqlCommand(Query, cn)
            Dim strReturn As String
            Try
                cn.Open()
                strReturn = Cm.ExecuteScalar
                If strReturn Is Nothing Then
                    strReturn = String.Empty
                End If
            Catch ex As Exception
                'Handle Error
            Finally
                Cm.Dispose()
                cn.Dispose()
            End Try
            Return strReturn
        End Function
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Dear StylezHouse Thanks a lot for improving my code Thanks Murtuza.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups