提示未将对象引用设置到对象的实例。

Dubhe_zhan 2014-07-19 02:58:35
公共类:
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.OleDb;
using System.Data.SqlClient;
using System.IO;

/// <summary>
/// CommonClass 的摘要说明
/// </summary>
public class CommonClass
{
public CommonClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["GetConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
public string MessageBox(string TxtMessage, string Url)
{
string str;
str = "<script language=javascript>alert('" + TxtMessage + "');location='"+Url+"'</script>";
return str;
}
public int GetAutoID(string FieldName, string TableName)
{
SqlConnection myConn = GetConnection();
SqlCommand myCmd = new SqlCommand("select Max(" + FieldName + ")as MaxID from" + TableName, myConn);
SqlDataAdapter dapt = new SqlDataAdapter(myCmd);
DataSet ds = new DataSet();
dapt.Fill(ds);
if (ds.Tables[0].Rows[0][0].ToString() == "")
{
return 1;
}
else
{
int IntFieldID = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1;
return (Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1);
}
}
}

页面里的代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.IO;
public partial class 文件管理 : System.Web.UI.Page
{
CommonClass CC = new CommonClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DDLBind();
AllGVBind();
this.ddlUD.Items.Insert(0, "请选择...");
}
}
protected void DDLBind()
{
SqlConnection myConn = CC.GetConnection();
myConn.Open();
SqlDataAdapter dapt = new SqlDataAdapter("select distinct fileUpDate from tb_files", myConn);
DataSet ds = new DataSet();
dapt.Fill(ds,"files");
this.ddlUD.DataSource = ds.Tables["files"].DefaultView;
this.ddlUD.DataTextField = ds.Tables["files"].Columns[0].ToString();
this.ddlUD.DataBind();
ds.Dispose();
dapt.Dispose();
myConn.Close();
}
protected void AllGVBind()
{
SqlConnection myConn = CC.GetConnection();
myConn.Open();
SqlDataAdapter dapt = new SqlDataAdapter("select * from tb_files", myConn);
DataSet ds = new DataSet();
dapt.Fill(ds,"files");
this.gvFiles.DataSource=ds.Tables["files"].DefaultView;
this.gvFiles.DataKeyNames=new string[]{"fileID"};
this.DataBind();
ds.Dispose();
dapt.Dispose();
myConn.Close();
}
protected void PartGVBind()
{
SqlConnection myConn = new SqlConnection();
myConn.Open();
string sqlStr = "select * from tb_files";
if (this.TextBox1.Text.Trim() != "" || ddlUD.SelectedIndex != 0)
{
sqlStr += "where";
if (this.TextBox1.Text.Trim() != "" && ddlUD.SelectedIndex == 0)
{
sqlStr += "fileName like'%" + this.TextBox1.Text.Trim() + "%'";
}
else if (this.TextBox1.Text.Trim() == "" && ddlUD.SelectedIndex != 0)
{
sqlStr += "fileUpDate='" + this.ddlUD.SelectedValue.ToString() + "'";
}
else
{
sqlStr += "fileUpDate='" + this.ddlUD.SelectedValue.ToString() + "'";
sqlStr += "and fileName like'%" + this.TextBox1.Text.Trim() + "%'";
}
}
SqlDataAdapter dapt = new SqlDataAdapter(sqlStr, myConn);
DataSet ds=new DataSet();
dapt.Fill(ds,"files");
this.gvFiles.DataSource=ds.Tables["files"].DefaultView;
this.gvFiles.DataKeyNames=new string[]{"fileID"};
this.DataBind();
ds.Dispose();
dapt.Dispose();
myConn.Close();
}
protected void DeleteTFN(string sqlStr)
{
SqlConnection myConn = CC.GetConnection();
myConn.Open();
SqlDataAdapter dapt = new SqlDataAdapter(sqlStr, myConn);
DataSet ds = new DataSet();
dapt.Fill(ds,"files");
string strFilePath = Server.MapPath("Files/") + ds.Tables["files"].Rows[0][0].ToString();
File.Delete(strFilePath);
myConn.Close();
}
protected static int IntIsSearch;
protected void Button1_Click(object sender, EventArgs e)
{
PartGVBind();
IntIsSearch = 1;
}
protected void btnDelete_Click(object sender,EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.Parent.Parent;
string sqlStr = "select fileTrueName from tb_files where fileID='" + gvFiles.DataKeyNames[gvr.RowIndex].ToString() + "'";
DeleteTFN(sqlStr);
SqlConnection myConn = CC.GetConnection();
myConn.Open();
string sqlDelStr="delete from tb_files where fileID='"+gvFiles.DataKeyNames[gvr.RowIndex].ToString()+"'";
SqlCommand myCmd = new SqlCommand(sqlDelStr,myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
if (IntIsSearch == 1)
{
PartGVBind();
}
else
{
AllGVBind();
}

}
protected void btnDF_Click(object sender, EventArgs e)
{
Button btn = ( Button)sender;
GridViewRow gvr = (GridViewRow) btn.Parent.Parent;
string sqlStr = "select fileTrueName from tb_files where fileID='" + gvFiles.DataKeyNames[gvr.RowIndex].ToString() + "'";
SqlConnection myConn = CC.GetConnection();
myConn.Open();
SqlDataAdapter dapt = new SqlDataAdapter(sqlStr, myConn);
DataSet ds = new DataSet();
dapt.Fill(ds, "files");
string strFilePath = Server.MapPath("File//" + ds.Tables["files"].Rows[0][0].ToString());
ds.Dispose();
if (File.Exists(strFilePath))
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(strFilePath, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", strFilePath.Length.ToString());
Response.WriteFile(strFilePath);
Response.Flush();
Response.End();
}
}
protected void gvFiles_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvFiles.PageIndex = e.NewPageIndex;
if (IntIsSearch == 1)
{
PartGVBind();
}
else
{
AllGVBind();
}
}
protected void gvFiles_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#00f7f';this.style.color='buttontext';this.style.cursor='default';");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor='';this.style.color=''");
e.Row.Attributes.Add("ondblclick","window.open('FileInfo.sapx?="+e.Row.Cells[0].Text+"')");
}
}
}
数据库表:数据库名:tb_files
字段名 类型 长度
fileID int 4
fileName varchar 50
fileUpDate varchar 30
fileload varchar 200
fileTureName varchar 50
运行提示未将对象引用设置到对象的实例。
...全文
473 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
EdsionWang 2014-07-20
  • 打赏
  • 举报
