직원 이미지를 직원 데이터베이스에 저장하려고합니다. 데이터베이스 테이블 empid, empname, empimage에 세 개의 필드가 있습니다. 여기 내 데이터베이스 부분입니다. 내가 어떤 이미지를 업로드하고 클릭하고 때asp.net을 사용하는 sql 데이터베이스의 이미지 저장에 도움이 필요합니다.
<asp:Label ID="lblEmpName" runat="server" Text="Employee Name"></asp:Label>
<asp:TextBox ID="txtEName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label>
<asp:FileUpload ID="imgUpload" runat="server" />
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
 
<asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label>
<br />
<hr />
<asp:Image ID="Image1" style="width:200px" Runat="server" />
그러나 : 버튼 클릭 이벤트에서
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();
}
을 그리고 여기 내 양식입니다 이 오류를 가져 오는 버튼을 제출하십시오 "개체 참조가 개체의 인스턴스로 설정되지 않았습니다." 누군가 내 실수를 지적했다. 객체가 초기화 당신이 그것을 참조하려고하지 않을 때
덕분에, 수밋
누구나 (자신을 포함) 호의를 베풀고 코드를 올바르게 포맷 할 수 있습니까?또한 호출 스택 – mfeingold
을 포함 시키십시오. 폼을 표시하고, 또한 어떤 라인에서 오류가 발생합니까? – AnthonyWJones