“/webexcel”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 59: private void Button1_Click(object sender, System.EventArgs e)
行 60: {
行 61: if (File1.PostedFile!=null)
行 62: {
行 63: rd=new Random(1);
源文件: d:\zjweb\webexcel\webform2.aspx.cs 行: 61 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
webexcel.WebForm2.Button1_Click(Object sender, EventArgs e) in d:\zjweb\webexcel\webform2.aspx.cs:61
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 59: private void Button1_Click(object sender, System.EventArgs e)
行 60: {
行 61: if (File1.PostedFile!=null)
行 62: {
行 63: rd=new Random(1);
源文件: d:\zjweb\webexcel\webform2.aspx.cs 行: 61 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
webexcel.WebForm2.Button1_Click(Object sender, EventArgs e) in d:\zjweb\webexcel\webform2.aspx.cs:61
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
解决方案 »
- asp.net ajax 如何实现自动查询功能?
- C#Bitmap System.ArgumentException: 参数无效
- 注册时与数据库中的属性列进行对比,存在则允许注册?
- 关于后台管理员登录的问题
- 无法隐藏登录名和密码
- 想做一个视频聊天室网站,不知道该怎么入手,高手指点一下吧
- 500分关于网站打包:以前在这里搜索资料想用VS来做安装程序,各种方法均失败,哪位能够提供其他解决办法?500分。
- 初学ASP.NET的几个疑惑问题?
- 迷途的羔羊要返航----重归本行业
- 抛砖引玉,在CS中怎么找到多级嵌套的DetailsView 中的控件?
- 散100分 求:Google的Calendar的实现原理,有相关的源代码更好!!!
- 分页问题
给的代码太少了
检查下在应用之前有没有实例化了
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;
namespace webexcel
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.DataGrid DataGrid3;
protected System.Web.UI.WebControls.Label Label2;
public Random rd;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
if (File1.PostedFile!=null)
{
rd=new Random(1);
string filename=DateTime.Now.Date.ToString("yyyymmdd")+DateTime.Now.ToLongTimeString().Replace(":","")+rd.Next(9999).ToString()+".xls";
File1.PostedFile.SaveAs(@Server.MapPath("file/")+filename);
//Response.Write(File1.PostedFile.FileName.ToString());
//Response.Write("上传成功");
Label1.Text="文件名为"+filename;
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+Server.MapPath("file")+"/"+filename+";Extended Properties=Excel 8.0" ;
OleDbConnection thisconnection=new OleDbConnection(conn);
thisconnection.Open();
string Sql="select * from [Sheet1$]";
OleDbDataAdapter mycommand=new OleDbDataAdapter(Sql,thisconnection);
DataSet ds=new DataSet();
mycommand.Fill(ds,"[Sheet1$]");
thisconnection.Close();
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
string conn1="User ID=sa;Data Source=127.0.0.1;Password=sa;Initial Catalog=index;Provider=SQLOLEDB.1;";
OleDbConnection thisconnection1=new OleDbConnection(conn1);
thisconnection1.Open();
int count=ds.Tables["[Sheet1$]"].Rows.Count;
for (int i=0;i<count;i++)
{
string id,id_1,id_2,id_3;
id=ds.Tables["[Sheet1$]"].Rows[i]["id"].ToString();
id_1=ds.Tables["[Sheet1$]"].Rows[i]["id_1"].ToString();
id_2=ds.Tables["[Sheet1$]"].Rows[i]["id_2"].ToString();
id_3=ds.Tables["[Sheet1$]"].Rows[i]["id_3"].ToString();
string excelsql="insert into excel(id,id_1,id_2,id_3) values ('"+id+"','"+id_1+"','"+id_2+"','"+id_3+"') ";
OleDbCommand mycommand1=new OleDbCommand(excelsql,thisconnection1);
mycommand1.ExecuteNonQuery();
}
Response.Write("更新成功");
thisconnection1.Close();
}
}
} }