We're building a brand new version of the site, and we'd love to hear your ideas
Members
Technology Zones
IBM Learning Center
Articles
Hosted By
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.
-
05-12-2008 7:13 AM
|
|
-
swathi.ch


- Joined on 05-08-2008
- India

- 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
|
|
Advertisement
|
|
-
-
radhadotnet


- Joined on 05-13-2008
- India

- Points 15
|
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
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 image As String = Request.QueryString("images").ToString()
- Dim da As New SqlDataAdapter("select Images from images where images='" + image + "'", 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 
|
|
-
-
-
swathi.ch


- Joined on 05-08-2008
- India

- 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
|
|
-
-
-
bhushanvi


- Joined on 05-20-2008
- United States

- 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.
|
|
-
-
bhushanvi


- Joined on 05-20-2008
- United States

- 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.
|
|
-
-
swathi.ch


- Joined on 05-08-2008
- India

- 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....
|
|
-
-
mukunda


- Joined on 02-13-2008
- India

- 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
|
|
Page 1 of 1 (9 items)
|
Search
Code Samples
New Members
|