程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> asp.net-其他信息: 未將對象引用設置到對象的實例。

asp.net-其他信息: 未將對象引用設置到對象的實例。

編輯:編程綜合問答
其他信息: 未將對象引用設置到對象的實例。

想要用代碼來編輯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
    { 

    }
}

}![圖片說明](http://img.ask.csdn.net/upload/201507/10/1436529438_407413.png)圖片說明

最佳回答:


那句報錯?用到session或者application對象時最好判斷下所讀取的鍵是否存在,要不程序池回收會導致session和application被釋放,在調用ToString就報錯了

 if(Application["count"]!=null)Label2.Text = "當前在線人數為" + Application["count"].ToString() + "人";
if( Session["UserID"]!=null)Label2.Text = Session["UserID"].ToString();
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved