Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best Practice 2

Status
Not open for further replies.

dhmfh

Programmer
Nov 28, 2005
69
GB
Hi Guys

I have the following code:

Public Shared Function ListLateAvailability()

Dim DBConnection As SqlConnection = New SqlConnection(GlobalParameters.DBConnection())
Dim ListLateAvailabilityTable As New DataTable
Dim SqlCommand As New SqlCommand("DSP_WLateAvailabilityProperty", DBConnection)

With SqlCommand
.CommandType = CommandType.StoredProcedure
End With

Dim DataAdapter As New SqlDataAdapter(SqlCommand)
DataAdapter.Fill(ListLateAvailabilityTable)

Return (ListLateAvailabilityTable)

'Check If Connection is Open
If (DBConnection.State = ConnectionState.Open) Then
DBConnection.Close()
End If

End Function

The query above return a recordset and outputs information.
What is the best way / practice to return the recordcount of the query that I am executing?

Kind regards
 
Hi,

Public Shared Function ListLateAvailability() As DataTable

Dim DBConnection As SqlConnection = New SqlConnection(GlobalParameters.DBConnection())
Dim ListLateAvailabilityTable As New DataTable
Dim SqlCommand As New SqlCommand("DSP_WLateAvailabilityProperty", DBConnection)

With SqlCommand
.CommandType = CommandType.StoredProcedure
End With

Dim DataAdapter As New SqlDataAdapter(SqlCommand)

DBConnection.Open()


DataAdapter.Fill(ListLateAvailabilityTable)

'Check If Connection is Open
If (DBConnection.State = ConnectionState.Open) Then
DBConnection.Close()
End If


Return (ListLateAvailabilityTable)


End Function

Once you have returned the DataTable you can get the row count by

MyDataTable.Rows.Count.ToString

HTH

j


 
Hi whatsthehampton

I have put the MyDataTable.Rows.Count.ToString
into a variable. How do I now return that value to a web page?

Kind regards
 
You don't return the value to the web page - you return the datatable and then count the rows on your page e.g.

Class:
Code:
Imports Microsoft.VisualBasic
Imports System.Data

Public Class Class1

    Public Function GetRecords() As DataTable
        Dim dt As New DataTable
        Dim dr As DataRow

        dt.Columns.Add("Column1")
        For i As Integer = 0 To 10
            dr = dt.NewRow
            dr("Column1") = i
            dt.Rows.Add(dr)
        Next

        Return dt

    End Function
End Class

Web Page:
Code:
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim newClass As New Class1

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myDataTable = newClass.GetRecords
        Label1.Text = myDataTable.Rows.Count
    End Sub

End Class


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top