Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simplfy Databind from reader loop to datapull and apply

Posted on 2014-09-23
3
Medium Priority
?
222 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 84

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

782 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