运行后出现错误“未将对象引用设置到对象的实例”------- DataRow[] dr = ds.Tables[0].Select();
protectedvoidButton1_Click1(objectsender,EventArgse){if(FileUpload1.HasFile==false)//...
protected void Button1_Click1(object sender, EventArgs e) { if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件 { Response.Write("<script>alert('请您选择Excel文件')</script> "); return;//当无文件时,返回 } string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名 if (IsXls != ".xls") { Response.Write("<script>alert('只可以选择Excel文件')</script>"); return;//当选择的不是Excel文件时,返回 } string filename = FileUpload1.FileName; //获取Execle文件名 DateTime日期函数 string savePath = Server.MapPath(("upfiles") + filename);//Server.MapPath 获得虚拟服务器相对路径 FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上 DataSet ds = ExcelSqlConnection(savePath, filename); //调用自定义方法 //DataRow[] dr = new DataRow(); //dr = ds.Tables[0].Select(); DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组 int rowsnum = ds.Tables[0].Rows.Count; if (rowsnum == 0) { Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示 } else { for (int i = 0; i < dr.Length; i++) { string id = dr[i]["学号"].ToString(); string pwd = dr[i]["密码"].ToString(); string classes = dr[i]["班级"].ToString(); string major = dr[i]["专业"].ToString(); } Response.Write("<script>alert('Excle表导入成功!');</script>"); }
}
#region 连接Excel 读取Excel数据 并返回DataSet数据集合 public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName) { string strCon = "server=.;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"; SqlConnection ExcelConn = new SqlConnection(strCon); try { string strCom = string.Format("SELECT * FROM [Sheet1$]"); ExcelConn.Open(); SqlDataAdapter myCommand = new SqlDataAdapter(strCom, ExcelConn); DataSet ds = new DataSet(); myCommand.Fill(ds, "[" + tableName + "$]"); ExcelConn.Close(); return ds; } catch { ExcelConn.Close(); return null; } } #endregion } 展开
}
#region 连接Excel 读取Excel数据 并返回DataSet数据集合 public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName) { string strCon = "server=.;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"; SqlConnection ExcelConn = new SqlConnection(strCon); try { string strCom = string.Format("SELECT * FROM [Sheet1$]"); ExcelConn.Open(); SqlDataAdapter myCommand = new SqlDataAdapter(strCom, ExcelConn); DataSet ds = new DataSet(); myCommand.Fill(ds, "[" + tableName + "$]"); ExcelConn.Close(); return ds; } catch { ExcelConn.Close(); return null; } } #endregion } 展开
展开全部
"未将对象引用设置到对象的实例" 就是 你在获取数据时并未获取到数据,但你在后面的程序中用到了这个数据集的数据单元去取数据,这样会报错。
追问
那请问该如何修改呢,我是个菜鸟不会改。。。。。谢谢奥
追答
for (int i = 0; i < dr.Length; i++) { string id = dr[i]["学号"].ToString(); string pwd = dr[i]["密码"].ToString(); string classes = dr[i]["班级"].ToString(); string major = dr[i]["专业"].ToString();
修改:
if(dr!=null)
{
for (int i = 0; i < dr.Length; i++) { string id = dr[i]["学号"].ToString(); string pwd = dr[i]["密码"].ToString(); string classes = dr[i]["班级"].ToString(); string major = dr[i]["专业"].ToString();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询