问题描述
- 其他信息: 未将对象引用设置到对象的实例。
-
想要用代码来编辑gridview控件的数据;然而我运行时告诉我“其他信息: 未将对象引用设置到对象的实例。”求教各位大神,该怎么改,最好有代码;下面就是我的代码,不重要的收起来了;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;public partial class liuyanban : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = "当前在线人数为" + Application["count"].ToString() + "人";
Label2.Text = Session["UserID"].ToString();
if (!IsPostBack)
{
this.bind();
}}
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["talkroomConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}protected void bind() { SqlConnection myConn = GetConnection(); myConn.Open(); string sqlStr = "select * from liaotian "; SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn); DataSet myDs = new DataSet(); myDa.Fill(myDs); GridView1.DataSource = myDs; GridView1.DataKeyNames = new string[] { "ID" }; GridView1.DataBind(); myDa.Dispose(); myDs.Dispose(); myConn.Close(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; this.bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); string CName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); string sqlStr = "update liaotian set 用户名'" + CName + "' where ID=" + ClassID; SqlConnection myConn = GetConnection(); myConn.Open(); SqlCommand myCmd = new SqlCommand(sqlStr, myConn); myCmd.ExecuteNonQuery(); myCmd.Dispose(); myConn.Close(); GridView1.EditIndex = -1; this.bind(); } protected void Button_send_Click(object sender, EventArgs e) { string took = TextBox1.Text; string connString = System.Configuration.ConfigurationManager.ConnectionStrings["talkroomConnectionString"].ConnectionString; SqlConnection myConn = new SqlConnection(connString); string sqlStr = " insert into liaotian(用户名,时间,内容) values('" + Session["UserID"].ToString() + "','" + System.DateTime.Now.ToString() + "','" + TextBox1.Text .ToString() + "') "; SqlCommand myCmd = new SqlCommand(sqlStr, myConn); myConn.Open(); myCmd.ExecuteNonQuery(); myConn.Close(); TextBox1.Text = " "; GridView1.DataBind (); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; this.bind(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection sqlCon = new SqlConnection(); sqlCon.ConnectionString = "server=PC201503061527;uid=sa;pwd=sa;database=talkroom"; sqlCon.Open(); string sql = string.Format("select * from liaotian where 用户名='{0}' ", Session["UserID"].ToString()); try { SqlCommand cmd = new SqlCommand(sql, sqlCon); SqlDataAdapter da = new SqlDataAdapter(sql, sqlCon); DataSet dadaset = new DataSet("liaotian"); da.Fill(dadaset); this.GridView1.DataSourceID = null; GridView1.DataSource = dadaset.Tables[0]; GridView1.DataBind(); } catch { } }
解决方案
那句报错?用到session或者application对象时最好判断下所读取的键是否存在,要不程序池回收会导致session和application被释放,在调用ToString就报错了
if(Application["count"]!=null)Label2.Text = "当前在线人数为" + Application["count"].ToString() + "人";
if( Session["UserID"]!=null)Label2.Text = Session["UserID"].ToString();
解决方案二:
this.GridView1.DataSourceID = null; 这句话不需要
解决方案三:
自定义的UI没有包含.GridView1控件
解决方案四:
很多可能,比如session application datasource等等为null,你得跟据错误的行来排错,如果你的web.config禁用了调试,你先开启。然后知道了错误的行,就显而易见了。总之有变量为null
解决方案五:
肯定用了一个为null的对象,可以开启调试模式,一步步跟踪代码,你会发现bug