using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using
System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.SqlClient;
using System.Data;
using
Utilities;
namespace
Database
{
public partial class ProductImageLoader :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DBConnect objDB = new DBConnect();
string strSQL = "SELECT ProductNumber, Description FROM Product ORDER BY
Description";
ddlProducts.DataSource
= objDB.GetDataSet(strSQL);
ddlProducts.DataTextField
= "Description";
ddlProducts.DataValueField
= "ProductNumber";
ddlProducts.DataBind();
}
}
// Uploads an image file and stores it in the database as a binary
object for a specific product
protected void btnUpload_Click(object sender, EventArgs e)
{
DBConnect objDB = new DBConnect();
SqlCommand objCommand = new SqlCommand();
int result = 0, imageSize;
string fileExtension, imageType, imageName, imageTitle, strSQL;
try
{
//
Use the FileUpload control to get the uploaded data
if
(FileUpload1.HasFile)
{
imageSize = FileUpload1.PostedFile.ContentLength;
byte[] imageData = new byte[imageSize];
FileUpload1.PostedFile.InputStream.Read(imageData, 0, imageSize);
imageName = FileUpload1.PostedFile.FileName;
imageType = FileUpload1.PostedFile.ContentType;
if (txtTitle.Text != "")
imageTitle = txtTitle.Text;
else
imageTitle = ddlProducts.SelectedItem.Text;
fileExtension = imageName.Substring(imageName.LastIndexOf("."));
fileExtension = fileExtension.ToLower();
if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".bmp" || fileExtension == ".gif")
{
// INSERT an image (BLOB) into the database using a stored
procedure 'storeProductImage'
strSQL = "StoreProductImage";
objCommand.CommandText
= strSQL;
objCommand.CommandType
= CommandType.StoredProcedure;
objCommand.Parameters.AddWithValue("@ImageTitle", imageTitle);
objCommand.Parameters.AddWithValue("@ImageData", imageData);
objCommand.Parameters.AddWithValue("@ImageType", imageType);
objCommand.Parameters.AddWithValue("@ImageLength", imageData.Length);
objCommand.Parameters.AddWithValue("@ProductNumber", ddlProducts.SelectedValue);
result
= objDB.DoUpdateUsingCmdObj(objCommand);
lblStatus.Text
= "Image was successully
uploaded.";
}
else
{
lblStatus.Text
= "Only jpg, bmp, and gif file formats
supported.";
}
}
}
catch (Exception ex)
{
lblStatus.Text
= "Error ocurred:
[" + ex.Message +
"] cmd=" + result;
}
}
}
}