2
Answers

Insert Image into database using AsyncFileUpload in Asp.Net

Bineesh  VP

Bineesh VP

10y
2.9k
1
Sir, me having a problem of insert image into database using Ajax AsyncFileUpload control. P

Please verify the code and correct and send back.

The database table to which image to be inserted is:-

tbl_Studentdetails:-




The AsyncFileUpload is implemented as:-

<td>
    <ajax:AsyncFileUpload ID="FupStudent" runat="server" UploaderStyle="Traditional"
            UploadingBackColor="OliveDrab" Width="212px"  />
     </td>




Finally, the code to add data into database is below. This code getting a error:-




  the line in Red color, is where exception take place.

protected void btnFileUpload_Click(object sender, EventArgs e)
        {
            try
            {
                string strFilePath = FupStudent.PostedFile.FileName;
                string strFileName = Path.GetFileName(strFilePath);
                string strExtension = Path.GetExtension(strFileName);
                string strContentType = strExtension;
                switch (strExtension)
                {
                    case ".jpeg":
                        strContentType = "Image/jpeg";
                        break;
                    case ".Png":
                        strContentType = "Image/PNG";
                        break;
                    case ".gif":
                        strContentType = "Image/GIF";
                        break;
                }
                if (strContentType != string.Empty)
                {
                    Stream fStream = FupStudent.PostedFile.InputStream;
                    BinaryReader bReader = new BinaryReader(fStream);
                    Byte[] bytes = bReader.ReadBytes((int)fStream.Length);
                    if (sqlCon.State == ConnectionState.Closed)
                    {
                        sqlCon.Open();
                    }
                    SqlCommand cmd = new SqlCommand("StudentAdd", sqlCon); // see the stored procedure below
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@firstName", txtFirstName.Text);
                    cmd.Parameters.AddWithValue("@lastName", txtLastName.Text);
                    cmd.Parameters.AddWithValue("@Age", txtAge.Text);
                    cmd.Parameters.AddWithValue("@Location", txtLocation.Text);
                    cmd.Parameters.Add(new SqlParameter("@Photo", DbType.Binary));
                    cmd.Parameters["@Photo"].Value = FupStudent.PostedFile;
                    int inCount=cmd.ExecuteNonQuery();
                    if (inCount > 0)
                    {
                        HttpContext.Current.Response.Write("<script>alert('Data Inserted Successfully')</script>");
                    }
                   
                }

            }
            catch (Exception ex)
            {
               
                HttpContext.Current.Response.Write("<script>alert('Student: 06')</script>" + ex.Message);
            }


ALTER PROCEDURE  StudentAdd
@firstName varchar(50),
@lastName varchar(50),
@Age int,
@Location varchar(50),
@Photo VarBinary(Max)

AS 

 
INSERT INTO tbl_StudentDetails
                      (firstName, lastName, Age, Location,Photo)
VALUES     (@firstName, @lastName, @Age, @Location,@Photo)


Answers (2)