恳请大家帮忙: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)