Posted on 2014-12-08
Last Modified: 2014-12-18
Having trouble figuring out how to get this binded to a datatable and run a foreach query on it.

My goal is to have an initial query ran that returns a list of id numbers from sql. Then it puts it to a datatable where i can run a foreach row value in the datatable and run a sql query and add them to a dataset that is then binded to a gridview in

Here is what I have so far.

        SqlConnection sqlconnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["outlook"].ConnectionString);
        SqlCommand cmd5 = sqlconnection1.CreateCommand();
        cmd5.CommandText = "SELECT xrefid from outlookreport.dbo.assignloans where userid=@userid" ;

        cmd5.Parameters.AddWithValue("@userid", "");
        SqlDataAdapter da = new SqlDataAdapter(cmd5);
        DataTable table;

Open in new window

Question by:desiredforsome
LVL 10

Expert Comment

by:Walter Padrón
ID: 40487567
From my experience  a table JOIN will do the job.
LVL 20

Accepted Solution

Daniel Van Der Werken earned 500 total points
ID: 40487814
This is actually very simple. At first, I had a difficult time understanding that you don't need to implement the "foreach" yourself with a GridView. It does it for you. Here is a simple page I put together that demonstrates what you need to do. Note that you need to set the

Open in new window

and then call

Open in new window

to get what you want done.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyGridView.aspx.cs" Inherits="WebApplication1.MyGridView" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>

Open in new window

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
    public partial class MyGridView : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            GridView1.DataSource = GetSomeDataTable();

        public DataTable GetSomeDataTable()
            DataTable dt = new DataTable();

            DataColumn c1 = new DataColumn("FirstName", typeof(string));
            DataColumn c2 = new DataColumn("LastName", typeof(string));


            DataRow dr1 = dt.NewRow();
            dr1["FirstName"] = "Mary";
            dr1["LastName"] = "Poppins";


            dr1 = dt.NewRow();
            dr1["FirstName"] = "Wyatt";
            dr1["LastName"] = "Earp";


            dr1 = dt.NewRow();
            dr1["FirstName"] = "Count";
            dr1["LastName"] = "Dracula";


            return dt;

Open in new window


Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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