Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m developing the application in asp.net. And I want to display image in my application when i select any image by browse button. So plz give me complete procedure and code in C#.net to image upload and store it into gridview and sql server 2005
Posted

The image will be store in database and you can bind the database with gridview
to upload an image take FileUpload Control
C#
protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {


            string filename = FileUpload1.FileName;
            string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".JPG" || fileExt == ".GIF" || fileExt == ".png" || fileExt == ".jpeg")
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\ThoughtForDay\\" + filename));
                string path = "~\\ThoughtForDay\\" + filename;
                SqlCommand cmd = new SqlCommand("Insert into ThoughtFortheDay(Imageurl,date1,date2) values('" + path + "',@date1,@date2)", cnn);
                cmd.Parameters.AddWithValue("Imageurl",fileExt);
                cmd.Parameters.AddWithValue("date1", txtdate1.Text);
                cmd.Parameters.AddWithValue("date2", txtdate2.Text);
                cmd.CommandType = CommandType.Text;
                cnn.Open();
                cmd.ExecuteNonQuery();
                lblinfo.Text = "Uploaded Successfully";
                cnn.Close();
                
            }
            else
            {
                lblinfo.Text = "Browse Only jpg,gif,bmp,png,jpeg Image File!";
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    
    }

ThoughtForDay is foledr name for images.
to show image in gridview see
Displaying Images in a GridView
isplaying Images in a GridView
 
Share this answer
 
 
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