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

x
?
Solved

ASP.NET GRIDVIEW DATASET SQL QUERY

Posted on 2014-12-08
2
Medium Priority
?
504 Views
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 asp.net

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", "test@gmail.com");
        SqlDataAdapter da = new SqlDataAdapter(cmd5);
        sqlconnection1.Open();
        DataTable table;

Open in new window

0
Comment
Question by:desiredforsome
2 Comments
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40487567
From my experience  a table JOIN will do the job.
see http://technet.microsoft.com/en-us/library/aa213233%28v=sql.80%29.aspx
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 2000 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
DataSource

Open in new window

and then call
DataBind()

Open in new window

to get what you want done.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>
</body>
</html>

Open in new window


MyGridView.aspx.cs:
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();
            GridView1.DataBind();
        }

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

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

            dt.Columns.Add(c1);
            dt.Columns.Add(c2);

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

            dt.Rows.Add(dr1);


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

            dt.Rows.Add(dr1);

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

            dt.Rows.Add(dr1);

            return dt;
        }
    }
}

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

926 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