How To Insert Image Into Database

hi
try for this code: insert image in to database.


.aspx:


<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" Style="left: 380px; position: relative;
top: -401px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

.cs:

page_load()
{
// give sql connections(connection string)
}
protected void Button1_Click(object sender, EventArgs e)
{
int len = FileUpload1.PostedFile.ContentLength;

byte[] pic = new byte[len];
FileUpload1.PostedFile.InputStream.Read(pic, 0, len);
string fup = FileUpload1.PostedFile.FileName;


if (k1 == 0)
{
cmd = new SqlCommand("insert into login_inf(first_name,logo) values (@name,@logo)", conn);


cmd.Parameters.Add("@name", TextBox1.Text.Replace("'", "''"));

cmd.Parameters.Add("@logo", pic);
cmd.ExecuteNonQuery();

}
}


Attachments

  • "Insert Image.aspx" (21412-23041-insertimage.txt)
  • Comments

    Author: n.ravi krishna13 Feb 2009 Member Level: Silver   Points : 0

    good programming.exellent.

    Author: Sasikumar16 Feb 2009 Member Level: Gold   Points : 1

    This was nice Code:

    It works fine for me, i am able to upload an Image

    How can i Retrieve the Image to Front End,through grid view to the image field
    Please Send Me the Code

    Thanks and Regards
    Sasikumar

    Author: skc12 Mar 2009 Member Level: Bronze   Points : 1

    Hi should i need to create at table login_inf
    cmd = new SqlCommand("insert into login_inf(first_name,logo) values (@name,@logo)", conn);

    Author: ankit18 Mar 2009 Member Level: Bronze   Points : 0

    this is a good code for inserting an image.
    Thanks for such a nice code.

    Author: Shuby Arora24 May 2009 Member Level: Gold   Points : 0

    Hi
    THis is a good code

    keep it up!

    Author: Makesh24 Sep 2010 Member Level: Silver   Points : 0

    thank you so much .. very useful ...

    Author: Ajmal Abbasi25 Sep 2010 Member Level: Bronze   Points : 1

    The code to add images in the database is same as above but the efficient approach is to just store the images path in the database instead of storing full images in database. Storing images in database and retrieving images from there slows down the performance.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: