Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simplfy Databind from reader loop to datapull and apply

Posted on 2014-09-23
3
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

704 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