问题描述
公共类:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.OleDb;usingSystem.Data.SqlClient;usingSystem.IO;///<summary>///CommonClass的摘要说明///</summary>publicclassCommonClass{publicCommonClass(){////TODO:在此处添加构造函数逻辑//}publicSqlConnectionGetConnection(){stringmyStr=ConfigurationManager.AppSettings["GetConnectionString"].ToString();SqlConnectionmyConn=newSqlConnection(myStr);returnmyConn;}publicstringMessageBox(stringTxtMessage,stringUrl){stringstr;str="<scriptlanguage=javascript>alert('"+TxtMessage+"');location='"+Url+"'</script>";returnstr;}publicintGetAutoID(stringFieldName,stringTableName){SqlConnectionmyConn=GetConnection();SqlCommandmyCmd=newSqlCommand("selectMax("+FieldName+")asMaxIDfrom"+TableName,myConn);SqlDataAdapterdapt=newSqlDataAdapter(myCmd);DataSetds=newDataSet();dapt.Fill(ds);if(ds.Tables[0].Rows[0][0].ToString()==""){return1;}else{intIntFieldID=Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString())+1;return(Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString())+1);}}}页面里的代码:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;usingSystem.IO;publicpartialclass文件管理:System.Web.UI.Page{CommonClassCC=newCommonClass();protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){DDLBind();AllGVBind();this.ddlUD.Items.Insert(0,"请选择...");}}protectedvoidDDLBind(){SqlConnectionmyConn=CC.GetConnection();myConn.Open();SqlDataAdapterdapt=newSqlDataAdapter("selectdistinctfileUpDatefromtb_files",myConn);DataSetds=newDataSet();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();}protectedvoidAllGVBind(){SqlConnectionmyConn=CC.GetConnection();myConn.Open();SqlDataAdapterdapt=newSqlDataAdapter("select*fromtb_files",myConn);DataSetds=newDataSet();dapt.Fill(ds,"files");this.gvFiles.DataSource=ds.Tables["files"].DefaultView;this.gvFiles.DataKeyNames=newstring[]{"fileID"};this.DataBind();ds.Dispose();dapt.Dispose();myConn.Close();}protectedvoidPartGVBind(){SqlConnectionmyConn=newSqlConnection();myConn.Open();stringsqlStr="select*fromtb_files";if(this.TextBox1.Text.Trim()!=""||ddlUD.SelectedIndex!=0){sqlStr+="where";if(this.TextBox1.Text.Trim()!=""&&ddlUD.SelectedIndex==0){sqlStr+="fileNamelike'%"+this.TextBox1.Text.Trim()+"%'";}elseif(this.TextBox1.Text.Trim()==""&&ddlUD.SelectedIndex!=0){sqlStr+="fileUpDate='"+this.ddlUD.SelectedValue.ToString()+"'";}else{sqlStr+="fileUpDate='"+this.ddlUD.SelectedValue.ToString()+"'";sqlStr+="andfileNamelike'%"+this.TextBox1.Text.Trim()+"%'";}}SqlDataAdapterdapt=newSqlDataAdapter(sqlStr,myConn);DataSetds=newDataSet();dapt.Fill(ds,"files");this.gvFiles.DataSource=ds.Tables["files"].DefaultView;this.gvFiles.DataKeyNames=newstring[]{"fileID"};this.DataBind();ds.Dispose();dapt.Dispose();myConn.Close();}protectedvoidDeleteTFN(stringsqlStr){SqlConnectionmyConn=CC.GetConnection();myConn.Open();SqlDataAdapterdapt=newSqlDataAdapter(sqlStr,myConn);DataSetds=newDataSet();dapt.Fill(ds,"files");stringstrFilePath=Server.MapPath("Files/")+ds.Tables["files"].Rows[0][0].ToString();File.Delete(strFilePath);myConn.Close();}protectedstaticintIntIsSearch;protectedvoidButton1_Click(objectsender,EventArgse){PartGVBind();IntIsSearch=1;}protectedvoidbtnDelete_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;GridViewRowgvr=(GridViewRow)btn.Parent.Parent;stringsqlStr="selectfileTrueNamefromtb_fileswherefileID='"+gvFiles.DataKeyNames[gvr.RowIndex].ToString()+"'";DeleteTFN(sqlStr);SqlConnectionmyConn=CC.GetConnection();myConn.Open();stringsqlDelStr="deletefromtb_fileswherefileID='"+gvFiles.DataKeyNames[gvr.RowIndex].ToString()+"'";SqlCommandmyCmd=newSqlCommand(sqlDelStr,myConn);myCmd.ExecuteNonQuery();myCmd.Dispose();myConn.Close();if(IntIsSearch==1){PartGVBind();}else{AllGVBind();}}protectedvoidbtnDF_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;GridViewRowgvr=(GridViewRow)btn.Parent.Parent;stringsqlStr="selectfileTrueNamefromtb_fileswherefileID='"+gvFiles.DataKeyNames[gvr.RowIndex].ToString()+"'";SqlConnectionmyConn=CC.GetConnection();myConn.Open();SqlDataAdapterdapt=newSqlDataAdapter(sqlStr,myConn);DataSetds=newDataSet();dapt.Fill(ds,"files");stringstrFilePath=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();}}protectedvoidgvFiles_PageIndexChanging(objectsender,GridViewPageEventArgse){this.gvFiles.PageIndex=e.NewPageIndex;if(IntIsSearch==1){PartGVBind();}else{AllGVBind();}}protectedvoidgvFiles_RowDataBound(objectsender,GridViewRowEventArgse){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字段名类型长度fileIDint4fileNamevarchar50fileUpDatevarchar30fileloadvarchar200fileTureNamevarchar50运行提示未将对象引用设置到对象的实例。
解决方案
解决方案二:
单步调试,看看哪一个报错就可以了啊
解决方案三:
初学者不会那样调试
解决方案四:
在每一个函数开始的地方打个断点,然后运行程序,会在断点处停下来,然后按F10逐行调试
解决方案五:
就是你在使用这个对象之前没有实例化,从报错的那一行往上看,给个地方初始化。
解决方案六:
打完断点以后,一运行直接报错
解决方案七:
引用5楼u011344707的回复:
打完断点以后,一运行直接报错
在哪里打的断点?还没到断点处就报错了
解决方案八:
WebConfig里面有这个GetConnectionString配置吗,自学的?页面竟然是中心,我见过数据字段是中文,页面名称中文倒是第一次
解决方案九:
是自学啊。不会WebConfig配置,页面中文名好像不影响运行
解决方案十:
在每个函数开头打断点,一运行就是刚才那个界面
解决方案十一:
有变量没初始化。。。调试运行就看到了
解决方案十二:
引用2楼u011344707的回复:
初学者不会那样调试
不能使用“单步调试”。你就是在vs上按F5执行(默认Debug模式,调试运行),vs调试器就会停在出错的那一行上,你可以用鼠标指点你要查看(是否为null)的变量。初学者如果连F5键都懒得操作,就没有办法了。
解决方案十三:
引用5楼u011344707的回复:
打完断点以后,一运行直接报错
不需要“打断点”。瞎打“断点”是无头苍蝇的做法,浪费时间。从你贴出来的图片来看,调试器之前提示你“是否要启用调试”时,你点击了“取消”。这是你自己造成的。
解决方案十四:
stringmyStr=ConfigurationManager.AppSettings["GetConnectionString"].ToString();
==>ConfigurationManager.ConnectionStrings["GetConnectionString"].ConnectionString;