2009-11-08 76 views
0

我試圖將員工圖像保存在員工數據庫中。我在數據庫表empid,empname,empimage中有三個字段。這是我的數據庫部分。在使用asp.net的sql數據庫中保存圖像需要幫助

CREATE DATABASE [Employee] 
GO 
USE [Employee] 
GO 
CREATE TABLE EmpDetails 
(
empid int IDENTITY NOT NULL, 
empname varchar(20), 
empimg image 
) 

在按鈕的單擊事件,我寫了下面的代碼:

SqlConnection connection = null; 
    try 
    { 
     FileUpload img = (FileUpload)imgUpload; 
     Byte[] imgByte = null; 
     if (img.HasFile && img.PostedFile != null) 
     { 
      //To create a PostedFile 
      HttpPostedFile File = imgUpload.PostedFile; 
      //Create byte Array with file len 
      imgByte = new Byte[File.ContentLength]; 
      //force the control to load data in array 
      File.InputStream.Read(imgByte, 0, File.ContentLength); 
     } 
     // Insert the employee name and image into db 
     string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString; 
     connection = new SqlConnection(conn); 

     connection.Open(); 
     string sql = "INSERT INTO EmpDetails(empname,empimg) VALUES(@enm, @eimg)SELECT @@IDENTITY"; 
     SqlCommand cmd = new SqlCommand(sql, connection); 
     cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim()); 
     cmd.Parameters.AddWithValue("@eimg", 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(); 
    } 

,這裏是我的表格:

<asp:Label ID="lblEmpName" runat="server" Text="Employee Name"></asp:Label> 
    &nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:TextBox ID="txtEName" runat="server"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label> 
    &nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:FileUpload ID="imgUpload" runat="server" /> 
    <br /> 
    <br /> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
     onclick="btnSubmit_Click" /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp  
    <asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label> 
    <br /> 
    <hr /> 
    <asp:Image ID="Image1" style="width:200px" Runat="server" /> 

但是當我上傳任何圖片和點擊提交按鈕獲取此錯誤「對象引用未設置爲對象的實例。」請有人指出我的錯誤。

謝謝,僅在未初始化的對象,並嘗試引用它發生 薩米特

+0

你可以做每個人(包括你自己)一個忙,並正確地格式化代碼?還包括調用堆棧 – mfeingold 2009-11-08 14:02:03

+0

向我們顯示錶單,還有哪條線路會出錯? – AnthonyWJones 2009-11-08 14:15:39

回答

1

對象引用錯誤。這可能是您在代碼中引用的任何對象。如果您在調試時仔細查看代碼,則可以準確查看哪個引用爲空。

您有一個聲明,如果初始化imgByte對象:

imgByte = new Byte[File.ContentLength]; 

如果IMG對象原來是空,代碼不運行。然後,你在這裏引用imgByte,無論IMG與否爲空:

cmd.Parameters.AddWithValue("@eimg", imgByte); 

檢查的HttpPostedFile對象本身不是null,移動你的imgByte對象初始化if語句之外。此外,名稱File已被System.IO.File對象使用。你可能想重命名它只是爲了安全。

+0

即使正在處理它,Visual Studio調試器也有一個選項可以將您引入任何.NET異常的調試。這非常有用,而且我一直在設置它。在調試菜單下,單擊「異常...」 – 2009-11-08 16:26:07

0

我會重新安排你的ADO.NET代碼 - 使它更安全,更可靠;同時,我會確保用分號在您的「SQL」串兩個SQL語句分開,使之清楚,SQL,這是兩個命令,真的:

string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString; 

using(connection = new SqlConnection(conn)) 
{ 
    string sqlStmt = "INSERT INTO dbo.EmpDetails(empname, empimg) " + 
         "VALUES(@enm, @eimg); SELECT @@IDENTITY"; 

    using(SqlCommand cmd = new SqlCommand(sqlStmt, connection)) 
    { 
     cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim()); 
     cmd.Parameters.AddWithValue("@eimg", imgByte); 

     connection.Open(); 

     int id = Convert.ToInt32(cmd.ExecuteScalar()); 

     connection.Close(); 

     lblResult.Text = String.Format("Employee ID is {0}", id); 
    } 
} 

任何這運氣?否則,你應該真的在調試器中逐步完成代碼,並參見,其中代碼中的引用了NULL(並且不檢查該條件)。

馬克

+0

嗨雖然我正在調試此代碼,我在這一行中得到異常...「string conn = ConfigurationManager.ConnectionStrings [」EmployeeConnString「]。ConnectionString;」 例外情況是: 未將對象引用設置爲對象的實例。 – Sumit 2009-11-08 15:48:37

+0

顯然,您的app.config或web.config中沒有連接字符串「EmployeeConnString」 - 請檢查! – 2009-11-08 16:34:26

0

雖然我調試這個代碼我是 得到例外在此 線... 「串康恩= ConfigurationManager.ConnectionStrings [」 EmployeeConnString 「]的ConnectionString;」。 例外是:對象引用未將 設置爲對象的實例。

您需要將名爲EmployeeConnString的連接字符串添加到web.config中。

您的異常處理程序無法區分發生異常的位置,並針對任何可能的錯誤發出單個錯誤消息。