Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用asp.net在sql数据库中保存图像的帮助吗_Asp.net_Image_Sql Server 2000 - Fatal编程技术网

需要使用asp.net在sql数据库中保存图像的帮助吗

需要使用asp.net在sql数据库中保存图像的帮助吗,asp.net,image,sql-server-2000,Asp.net,Image,Sql Server 2000,我正在尝试将员工图像保存到员工数据库中。在数据库表empid、empname和empmimage中有三个字段。这是我的数据库部分 CREATE DATABASE [Employee] GO USE [Employee] GO CREATE TABLE EmpDetails ( empid int IDENTITY NOT NULL, empname varchar(20), empimg image ) 在按钮单击事件中,我编写了以下代码: SqlConnection connection =

我正在尝试将员工图像保存到员工数据库中。在数据库表empid、empname和empmimage中有三个字段。这是我的数据库部分

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" />   




 

但是,当我上传任何图像并点击提交按钮时,会出现“对象引用未设置为对象实例”的错误。请有人指出我的错误

谢谢,
Sumit

对象引用错误仅在对象未初始化且您尝试引用它时发生。这可以是代码中引用的任何对象。如果在调试时单步执行代码,则可以准确地看到哪个引用为null

您有一个初始化imgByte对象的if语句:

  imgByte = new Byte[File.ContentLength];
如果img对象结果为null,则该代码不会运行。然后在此处引用imgByte,无论img是否为null:

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

检查HttpPostedFile对象本身是否不为null,并将imgByte对象初始化移到if语句之外。此外,名称文件已被System.IO.File对象使用。为了安全起见,您可能需要对其进行重命名。

我会重新安排您的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);
     }
}
这有什么好运气吗??否则,您应该在调试器中一步一步地检查代码,并查看其中您在代码中引用了空值的内容(并且您没有检查该条件)

马克

当我调试这段代码时,我 在这方面有例外吗 行…“字符串连接”= ConfigurationManager.ConnectionString[“EmployeeConnString”]。ConnectionString 例外情况是:未设置对象引用 指向对象的实例

您需要将名为EmployeeConnString的连接字符串添加到web.config


您的异常处理程序无法区分异常发生的确切位置,并针对任何可能的错误发出一条错误消息。

您能帮每个人(包括您自己)一个忙并正确格式化代码吗?还包括调用stackShow表单,以及您在哪一行得到错误?嗨,当我调试此代码时,我在这一行遇到异常…“string conn=ConfigurationManager.ConnectionString[“EmployeeConnString”].ConnectionString;“异常是:对象引用未设置为对象的实例。显然,在app.config或web.config中没有名为“EmployeeConnString”的连接字符串-请检查!Visual Studio调试器有一个选项,可以让您对任何.NET异常进行调试,即使它正在被处理。它非常有用,我一直都在运行它。在调试菜单下,单击“异常…”