无法使用GridView获取所有数据

时间:2015-09-01 17:33:31

标签: c# asp.net gridview

我的代码有问题 这是aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdministratorPage.aspx.cs" Inherits="SoftwareAnalysisAndDesign.SAD.AdministratorPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Admin Page</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="Bootstrap/css/bootstrap-theme.css" />
    <link rel="stylesheet" href="Bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="webcss/AdministratorPageStyle.css" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="headerwrapper">
            <div id="headercontent" class="jumbotron">
                <img id="img1" src="usjrlogo.png" />
                <h1 id="title">Online AppSess System</h1>
            </div>
        </div>
        <br />
        <div class="container">
            <div class="row" id="buttons">
                <div class="col-md-8">
                    <button type="button" runat="server" onserverclick="Button1_Click" id="ListofStudent" class="btn btn-primary">View Student</button>
                </div>
            </div>
            <div class="row">
                <asp:GridView runat="Server" id="data"/>
            </div>
        </div>
    </form>
</body>
</html>

背后的aspx代码:

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

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class AdministratorPage : System.Web.UI.Page
    {
        public string query1 = null;
        private MSConnector connector1 = new MSConnector();
        private DataSet ds;
        private DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Connection String
            connector1.ConnectionString = "Data Source=keith;Initial Catalog=Student;Integrated Security=True";

            //Query
            query1 = "select * from student'";

            //Execute query
            ds = connector1.ExecuteQuery(query1);
            dt = ds.Tables[0];

            //Bind all the data
            if (dt.Rows.Count > 9)
            {
                data.DataSource = dt;
                data.DataBind();
            }
            else
            {
                Response.Write("<script>alert('No Data Found.')</script>");
            }
        }
    }
}

这是我的数据库连接器,我将其添加为我的项目的参考:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSSQLConnector
{
    public class MSConnector
    {
        public String ConnectionString { get; set; }

        public DataSet ExecuteQuery(String sqlStatement) 
        {
            try
            {

                DataSet results = new DataSet();

                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    if (conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    using(SqlDataAdapter da = new SqlDataAdapter(sqlStatement, conn))
                    {
                        da.Fill(results);
                    }

                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }

                return results;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

当我试图从我的学生那里得到所有数据时,这个错误就出现了:

(MSSQLConnector.dll中发生了'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理 附加信息:字符串''后面的未闭合引号。)

我不知道是什么问题,但是当我使用断点逐步检查我的程序时,

它跳过了DataTable实现

dt = ds.Tables[0];

请帮忙。

2 个答案:

答案 0 :(得分:3)

您的查询有一个额外的刻度线,例如

dispatchTouchEvent()

答案 1 :(得分:1)

query = "select * from student'"; //error

query = "select * from student"; //correct