ASP.net C# MySql数据库 开发 想实现的功能是把图片以二进制流的方式存入数据库,可是程序都和网上的示例一样,但是一直报如下的错误,期待高人解答~~~异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 25: protected void Button1_Click(object sender, EventArgs e)
行 26: {
行 27: HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
行 28: FileLength = UpFile.ContentLength; //记录文件长度
行 29: try
源文件: d:\项目\basketballManageClub\test_test2.aspx.cs 行: 27
源代码 如下:
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.IO;
using MySql.Data;
using MySql.Data.MySqlClient;
public partial class test : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE;
protected Int32 FileLength = 0; protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength == 0)
{
this.Label2.Text = "<b>请你选择上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图片临时存储的Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对象
//读取图像文件数据,FileByteArray为图像存储的实体,0为读取起始位置,FileLength为数据长度
StreamObject.Read(FileByteArray, 0, FileLength); //建立Sql Server连接
MySqlConnection con = new MySqlConnection("server =localhost;database =bmc;uid=root;pwd = ");
String SqlCmd = "insert into image(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@ContentType,@ImageDescription,@ImageSize)";
MySqlCommand CmdObj = new MySqlCommand(SqlCmd, con);
//设置参数
CmdObj.Parameters.Add("@Image", MySqlDbType.Binary, FileLength); //图片信息
CmdObj.Parameters.Add("@ContentType", MySqlDbType.VarChar, 50); //图片类型
CmdObj.Parameters.Add("@ImageDescription", MySqlDbType.VarChar, 200);//图片简介
CmdObj.Parameters.Add("@ImageSize", MySqlDbType.Int32); //图片大小 CmdObj.Parameters["@Image"].Value = FileByteArray;
//记录文件类型
CmdObj.Parameters["@ContentType"].Value = UpFile.ContentType;
//其他表单数据记录上传
CmdObj.Parameters["@ImageDescription"].Value = this.TextBox1.Text;
//记录文件长度
CmdObj.Parameters["@ImageSize"].Value = UpFile.ContentLength; con.Open();
CmdObj.ExecuteNonQuery();
con.Close(); this.Label2.Text = "<b>图片上传成功</b>";
}
}
catch (Exception ex)
{
this.Label2.Text = ex.ToString();
}
}
}
行 25: protected void Button1_Click(object sender, EventArgs e)
行 26: {
行 27: HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
行 28: FileLength = UpFile.ContentLength; //记录文件长度
行 29: try
源文件: d:\项目\basketballManageClub\test_test2.aspx.cs 行: 27
源代码 如下:
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.IO;
using MySql.Data;
using MySql.Data.MySqlClient;
public partial class test : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE;
protected Int32 FileLength = 0; protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength == 0)
{
this.Label2.Text = "<b>请你选择上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图片临时存储的Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对象
//读取图像文件数据,FileByteArray为图像存储的实体,0为读取起始位置,FileLength为数据长度
StreamObject.Read(FileByteArray, 0, FileLength); //建立Sql Server连接
MySqlConnection con = new MySqlConnection("server =localhost;database =bmc;uid=root;pwd = ");
String SqlCmd = "insert into image(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@ContentType,@ImageDescription,@ImageSize)";
MySqlCommand CmdObj = new MySqlCommand(SqlCmd, con);
//设置参数
CmdObj.Parameters.Add("@Image", MySqlDbType.Binary, FileLength); //图片信息
CmdObj.Parameters.Add("@ContentType", MySqlDbType.VarChar, 50); //图片类型
CmdObj.Parameters.Add("@ImageDescription", MySqlDbType.VarChar, 200);//图片简介
CmdObj.Parameters.Add("@ImageSize", MySqlDbType.Int32); //图片大小 CmdObj.Parameters["@Image"].Value = FileByteArray;
//记录文件类型
CmdObj.Parameters["@ContentType"].Value = UpFile.ContentType;
//其他表单数据记录上传
CmdObj.Parameters["@ImageDescription"].Value = this.TextBox1.Text;
//记录文件长度
CmdObj.Parameters["@ImageSize"].Value = UpFile.ContentLength; con.Open();
CmdObj.ExecuteNonQuery();
con.Close(); this.Label2.Text = "<b>图片上传成功</b>";
}
}
catch (Exception ex)
{
this.Label2.Text = ex.ToString();
}
}
}
解决方案 »
- ado.net操作oracle数据库时出错???
- 在VS中运行页面和发布之后在IIS中运行到底有什么区别
- 多前台共有一个后台<销售渠道管理>请顶尖的高手帮忙分析数据库设计
- 新上线Asp.net网站:欢迎鲜花和鸡蛋,.net2.0/sql 2000、单点登陆、远程图片、部分自定Ajax ...
- 没分了 帮帮忙啊 正则表达式高手请进 急啊!!!!!!!
- Manifest问题!
- 谁愿意和我一起做学校管理系统
- 我要求做到每一个用户发布一次自己的产品图片都要建在 upimg 目录下的一个子目录里,且这个子目录不能重名
- 高分求助:《aps.net web 应用程序开发新思维》的配书源码!!!!
- 没有为该对象定义无参数的构造函数,新人求教
- asp.net用户登录后不能修改密码
- 在JavaScript中,如何对TextBox的值进行修改?
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 25: protected void Button1_Click(object sender, EventArgs e)
行 26: {
行 27: HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
行 28: FileLength = UpFile.ContentLength; //记录文件长度
行 29: try
源文件: d:\项目\basketballManageClub\test_test2.aspx.cs 行: 27 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
test_test2.Button1_Click(Object sender, EventArgs e) in d:\testtest.aspx.cs:27
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919