| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1011 人关注过本帖
标题:恳请大家帮忙:GridView对数据进行排序时出现未将对象引用设置到对象的实例
只看楼主 加入收藏
serven7
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-4-13
收藏
 问题点数:0 回复次数:0 
恳请大家帮忙:GridView对数据进行排序时出现未将对象引用设置到对象的实例
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sconn = "server=.;database=studentsys;trusted_connection=true";
            SqlConnection dd = new SqlConnection(sconn);
            SqlDataAdapter sda = new SqlDataAdapter("select * from tblstudent", dd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }

    private void SetBind()
    {
         
        DataSet ds = new DataSet();//实现排序功能的SetBind
        string sConnectionString = "server=.;database=studentsys;trusted_connection=true";
        using(SqlConnection conn=new SqlConnection (sConnectionString))
        {
            string dt = "select * from tblstudent";
            SqlDataAdapter da = new SqlDataAdapter(dt,conn);
            da.Fill(ds);
        }
        DataView dv = new DataView(ds.Tables[0]);
        if(ViewState["SortExpreeion"]!= null)
            dv.Sort=ViewState["SortExpression"].ToString ()+""+ViewState["SortDirection"].ToString ();
        GridView1.DataSource = dv;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if ( == "Del")
        {
            int iIndex = Convert.ToInt16();
            string sUserID = GridView1.DataKeys[iIndex].Value.ToString();
            string sconn = "server=.;database=studentsys;trusted_connection=true";
            string sSql = "delete from tblstudent where stud_id=@stud_id";
            using (SqlConnection dd = new SqlConnection(sconn))
            {
                dd.Open();
                using (SqlCommand cmd = new SqlCommand(sSql, dd))
                {
                    cmd.Parameters.AddWithValue("@stud_id",sUserID);
                    ClientScript.RegisterStartupScript(Page.GetType(),"",string.Format (" <script> alert ('删除了{0}条记录'); </script>",cmd.ExecuteNonQuery()));
                    SetBind();
                }

            }
            
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortExpression"] != null)
        {
            if (ViewState["SorExpression"].ToString() == e.SortExpression)
            {
                if (ViewState["SortDirection"].ToString() == "ASC")
                {
                    ViewState["SortDireciton"] = "DESC";
                }

                else
                {
                    ViewState["SortDirection"] = "ASC";

                }
            }
            else
            {
                ViewState["SortDirection"] = "ASC";
                ViewState["SortExpression"] = e.SortExpression;
            }
        }
       else
        {  
         ViewState["SortDirection"]="ASC";
         ViewState["SortExpression"]=e.SortExpression;
        }
        SetBind();
    }
}


/*错误信息*/
用户代码未处理 System.NullReferenceException
  Message="未将对象引用设置到对象的实例。"
  Source="App_Web_6ozavrjw"
  StackTrace:
       在 _Default.GridView1_Sorting(Object sender, GridViewSortEventArgs e) 位置 e:\学学的东西\Csharp作业文件存放\WebSite1\Default.aspx.cs:行号 84
       在 System.Web.UI.WebControls.GridView.OnSorting(GridViewSortEventArgs e)
       在 System.Web.UI.WebControls.GridView.HandleSort(String sortExpression, SortDirection sortDirection)
       在 System.Web.UI.WebControls.GridView.HandleSort(String sortExpression)
       在 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
       在 System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
搜索更多相关主题的帖子: using System GridView 实例 Web 
2008-04-13 16:39
快速回复:恳请大家帮忙:GridView对数据进行排序时出现未将对象引用设置到对象的 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017089 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved