Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi
I have created a project on asp.net on jewelry inventory. the fields are serial no,category, image, description,cost price, selling price

In my project I have used image uploader to upload images on server. this part works fine.
But if i want to edit any image later and put new image, the updated image is not displaying after i click the update button

My images are saved in db in binary form. because if it was in bytes, then I couldn't retrieve the images from the db to display on the crystal reports

Every time i have to clear the cache then it comes.
And I have to log out from the application running on domain and then log in,then it displays it.

This is not the good approach to do it. every time a user cannot clear the cache and do it

Kindly help me with coding for different approach so that my editing of image woks fine upon clicking on update button

I am putting the code in the next page

I hope experts will help me after evaluating my code and help me suggest where I have gone wrong
Posted
Comments
sudeshna from bangkok 10-Oct-15 2:26am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;

public partial class SearchByCode : System.Web.UI.Page
{
///
/// Product image folder path to read image file from folder or write image file in to folder.
///

private string imagePath = string.Empty;

protected void Page_Load(object sender, EventArgs e)
{
this.imagePath = Server.MapPath("ProductImage/");
}

protected void btnSearch_Click(object sender, EventArgs e)
{
this.BindJewellery();
}

private void BindJewellery()
{
DataSet dsJewellery = this.GetAllJewellery(this.txtSerialNumber.Text);
if (dsJewellery != null && dsJewellery.Tables.Count > 0 && dsJewellery.Tables[0].Rows.Count > 0)
{
this.lblMessageInfo.Visible = false;
this.gvJewellery.DataSource = dsJewellery;
this.gvJewellery.DataBind();
this.gvJewellery.Visible = true;
}
else
{
this.gvJewellery.Visible = false;
this.lblMessageInfo.Text = "No record(s) found!";
this.lblMessageInfo.Visible = true;
this.divUpdateJewellary.Visible = false;
}
}

private DataSet GetAllJewellery(string serialNumber)
{
using (SqlConnection con = new SqlConnection(JewelleryHelper.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("spGetJeweleryOnSerialNumber", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = serialNumber;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
return ds;
}
}
}

protected void lbtnEdit_Click(object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton)sender;
DataSet ds = this.GetAllJewellery(lbtn.CommandArgument);
if (ds != null && ds.Tables.Count > 0)
{
this.txtSlNumber.Text = ds.Tables[0].Rows[0]["SerialNumber"].ToString();
this.ddlCategory.SelectedValue = ds.Tables[0].Rows[0]["Category"].ToString();
this.imageProduct.Src = "ProductImage/" + JewelleryHelper.GetImageFromBinaryData(ds.Tables[0].Rows[0]["Image"], ds.Tables[0].Rows[0]["SerialNumber"].ToString(), this.imagePath);
this.txtdesc.Text = ds.Tables[0].Rows[0]["Description"].ToString();
this.txtCostPrice.Text = ds.Tables[0].Rows[0]["CostPrice"].ToString();
this.txtSoldPrice.Text = ds.Tables[0].Rows[0]["SoldPrice"].ToString();
this.txtCustomer.Text = ds.Tables[0].Rows[0]["CustomerName"].ToString();
if (!string.IsNullOrWhiteSpace(Convert.ToString(ds.Tables[0].Rows[0]["SoldDate"])))
{
this.txtsolddate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["SoldDate"].ToString()).ToString("dd/MM/yyyy");
}
this.divUpdateJewellary.Visible = true;
}
else
{
this.lblInfo.Text = "An error occured while retrieving data jewelry.";
this.lblInfo.Visible = true;
this.lblInfo.ForeColor = System.Drawing.Color.Red;
}
}

protected void gvJewellery_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (drv != null)
{
LinkButton lbtnEdit = e.Row.FindControl("lbtnEdit") as LinkButton;
LinkButton lbtnJewellery = e.Row.FindControl("lbtnJewellery") as LinkButton;
Image imgJe
sudeshna from bangkok 10-Oct-15 2:28am    
protected void ibtnUploadImage_Click(object sender, ImageClickEventArgs e)
{
if (this.fuUploadImage.HasFile)
{
this.fuUploadImage.PostedFile.SaveAs(this.imagePath + this.fuUploadImage.FileName);
this.imageProduct.Src = "ProductImage/" + this.fuUploadImage.FileName;
}
}

protected void lbtnJewellery_Click(object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton)sender;
DataSet ds = this.GetJewelleryBySerialNumber(lbtn.CommandArgument);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
this.lblSerialNumber.Text = Convert.ToString(ds.Tables[0].Rows[0]["SerialNumber"]);
this.lblCategory.Text = Convert.ToString(ds.Tables[0].Rows[0]["Category"]);
this.lblConsignmentName.Text = !string.IsNullOrWhiteSpace(Convert.ToString(ds.Tables[0].Rows[0]["ConsignmentName"])) ? Convert.ToString(ds.Tables[0].Rows[0]["ConsignmentName"]) : "N/A";
this.imageProductPopup.Src = "ProductImage/" + JewelleryHelper.GetImageFromBinaryData(ds.Tables[0].Rows[0]["Image"], Convert.ToString(ds.Tables[0].Rows[0]["SerialNumber"]), this.imagePath);
this.lblDescription.Text = Convert.ToString(ds.Tables[0].Rows[0]["Description"]);
this.lblCostPrice.Text = Convert.ToString(ds.Tables[0].Rows[0]["CostPrice"]);
this.lblSellingPrice.Text = Convert.ToString(ds.Tables[0].Rows[0]["SellingPrice"]);
ScriptManager.RegisterStartupScript(this, this.GetType(), "JewelleryInfo", "loadPopup();", true);
}
}

