We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

Last post 05-22-2008 10:21 AM by mukunda. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 05-12-2008 7:13 AM

    • swathi.ch
    • Not Ranked
    • Joined on 05-08-2008
    • India
    • Junior Member
    • Points 190

    How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    Hi,

       i hv empid,empname,empphoto in my Form...So, i want to  get these details with empphoto....

    So, how can i load empphoto in DB and how to retrive it into PictureBox... i hv one PictureBox for this empphoto...and my DB is SQLSERVER2005

    i want it in C# windows appln...

    Plzzzz can anybody help me for doing this....plzzzzz

     Advance Thnxx

     

    • Post Points: 15
  • 05-13-2008 11:25 AM In reply to

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    hi Swathi

    First Create a folder with the name Photos In your application .

    Soluton explorer--> right click--> create new folder(with the name photos).

    After that in your code write the bellow code:

    1. protected sub Button1_Click(Byval sender as Object,Byval e As EventArgs)
    2.     Try
    3.         If FileUpload1.HasFile Then
    4.             Dim str As String = FileUpload1.FileName
    5.             If chechkfile(str) Then
    6.                 Dim path As String = "~/photos/" + FileUpload1.FileName
    7.                 FileUpload1.SaveAs(MapPath(path))
    8.                 con.Open()
    9.                 Dim cmd As New SqlCommand("insert into images values('" + TextBox1.Text + "','" + path + "')", con)
    10.                 cmd.ExecuteNonQuery()
    11.                    
    12.                 con.Close()
    13.                
    14.             End If
    15.         End If
    16.     Catch exp As Exception
    17.         Response.Write(exp.ToString())
    18.     End Try
    19. End Sub

     it is for insert images into the database.

    This bellow code for retrive the images from the database

    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    2.     Dim image As String = Request.QueryString("images").ToString()
    3.     Dim da As New SqlDataAdapter("select Images from images where images='" + image + "'", con)
    4.     Dim ds As New DataSet()
    5.     con.Open()
    6.     da.Fill(ds, "images")
    7.     If ds.Tables("images").Rows.Count > 0 Then
    8.         Image1.ImageUrl = ds.Tables("images").Rows(0)(0).ToString()
    9.     End If
    10.     con.Close()
    11.    
    12. End Sub

     

    ok bye Smiley Face

     

     

    • Post Points: 10
  • 05-13-2008 11:35 AM In reply to

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    hi Swathi First Create a folder with the name Photos In your application . Soluton explorer--> right click--> create new folder(with the name photos). After that in your code write the bellow code: protected sub Button1_Click(Byval sender as Object,Byval e As EventArgs) Try If FileUpload1.HasFile Then Dim str As String = FileUpload1.FileName If chechkfile(str) Then Dim path As String = "~/photos/" + FileUpload1.FileName FileUpload1.SaveAs(MapPath(path)) con.Open() Dim cmd As New SqlCommand("insert into images values('" + TextBox1.Text + "','" + path + "')", con) cmd.ExecuteNonQuery() con.Close() End If End If Catch exp As Exception Response.Write(exp.ToString()) End Try End Sub Private Function chechkfile(ByVal str1 As String) As Boolean Dim ope As String = Path.GetExtension(str1) Select Case ope.ToLower() Case ".jpg" Return True Case ".jpeg" Return True Case ".gif" Return True Case ".bmp" Return True Case Else Return False End Select End function it is for insert images into the database. This bellow code for retrive the images from the database Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim da As New SqlDataAdapter("select Images from images", con) Dim ds As New DataSet() con.Open() da.Fill(ds, "images") If ds.Tables("images").Rows.Count > 0 Then Image1.ImageUrl = ds.Tables("images").Rows(0)(0).ToString() End If con.Close() End Sub ok bye
    • Post Points: 5
  • 05-13-2008 12:37 PM In reply to

    • swathi.ch
    • Not Ranked
    • Joined on 05-08-2008
    • India
    • Junior Member
    • Points 190

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    Hi Radha,

              thnx 4 ur reply....can u plzzz help me again. plzzz i want it in C# windows appln... and my DB is SQLSERVER2005.......

    i didn't get File upload in c#...

    So, plzz help me.........

     Thnqq

    • Post Points: 10
  • 05-16-2008 8:55 PM In reply to

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    The best solution to this task is that you use Serialization. Implementing a serialization process to the image, the image is "transformed" into an array of bytes... .that you can save without problem into a image or binary field in your SQL's datatable. To Get the image from the database.. only do the inverse process: Deserialization. Check for this process into the MSDN help for more details. Regards
    • Post Points: 5
  • 05-21-2008 7:58 PM In reply to

    • bhushanvi
    • Not Ranked
    • Joined on 05-20-2008
    • United States
    • New Member
    • Points 40

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    Hi. Here is the code posted by RadhaDotNet in C#

    protected void Button1_Click(object sender, EventArgs e)
    {
        try {
            if (FileUpload1.HasFile) {
                string str = FileUpload1.FileName;
                if (chechkfile(str)) {
                    string path = "~/photos/" + FileUpload1.FileName;
                    FileUpload1.SaveAs(MapPath(path));
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into images values('" + TextBox1.Text + "','" + path + "')", con);
                    cmd.ExecuteNonQuery();
                   
                    con.Close();
                   
                }
            }
        }
        catch (Exception exp) {
            Response.Write(exp.ToString());
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string image = Request.QueryString("images").ToString();
        SqlDataAdapter da = new SqlDataAdapter("select Images from images where images='" + image + "'", con);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds, "images");
        if (ds.Tables("images").Rows.Count > 0) {
            Image1.ImageUrl = ds.Tables("images").Rows(0)(0).ToString();
        }
        con.Close();
       
    }

    All you needed to do was copy 'N' paste it in VB to C# convertor and get the output and copy 'N' paste it back in your code editor!

    Regards,

    Bhushan.

     

      

    • Post Points: 5
  • 05-21-2008 8:09 PM In reply to

    • bhushanvi
    • Not Ranked
    • Joined on 05-20-2008
    • United States
    • New Member
    • Points 40

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    Hi. I did not see the code carefully and saw your need to need save and retrieve it to and from SQL Server DB respectively. In that case, you need to  use FileUpload control only to select and store the image to the database. However, to retrieve it from database, and again display it into an image control is a question. I will get back on that. If I find any resource.

    Regards,

    Bhushan.

     

    • Post Points: 10
  • 05-22-2008 7:22 AM In reply to

    • swathi.ch
    • Not Ranked
    • Joined on 05-08-2008
    • India
    • Junior Member
    • Points 190

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    hi Bhushan,

                 thnxx 4 ur reply, plzzz help me on this....i didn't get yet...Actually my requirement is.....

    i designed Passport_Details of the Emp in a From with EmpPhoto....so, when i run it i want to get details with Picture.

    So, i designed Details for one table and EmpID,EmpPhoto for another table.......So, i want to get empphot vth particular ID.....plzz help me............

     
    Advance Thnx....
     

    • Post Points: 10
  • 05-22-2008 10:21 AM In reply to

    • mukunda
    • Not Ranked
    • Joined on 02-13-2008
    • India
    • New Member
    • Points 55

    Re: How to store Image into Databse and how to retrive it from Database into PIctureBox....Plzz help me..

    Hi

    I think you know how to  save the Image in folder and use the same image name to save the ImagePath in the database with "~" appended to it. see the fallowing code.

    string imagepath= "~/"+txtEmpName.Text+".jpg";//Here i am saving the image with same name as Emapname with extenction as jpg.

    File1.PostedFile.SaveAs(@MapPath(filepath));

    imagepath=imagepath.Replace("\\",@"\"); 

    "insert into emp_dtl values('" + txtEmpName.Text + "','" + imagepath + "')";//Execute this query

    Getting the Details:

    string sqlquery="select empname,emp_imagepath from emp_dtl where empid=1";

    SqlConnection con = new SqlConnection(sqlcon);DataSet ds = new DataSet();

    con.Open();

    SqlDataAdapter da =new SqlDataAdapter(sqlquery,con);

    da.Fill(ds);

    con.Close();

    if(ds.Tables[0].Rows.Count>0 )

    {

    txtEmpName.Text = ds.Tables[0].Rows[0][0].ToString();

    Image1.ImageUrl = ds.Tables[0].Rows[0][1].ToString().Replace(
    "\\",@"\");

    }

    I think this helps to achieve your requirement.

     

    Thanks

    Mukunda

     

    • Post Points: 5
Page 1 of 1 (9 items)