Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have five column in which one is image column
me want of save all the column .other column is save but the problem is an image which not save any have any idea or solution plz i need it
Your sinser Mian sahib Jan
Posted
Comments
Schatak 3-Apr-14 4:46am    
inappropriate information. what error you are getting while saving? what datatype you have taken in SQL side?
Mian Sahib Jan 3-Apr-14 4:54am    
giving no error but the field are save empty and sql server i take nvarchar data type.my code are given below
protected void btnAdd_Click(object sender, EventArgs e)
{
filldatatable();
clear();
txtSno.Focus();
}
public void filldatatable()
{
string FileName ;
if (ImageUpload.PostedFile != null)
{
string fileExt =
System.IO.Path.GetExtension(ImageUpload.FileName);
// check image extension and size
if ((fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png") && ImageUpload.PostedFile.ContentLength < 1048576)
{

FileName = Path.GetFileName(ImageUpload.PostedFile.FileName);
//string itemID = string.Empty;
//Save files to Folder
string savelocation = Server.MapPath("~/ImageStorage/");
string fileExtention = System.IO.Path.GetExtension(ImageUpload.FileName);
//creating filename to avoid file name conflicts.
string fileName = Guid.NewGuid().ToString();
//saving file in savedImage folder.
string savePath = savelocation + FileName;
ImageUpload.SaveAs(savePath);

}
}
DataTable dt = new DataTable();

if (!dt.Columns.Contains("ItemSno"))
{
dt.Columns.Add("ItemSno");
}
if (!dt.Columns.Contains("ItemName"))
{
dt.Columns.Add("ItemName");
}
if (!dt.Columns.Contains("ItemQuantity"))
{
dt.Columns.Add("ItemQuantity");
}

if (!dt.Columns.Contains("ItemPrice"))
{
dt.Columns.Add("ItemPrice");
}
if (!dt.Columns.Contains("ItemTotalPrice"))
{
dt.Columns.Add("ItemTotalPrice");
}
if (!dt.Columns.Contains("ImageUpload"))
{
dt.Columns.Add("ImageUpload");
}
//if (!dt.Columns.Contains("id"))
//{
// dt.Columns.Add("id");
//}
if (Session["datatable"] != null)
{
dt = (DataTable)Session["datatable"];
}
DataRow dr = dt.NewRow();

dr[0] = txtSno.Text;

dr[1] = txtItems.Text;

dr[2] = txtQuantity.Text;

dr[3] = txtPrice.Text;
float price;
float quantity;
float totalprice;
price = float.Parse(txtPrice.Text);
quantity = float.Parse(txtQuantity.Text);
totalprice = price * quantity;
txtTotalPrice.Text = totalprice.ToString();
dr[4] = txtTotalPrice.Text;
dr[5] = ImageUpload.FileName.ToString();
//////////////////////////////////////


dt.Rows.Add(dr);

if (Session["datatable"] == null)
{
Session["datatable"] = dt;
}

Repeater1.DataSource = dt;
Repeater1.DataBind();


}
protected void SaveRecords_Click(object sender, EventArgs e)
{
for (int index = 0; index < Repeater1.Items.Count; index++)
{

Label lblSno = Repeater1.Items[index].FindControl("lblSno") as Label;
Label lblItemName = Repeater1.Items[index].FindControl("lblItemName") as Label;
Label lblItemQuantity = Repeater1.Items[index].FindControl("lblItemQuantity") as Label;
Label lblItemPrice = Repeater1.Items[index].FindControl("lblItemPrice") as Label;
Label lblItemTotalPrice = Repeater1.Items[index].FindControl("lblItemTotalPrice") as Label;
//Image ImageUpload = Repeater1.Items[index].FindControl("ImageUpload") as Image;
//hiddenfield itemname = repeater1.items[index].findcontrol("itemname") as hiddenfield;
string FileName = Path.GetFileName(ImageUpload.PostedFile.FileName);
FileName = Guid.
Mian Sahib Jan 3-Apr-14 4:55am    
/////remanig code
////////////
string FileName = Path.GetFileName(ImageUpload.PostedFile.FileName);
FileName = Guid.NewGuid().ToString();

String strConnString = ("Data Source=.;Initial Catalog=PRODUCT;Integrated Security=True");
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "insert into tbl_Product (Sno,ItemName,ItemImage,ItemQuantity,ItemPrice,Total)values('" + lblSno.Text + "','" + lblItemName.Text + "','" + ImageUpload.FileName.ToString() + "','" + lblItemQuantity.Text + "','" + lblItemPrice.Text + "','" + lblItemTotalPrice.Text + "')";
SqlCommand cmd = new SqlCommand(strQuery, con);
con.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Data Successfully Saved');</script>");

}
}

1 solution

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