GridView无法显示

时间:2014-06-12 20:39:33

标签: c# asp.net gridview ado.net sqldatareader

这非常简单。我有一个GridView,我在代码隐藏中设置数据源,但GridView根本不显示。

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="scorecard.aspx.cs" Inherits="Scorecard" Title="Scorecard" %>

<form runat="server">
    <div style="background-color: maroon; height: 100%;" runat="server">
        <div style="background-color: white;">
            <img src="le log.jpg" />
        </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ViewStateMode="Enabled"></asp:GridView>
    </div>
</form>

代码隐藏:

using System;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class Scorecard : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var username = Request.Params["LOGON_USER"].ToString();
        var track = Request.Params["track"].ToString();

        var userKey = 0;
        var fullName = "";

        if (Request.Params["track"] == null || string.IsNullOrEmpty(username))
        {
            Response.Write("<p style=\"color: red;\">An error occurred. Try again.</p>");
        }
        else
        {
            var config = WebConfigurationManager.OpenWebConfiguration("/");

            if (config.ConnectionStrings.ConnectionStrings.Count > 0)
            {
                System.Configuration.ConnectionStringSettings connString = config.ConnectionStrings.ConnectionStrings["CompanyCollege"];

                try
                {
                    using (SqlConnection conn = new SqlConnection(connString.ConnectionString))
                    {
                        SqlCommand cmd1 = new SqlCommand(string.Format("SELECT UserKey, FirstName + ' ' + LastName AS FullName FROM UserProfile WHERE Username = '{0}'", username), conn);
                        SqlCommand cmd2 = new SqlCommand("Report_AssessmentTrackingByUser", conn);
                        cmd2.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd2.Parameters.Add("@UserKey", System.Data.SqlDbType.Int);
                        cmd2.Parameters["@UserKey"].Value = userKey;

                        conn.Open();

                        using (var reader = cmd1.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                userKey = (int)reader.GetValue(0);
                                fullName = (string)reader.GetValue(1);
                            }
                        }

                        using (var reader = cmd2.ExecuteReader())
                        {
                            DataTable dt = new DataTable();
                            dt.Load(reader);
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                Response.Write("<p style=\"color: red;\">An error occurred. Try again.</p>");
            }
        }
    }
}

有人可以告诉我这里有什么问题吗?

0 个答案:

没有答案