Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I wants to retrieve images from sql database based on image name and store that images in local folder named as Images. I can retrieve images from sql database but i dont know how to create new folder named as Images and store my extracted image at local folder.

Thanks and Regards,

Dhinesh kumar V
Posted
Comments
ArunRajendra 25-Sep-13 0:56am    
Are you storing image as Blob in the database?
[no name] 25-Sep-13 0:58am    
Post what code you have tried.

Here: http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory.aspx[^].

I don't know how you store the image names though. I hope you have a column in your database to carry this information. Also, you should do something if those names are no unique.

Perhaps your life will be easier if you stop using user-level word "folder", which is nothing but a user-level metaphorе, and embrace the programming-level term "directory".

—SA
 
Share this answer
 
Hi...
!st create a Images folder to ur project. Then
for images are saved in folder abd its path insertered in database.
In aspx:
VB
<asp:FileUpload ID="imgful" runat="server" Height="25px" style="font-weight: 700" />
<asp:button id="btnimg" runat="server" text="SaveProduct" onclick="btnimg_Click" xmlns:asp="#unknown"> ForeColor="DeepPink" style="font-weight: 700" Width="99px" Height="28px"/> </asp:button>

In aspx.cs:
C#
protected void btnimg_Click(object sender, EventArgs e)
 {
   //Get Filename from fileupload control
                string filename = Path.GetFileName(imgful.PostedFile.FileName);
                //Save images into Images folder
                imgful.SaveAs(Server.MapPath("~/Images/" + filename));
                //Open the database connection
                con = new MySqlConnection(cs);
                con.Open();
cmd = new MySqlCommand("Insert into Items(ProductName,ImageUrl) values(@ImageName,@ImagePath)", con);
                        //Passing parameters to query
                        cmd.Parameters.AddWithValue("@ImageName", filename);
                        cmd.Parameters.AddWithValue("@ImagePath", "~/Images/" + filename);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        //Close dbconnection
                        con.Close();
}

for retriving images and am displaying in gridview
In button_click:
C#
con = new MySqlConnection(cs);
            con.Open();
            //Query to select images path and name from database
            cmd = new MySqlCommand("select * from Items", con);
            cmd.ExecuteNonQuery();
            da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();

            gdimg.DataSource = ds;
            gdimg.DataBind();
            gdimg.Visible = true;

May its useful to u. Thank u.
 
Share this answer
 
v2
C#
protected void btn_submit_Click(object sender, EventArgs e)
   {
        if (FileUpload1.HasFile)
       {
           if (CheckExtension(FileUpload1.FileName))
           {
               if (FileUpload1.PostedFile.ContentLength < 10000000)
               {
                   String Path = Page.Server.MapPath("~/uploaded");
                   Path = Path + "\\" + FileUpload1.FileName;
                   FileUpload1.SaveAs(Path);


                   movie_detail objt = new movie_detail();
                   objt.Movie_Name = FileUpload1.FileName;
                   objt.Image_path = Path;
                   objt.Id =Convert.ToInt32(TextBox1.Text);
                   objt.label = txt_vtitle.Text;
                   objt.price = Convert.ToInt32(txt_date.Text);
                   objt.Categories = ddl_category.SelectedItem.Text;
                   obj.movie_details.InsertOnSubmit(objt);
                   obj.SubmitChanges();
                   Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
                   Label1.Text = "movie detail saved succesfully";

               }
               else
               {
                   Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
                   Label1.Text = "file size must be lessthen 10 mb";
               }
           }
           else
           {
               Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
               Label1.Text = "file extension only .jpg is accepted ";
           }
       }
       else
       {
           Label1.Style.Add(HtmlTextWriterStyle.Color, "Red");
           Label1.Text = "plz select a movie image";
       }




       reset();
   }
   public void reset()
   {
       ddl_category.SelectedItem.Text = "";
       txt_vtitle.Text = "";
       TextBox1.Text = "";
       txt_date.Text = "";

   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900