protected void btnUploadJewelry_Click(object sender, EventArgs e)
{
int flag = this.UpdateJewelry();
if (flag > 0)
{
JewelleryHelper.DeleteFileIfExists(Server.MapPath(this.imageProduct.Src));
////JewelleryHelper.DeleteTemporaryInternetFiles();
this.BindJewellery();
this.divUpdateJewellary.Visible = false;
this.lblInfo.Visible = false;
////Alert message after jewellery added successfully.
ScriptManager.RegisterStartupScript(this, this.GetType(), "AddJewellery", "alert('Jewelry has been updated successfully.');", true);
}
else
{
this.lblInfo.Text = flag == 0 ? "Not any rows has been updated." : "An error occured while updating jewelry.";
this.lblInfo.Visible = true;
this.lblInfo.ForeColor = System.Drawing.Color.Red;
}
}

private DataSet GetJewelleryBySerialNumber(string serialNumber)
{
using (SqlConnection con = new SqlConnection(JewelleryHelper.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("spGetJeweleryBySerialNumberForPopup", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = serialNumber;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
return ds;
}
}
}

private int UpdateJewelry()
{
int flag = 0;
using (SqlConnection con = new SqlConnection(JewelleryHelper.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("spUpdateJewelleryBySerialNumber", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = this.txtSerialNumber.Text;
cmd.Parameters.Add("@Category", SqlDbType.VarChar).Value = this.ddlCategory.SelectedValue;
cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = this.txtdesc.Text;
if (!string.IsNullOrEmpty(this.imageProduct.Src))
{
byte[] binaryImage = JewelleryHelper.ConvertImageInBinaryData(S
sudeshna from bangkok 10-Oct-15 2:29am    
private int UpdateJewelry()
{
int flag = 0;
using (SqlConnection con = new SqlConnection(JewelleryHelper.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("spUpdateJewelleryBySerialNumber", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = this.txtSerialNumber.Text;
cmd.Parameters.Add("@Category", SqlDbType.VarChar).Value = this.ddlCategory.SelectedValue;
cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = this.txtdesc.Text;
if (!string.IsNullOrEmpty(this.imageProduct.Src))
{
byte[] binaryImage = JewelleryHelper.ConvertImageInBinaryData(Server.MapPath(this.imageProduct.Src));
if (binaryImage != null)
{
cmd.Parameters.Add("@Image", SqlDbType.Image).Value = binaryImage;
}
}
cmd.Parameters.Add("@CostPrice", SqlDbType.VarChar).Value = JewelleryHelper.GetEncodedCostPrice(this.txtCostPrice.Text);
double costPrice;
if (double.TryParse(this.txtCostPrice.Text, out costPrice))
{
cmd.Parameters.Add("@SellingPrice", SqlDbType.Float).Value = JewelleryHelper.GetSellingPrice(costPrice);
}
if (!string.IsNullOrEmpty(this.txtSoldPrice.Text))
{
cmd.Parameters.Add("@SoldPrice", SqlDbType.Float).Value = double.Parse(this.txtSoldPrice.Text);
}

if (!string.IsNullOrEmpty(this.txtsolddate.Text))
{
cmd.Parameters.Add("@SoldDate", SqlDbType.DateTime).Value = Convert.ToDateTime(this.txtsolddate.Text);
}

cmd.Parameters.Add("@CustomerName", SqlDbType.VarChar).Value = this.txtCustomer.Text;
cmd.Parameters.Add("@AffectedRows", SqlDbType.Int);
cmd.Parameters["@AffectedRows"].Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();

flag = (int)cmd.Parameters["@AffectedRows"].Value;
}
}

return flag;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900