本文介绍了我在上传图片代码时遇到问题..错误是未将Object reference设置为对象的实例.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是,
C#代码
受保护的无效btnSubmit_Click(对象发送者,EventArgs e)
{
SqlConnection连接= null;
试试
{
FileUpload img =(FileUpload)imgUpload;
Byte [] imgByte = null;
如果(img.HasFile && img.PostedFile!= null)
{
HttpPostedFile File = imgUpload.PostedFile;
imgByte =新的Byte [File.ContentLength];
File.InputStream.Read(imgByte,0,File.ContentLength);
}
字符串conn = ConfigurationManager.ConnectionStrings ["EmployeeConnString"].ConnectionString;
connection =新的SqlConnection(conn);
connection.Open();
字符串sql ="INSERT INTO EmpDetails(empname,empimg)VALUES(@ empname,@ empimg)";
SqlCommand cmd =新的SqlCommand(sql,连接);
cmd.Parameters.AddWithValue("@ empname",txtEName.Text.Trim());
cmd.Parameters.AddWithValue("@ empimg",imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text = String.Format("Employee ID is {0}",id);
}
赶上
{
lblResult.Text =出现错误";
}
终于
{
connection.Close();
}

my code is,
C# code
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
HttpPostedFile File = imgUpload.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
connection = new SqlConnection(conn);
connection.Open();
string sql = "INSERT INTO EmpDetails(empname,empimg)VALUES(@empname,@empimg)";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@empname", txtEName.Text.Trim());
cmd.Parameters.AddWithValue("@empimg", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text = String.Format("Employee ID is{0}", id);
}
catch
{
lblResult.Text = "There was an error";
}
finally
{
connection.Close();
}

推荐答案

try
{
string filepath = AppDomain.CurrentDomain.BaseDirectory + "gallery/";
<pre lang="cs">if (!Directory.Exists(filepath))
           {
               Directory.CreateDirectory(filepath);
           }
<pre lang="sql">if (FileUpload.HasFile)
        {
 tempName1 = Guid.NewGuid().ToString().Substring(0, 5);
string fileExtension = Path.GetExtension(FileUpload.FileName);
 imgupload.SaveAs(AppDomain.CurrentDomain.BaseDirectory + filepath + tempName1+fileExtension );

string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
connection = new SqlConnection(conn);
connection.Open();
string sql = "INSERT INTO EmpDetails(empname,empimg)VALUES(@empname,@empimg)";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@empname", txtEName.Text.Trim());
cmd.Parameters.AddWithValue("@empimg", tempName1+fileExtension);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text = String.Format("Employee ID is{0}", id);
}
}
catch
{
//error
}




谢谢
~~ Karthik.J ~~




Thanx
~~Karthik.J~~


这篇关于我在上传图片代码时遇到问题..错误是未将Object reference设置为对象的实例.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 20:39