Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to append to a gridview 1

Status
Not open for further replies.

dembyg

Programmer
Oct 20, 2001
58
US
I need a little help enclosed is sample code to fill a gridview.. I have on recordset to get a count of how man time i need to loop through the second record set... which works.. I need help appending data to a gridview when there is mulitple recordsets please advise
Dim sSql As String = "spCount "
sSql = sSql & "'" & Session("sesdtFrom") & "',"
sSql = sSql & "'" & Session("sesdtTo") & "'"

Dim myCommand As New SqlCommand(sSql, conn)
conn.Open()

Dim MyReader As SqlDataReader = command.ExecuteReader()

If MyReader.HasRows Then
Dim i As Integer
For i = 0 To MyReader.FieldCount - 1

Do While MyReader.Read
Dim xSql As String = " exec spGetError "
xSql = xSql & "'" & Session("sesdtFrom") & "',"
xSql = xSql & "'" & Session("sesdtTo") & "',"
xSql = xSql & MyReader("iSiteID")

Dim MyNewCommand As New SqlCommand(xSql, sqlConnection1)

sqlConnection1.Open()
Response.Write(xSql)
Dim MyNewReader As SqlDataReader = MyNewCommand.ExecuteReader()


If MyNewReader.HasRows Then
GridView1.DataSource = MyNewReader
GridView1.DataBind()
End If
MyNewReader.Close()
sqlConnection1.Close()

Loop

Next
 
It's not a good practice to bind datareaders directly to GUI controls. instead you should load a dataset with results from the datareader. close/dispose all the database objects and then bind the dataset to the GUI control.

You cannot append rows to the gridview, you can only bind enumerated objects to it. therefor you need load the dataset with results from both readers into tables. then merge the data into a third table. then bind this table to the grid.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you jmeckley for your response.. Wonder if you could include sample code that would bind the enumerated object... I dont think i understand how to.. thanks
 
Code:
protected void pageload()
{
   gridview.DataSource = new Service().GetData();
   gridview.DataBind();
}
Service could be implemented a number of ways.
in it's simplest form.
Code:
public class Service
{
   public IEnumerable GetData()
   {
       yield return 1;
       yield return 2;
       yield return 3;
   }
}
for your scenario I would do something like this
Code:
public class Service
{
   public IEnumerable GetData()
   {
       DataSet data = new DataSet();
       Load(data);
       Transform(data);
       return data.Tables[x];
   }

   private void Load(DataSet data)
   {
       using(IDbConnection connection = new SqlConnection())
       {
          connection.Open();
          using(IDbCommand command = connection.CreateCommand())
          {
              command.CommandText = [sql];
              data.Load(command.ExecuteReader());
          }
          using(IDbCommand command = connection.CreateCommand())
          {
              command.CommandText = [sql];
              data.Load(command.ExecuteReader());
          }
       }
   }

   private void Transform(DataSet data)
   {
       //do work here to merge data.Tables[0] and data.Tables[1];
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top