Gridview没有在c#中显示页面加载

时间:2015-12-17 06:36:59

标签: c# asp.net gridview

protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    SqlConnection con = Connection.DBconnection();
                    {
                        SqlCommand com = new SqlCommand("sp_selectlendingstatus", con);
                        com.CommandType = CommandType.StoredProcedure;
                        com.Parameters.AddWithValue("@studentid", txtstudentid.Text.Trim());
                        com.Parameters.AddWithValue("@bookid", txtbookid.Text.Trim());
                        com.Parameters.AddWithValue("@status", status.Text.Trim());
                        SqlDataAdapter sda = new SqlDataAdapter(com);
                        DataSet ds = new DataSet();
                        sda.Fill(ds);
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                    }
                }
            }

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataKeyNames="ID" EnablePersistedSelection="True" BackColor="White" 
        Height="240px" 
        Width="755px">
        <Columns>
        <asp:BoundField DataField="ID" HeaderText="Student_Id" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="Book_id" HeaderText="Book_id" InsertVisible="False" ReadOnly="True" SortExpression="Book_id" />                 
            <asp:BoundField DataField="Book_name" HeaderText="Book_name" SortExpression="Book_name" />
            <asp:BoundField DataField="Author_name" HeaderText="Author_name" SortExpression="Author_name" />
            <asp:BoundField DataField="Publisher_name" HeaderText="Publish_name" SortExpression="Publisher_name" />
            <asp:BoundField DataField="Publish_date" HeaderText="Publish_date" SortExpression="Publish_date" />  
            <asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" /> 
            </Columns>
            </asp:GridView> 

SP:

ALTER PROCEDURE sp_selectlendingstatus

AS
Begin
select * from book_lending left outer join studentlibrary ON studentlibrary.Book_id=book_lending.bookid
End

我刚接触.net ..

在页面加载中,我需要显示gridview。

我尝试了上面的源代码,但是当我运行gridview时没有显示。

它显示错误,sp_selectlendingstatus没有传递参数。

所以如果我在sp_selectlendingstatus程序中添加参数,就像这样,

ALTER PROCEDURE sp_selectlendingstatus
(
@bookid int,
@studentid int,
@status varchar(50)
)
AS
Begin
select * from book_lending left outer join studentlibrary ON studentlibrary.Book_id=book_lending.bookid
End

并在sql server中运行EXEC sp_selectlendingstatus。

显示错误@studentid,@ bookid未提供。

我可以知道我的代码中有什么错误吗?

任何帮助都将受到高度赞赏。

谢谢,

1 个答案:

答案 0 :(得分:0)

根据您的选择查询,您可以编写如下格式。在绑定网格之前,您必须检查所有检索列名称是否存在于gridview中,否则会引发错误。

private void BindStudents()
{
String strConnStr =  ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlConnection con =  new SqlConnection(strConnStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_selectlendingstatus";
cmd.Connection = con;
try
{
    con.Open();
    GridView1.EmptyDataText = "No Records Found";
    GridView1.DataSource = cmd.ExecuteReader() ;
    GridView1.DataBind(); 
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    con.Close();
    con.Dispose();
 }
}