回复
 string myStr = ConfigurationManager.AppSettings["GetConnectionString"].ToString();
==>
ConfigurationManager.ConnectionStrings["GetConnectionString"].ConnectionString; 
  • 打赏
  • 举报
回复
引用 5 楼 u011344707 的回复:
打完断点以后,一运行直接报错
不需要“打断点”。瞎打“断点”是无头苍蝇的做法,浪费时间。 从你贴出来的图片来看,调试器之前提示你“是否要启用调试”时,你点击了“取消”。这是你自己造成的。
  • 打赏
  • 举报
回复
引用 2 楼 u011344707 的回复:
初学者不会那样调试
不能使用“单步调试”。你就是在vs上按F5执行(默认Debug模式,调试运行),vs调试器就会停在出错的那一行上,你可以用鼠标指点你要查看(是否为null)的变量。 初学者如果连F5键都懒得操作,就没有办法了。
欢乐的小猪 2014-07-20
  • 打赏
  • 举报
回复
有变量没初始化。。。调试运行就看到了
Dubhe_zhan 2014-07-19
  • 打赏
  • 举报
回复
在每个函数开头打断点,一运行就是刚才那个界面
Dubhe_zhan 2014-07-19
  • 打赏
  • 举报
回复
是自学啊。不会WebConfig配置,页面中文名好像不影响运行
wtnu200 2014-07-19
  • 打赏
  • 举报
回复
WebConfig里面有这个GetConnectionString配置吗,自学的?页面竟然是中心,我见过数据字段是中文,页面名称中文倒是第一次
熙风 2014-07-19
  • 打赏
  • 举报
回复
引用 5 楼 u011344707 的回复:
打完断点以后,一运行直接报错
在哪里打的断点?还没到断点处就报错了
Dubhe_zhan 2014-07-19
  • 打赏
  • 举报
回复
打完断点以后,一运行直接报错
涛锅 2014-07-19
  • 打赏
  • 举报
回复
就是你在使用这个对象之前没有实例化,从报错的那一行往上看,给个地方初始化。
chinnsyuutou 2014-07-19
  • 打赏
  • 举报
回复
在每一个函数开始的地方打个断点,然后运行程序,会在断点处停下来,然后按F10逐行调试
Dubhe_zhan 2014-07-19
  • 打赏
  • 举报
回复
初学者不会那样调试
chinnsyuutou 2014-07-19
  • 打赏
  • 举报
回复
单步调试,看看哪一个报错就可以了啊

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