加载页面时不显示GridView

时间:2014-05-09 23:25:22

标签: asp.net gridview

您好我正在为学校做项目。我正在构建一个具有通过SQLDataSource填充的DropDownList的页面。它显示了我的数据库中名为'COURSES'的表中的类的名称,当选择其中一个类时,GridView应该显示来自表'STUDENTS'和'GRADES'的数据。当我做出选择时,GridView根本不显示。我认为问题出在SELECT命令的WHERE子句中。任何帮助将不胜感激!

这是我的aspx文件:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Students</title>
    <link type="text/css" rel="stylesheet" href="termproject.css" />

</head>
<body>
    <form id="form1" runat="server">
    <div id="main">
        Course Name:&nbsp;&nbsp;<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="CourseName" DataValueField="CourseID"></asp:DropDownList>
        <br />
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PayablesConnectionString %>" SelectCommand="SELECT [CourseName], [CourseID] FROM [COURSES] ORDER BY [CourseName]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" DataKeyNames="CourseID" AllowPaging="True" AllowSorting="True">
            <Columns>
                <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
                <asp:BoundField DataField="StudentID" HeaderText="StudentID" SortExpression="StudentID" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:PayablesConnectionString %>" SelectCommand="SELECT COURSES.CourseID, GRADES.Grade, STUDENTS.StudentID, STUDENTS.Title, STUDENTS.FirstName, STUDENTS.LastName FROM COURSES INNER JOIN GRADES ON COURSES.CourseID = GRADES.CourseID INNER JOIN STUDENTS ON GRADES.StudentID = STUDENTS.StudentID WHERE (COURSES.CourseName = @CourseID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="CourseID" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="StudentID" DataSourceID="SqlDataSource3">
            <Fields>
                <asp:BoundField DataField="StudentID" HeaderText="StudentID" InsertVisible="False" ReadOnly="True" SortExpression="StudentID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                <asp:CommandField ShowEditButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:PayablesConnectionString %>" DeleteCommand="DELETE FROM [STUDENTS] WHERE [StudentID] = @original_StudentID AND (([Title] = @original_Title) OR ([Title] IS NULL AND @original_Title IS NULL)) AND (([FirstName] = @original_FirstName) OR ([FirstName] IS NULL AND @original_FirstName IS NULL)) AND (([LastName] = @original_LastName) OR ([LastName] IS NULL AND @original_LastName IS NULL)) AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL))" InsertCommand="INSERT INTO [STUDENTS] ([Title], [FirstName], [LastName], [Address]) VALUES (@Title, @FirstName, @LastName, @Address)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [STUDENTS] WHERE ([StudentID] = @StudentID)" UpdateCommand="UPDATE [STUDENTS] SET [Title] = @Title, [FirstName] = @FirstName, [LastName] = @LastName, [Address] = @Address WHERE [StudentID] = @original_StudentID AND (([Title] = @original_Title) OR ([Title] IS NULL AND @original_Title IS NULL)) AND (([FirstName] = @original_FirstName) OR ([FirstName] IS NULL AND @original_FirstName IS NULL)) AND (([LastName] = @original_LastName) OR ([LastName] IS NULL AND @original_LastName IS NULL)) AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL))">
            <DeleteParameters>
                <asp:Parameter Name="original_StudentID" Type="Int32" />
                <asp:Parameter Name="original_Title" Type="String" />
                <asp:Parameter Name="original_FirstName" Type="String" />
                <asp:Parameter Name="original_LastName" Type="String" />
                <asp:Parameter Name="original_Address" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="StudentID" PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
                <asp:Parameter Name="original_StudentID" Type="Int32" />
                <asp:Parameter Name="original_Title" Type="String" />
                <asp:Parameter Name="original_FirstName" Type="String" />
                <asp:Parameter Name="original_LastName" Type="String" />
                <asp:Parameter Name="original_Address" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        <br />
    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

这是你的sql:

SELECT Courses.Courseid, 
       Grades.Grade, 
       Students.Studentid, 
       Students.Title, 
       Students.Firstname, 
       Students.Lastname 
FROM   Courses 
       INNER JOIN Grades 
               ON Courses.Courseid = Grades.Courseid 
       INNER JOIN Students 
               ON Grades.Studentid = Students.Studentid 
WHERE  ( Courses.Coursename = @CourseID ) 

不是Courses.Courseid而不是Courses.Coursename吗?所以:

WHERE  ( Courses.Courseid = @CourseID ) 

(我避免声明性数据源控件的原因之一)