提示未将对象引用设置到对象的实例。
公共类:
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
运行提示未将对象引用设置到对象的实例。