Solved

Simplfy Databind from reader loop to datapull and apply

Posted on 2014-09-23
3
204 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 82

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

631 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