Solved

Simplfy Databind from reader loop to datapull and apply

Posted on 2014-09-23
3
185 Views
Last Modified: 2014-12-22
Currently I am using the following code to populate a Gridview, I want to see if there is a way that I can have a similar setup but have the data pulled as a whole object and applied to the gridview without the looping.  I'm not sure how the reader works, E.G. pulls the entire dataset into memory then loops through those or reads each record from the database, but I believe that it's the latter?  I'm told by various forums and one of my DBA's that apparently the loop read process can cause longer run times, I'm trying to make this process a bit faster so want to see if anyone has any suggestions?  Thanks for your time!!

  Function CreateUCAR() As ICollection
    Dim pf As DbProviderFactory = DbProviderFactories.GetFactory("Teradata.Client.Provider")
    Dim con As DbConnection = pf.CreateConnection()
    Dim command As DbCommand = con.CreateCommand
    Dim stringBuilder As New Teradata.Client.Provider.TdConnectionStringBuilder()
    Dim SqlQry As StringBuilder = New StringBuilder
    Dim SelStmt, ThisEmp, ThisUser, MyMsg As String
    ' Create sample data for the DataList control.
    Dim dt As DataTable = New DataTable()
    Dim dr As DataRow

    ' Define the columns of the table.
    dt.Columns.Add(New DataColumn("DBNam", GetType(String)))
    dt.Columns.Add(New DataColumn("Tbls", GetType(String)))
    dt.Columns.Add(New DataColumn("Rights", GetType(String)))

    stringBuilder.DataSource = Session.Contents("DataSource")
    stringBuilder.UserId = Session.Contents("AdminLogin")
    stringBuilder.Password = Session.Contents("AdminPasswd")
    stringBuilder.PersistSecurityInfo = True
    con.ConnectionString = stringBuilder.ConnectionString

    Try
      SelStmt = "Sel * from MYTABLE"
      command.CommandText = SelStmt
      command.CommandType = CommandType.Text
      command.CommandTimeout = 1000
      Dim reader As DbDataReader = command.ExecuteReader()

      Do While reader.Read()
        If reader.HasRows Then
          dr = dt.NewRow
          dr(0) = reader("databasename")
          dr(1) = reader("tablename")
          dr(2) = reader("accessright")
          dt.Rows.Add(dr)
        End If
      Loop

      reader.Close()
    Catch ex As Exception
      If ex.Message.ToString.Trim = "[.NET Data Provider for Teradata] [100006] Invalid connection string attribute:  is not a valid value for DATASOURCE." Then
        subLoginFailed()
        Exit Function
      Else
        MyMsg = "Error loading UCAR:" & vbCrLf & ex.Source & vbCrLf & vbCrLf & ex.Message
        DisplayErr(MyMsg)
        con.Close()
        Exit Function
      End If
    Finally
      con.Close()
    End Try

    Dim dv As DataView = New DataView(dt)
    Return dv
  End Function

Open in new window

0
Comment
Question by:EDW_Gideon
3 Comments
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 40340854
they are both correct and also incorrect as it is a 'it depends situation'   when you are returning a few thousand records of moderate size of each record then they are correct, but if your query returns a few million records or records of a fair size then they are incorrect.  the data has to be sent to your application and stored in memory ready for usage.

While processing this request your disk io will spike unless the sql has cached the records and also other users will be blocked.  Another contentious issue is do I open the sql connection at the start of the program and close the connection on program exit or do I explicit open and close the connection as required.  a sql open/close takes a finite amount of me and this time can add up..
0
 

Author Comment

by:EDW_Gideon
ID: 40341625
Is there a way to pull the dataset and have it directly applied to the gridview without looping through the records as shown in the example above?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 40342050
Read requests each row from database, one at a time.

For situation like yours, I prefer to use Datatable with an adapter

Dim dbadp As New SqlTableAdapter("Select ...", ConnectionString)
Dim dTable As New DataTable

dbadp.Fill(dTable)
dbadp.Dispose

GridView1.DataSource = dTable
GridView1.DataBind
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now