Community discussion forum
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
Simply the fastest line-level profiler for .NET ever
“The low overhead means it has minimal impact on the execution of my program”
Mark Everest, Development Team Leader, Renault F1 Team Ltd.Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now
-
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

-
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 IfCatch 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
-
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
-
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.
-
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.
-
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 a reply
Quick links
Recent activity
- MA Awan replied to Key_Press() event for text box
- jack semwal replied to i have struck with my proj...
- Mandy Bradley replied to Help please for student
- Barry Roberts replied to Loop help needed
- Navin Hirani replied to Error in VB code
- Marissa Levy replied to Gizmox Announces release of...
Enter your message below
Sign in or Join us (it's free).