simple file upload

Discussion in 'ASP.NET / ASP.NET Core' started by npkarthi, Apr 8, 2011.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. npkarthi

    npkarthi Guest

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
    //Open the database connection
    con.Open();
    OleDbCommand cmd = new OleDbCommand("select * from imagespath", con);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    gvImages.DataSource = ds;
    gvImages.DataBind();
    }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
    //Get Filename from fileupload control
    string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
    //Save images into Images folder
    fileuploadimages.SaveAs(Server.MapPath("Images/"+filename));
    //Getting dbconnection from web.config connectionstring
    OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
    //Open the database connection
    con.Open();
    //Query to insert images path and name into database
    OleDbCommand cmd = new OleDbCommand("Insert into ImagesPath(ImageName,ImagePath) values('" + filename + "','Images/" + filename + "')", con);
    //Passing parameters to query
    //cmd.Parameters.AddWithValue("@ImageName", filename);
    //cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
    cmd.ExecuteNonQuery();
    //Close dbconnection
    con.Close();
    Response.Redirect("~/Default.aspx");
    }
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page