Solved

Simplfy Databind from reader loop to datapull and apply

Posted on 2014-09-23
3
181 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 78

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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