.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to insert image from selected image in IMAGE CONTROL in asp.net

Posted By: kavitha     Posted Date: November 25, 2010    Points:2   Category :ASP.Net
hi friends,

how to insert image from selected image in image control in asp.net


cmd.CommandText = "insert into erlogo1 values('" + Image1.ImageUrl.ToString() + "')";

thanks,

protected void btnlogo_Click(object sender, EventArgs e)
{
OpenConnection(con);
cmd.Connection = con;
cmd.CommandText = "insert into erlogo1 values('" + txtdate.Text + "','" + txterid.Text + "','" + lblname.Text + "','" + Image1.ImageUrl.ToString() + "')";
Response.Write("<script>alert('Inserted Successfully')</script>");
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("home.aspx");
}

Responses
Author: SureshM             
Posted Date: November 26, 2010     Points: 5   

using your Code, Cannot store the image, only you can store the path of the image.

if u want to store image, you have to create a column with the datatype of "IMAGE" in DB.
its my sample code:

I am using Imagebutton for display image.
1. " alter table products add productImage IMAGE "

Next Convert the image as binary.

FileInfo fInfo = new FileInfo(Server.MapPath(imgbtn.ImageUrl.ToString()));
FileStream fs = new FileStream(Server.MapPath(imgbtn.ImageUrl.ToString()), FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] imgbytes = br.ReadBytes((int)fInfo.Length);

SqlConnection objcon = new SqlConnection();
objcon.ConnectionString = strconn;
objcon.Open();
SqlCommand objcmd = new SqlCommand();
objcmd.Connection = objcon;
objcmd.CommandText = "INSERT INTO Products (ProductName,productImage) Values(@name,@img)";// ('" + "suresh" + "','" + imgbytes + "')";

SqlParameter objpara = new SqlParameter("@img", (object)imgbytes);
objcmd.Parameters.Add(objpara);

SqlParameter objpara1 = new SqlParameter("@name", "Suresh");
objcmd.Parameters.Add(objpara1);

objcmd.ExecuteNonQuery();

This will help you.......


Author: SureshM             
Posted Date: November 26, 2010     Points: 5   

When display images from the database.

you have to use this code:

byte[] imageData = (byte[])dataGridView1.Rows[e.RowIndex].Cells["ProductImage"].Value;

//Initialize image variable
Image newImage;
//Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);

//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true);
}

imgcontrol.Image = newImage;


Author: kavitha             
Posted Date: November 26, 2010     Points: 5   

thanks for your response.

please give in detail.

thanks.

Author: SureshM             
Posted Date: November 26, 2010     Points: 5   

hi,

I am using Image button to display images, when i click on the particular image it will be stored into the DB.
In DB, i have created a column to store image, the datatype is IMAGE.

We Cannot store the image directly into the db. so we have to convert into binary using the below code.

FileInfo fInfo = new FileInfo(Server.MapPath(imgbtn.ImageUrl.ToString()));
FileStream fs = new FileStream(Server.MapPath(imgbtn.ImageUrl.ToString()), FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] imgbytes = br.ReadBytes((int)fInfo.Length);
then you can store the bytes into db.

................
............

SqlParameter objpara = new SqlParameter("@img", (object)imgbytes);
objcmd.Parameters.Add(objpara);
.........

Anoter way.

you can save the selected image into a local folder of your project. then save path into the DB.
this method is commonly used. because when we store images in db. db size will expand.


Author: kavitha             
Posted Date: November 27, 2010     Points: 5   

thanks,

but,

Error in click button


'handler.aspx?sid=2' is not a valid virtual path.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 'handler.aspx?sid=2' is not a valid virtual path.

Author: SureshM             
Posted Date: November 27, 2010     Points: 5   

the image url must be look like this. example

../deleket/sleek-xp-basic/256/Close-2-icon.png.

you have to store the image url not redirectin url.

below is the redirecting url... not the image url
handler.aspx?sid=2

debug your code, what is the value in server.mappath




Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend