Search This Blog

Thursday, December 15, 2011

asp.net image upload and show in the gridview


code: aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Product_upload : System.Web.UI.Page
{
    static string path;
    SqlConnection con = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string name = FileUpload1.FileName;
            path = "~//images//" + FileUpload1.FileName;
            FileUpload1.SaveAs(Server.MapPath(path));
            Image1.ImageUrl = path;
            lblname.Text = txt_name.Text;
            lblmsg.Text = "File is successfully uploaded";
        }
        else
        {
            lblmsg.Text = "Please Browse the Image first";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into tbl_image values(@name,@image)",con);
        cmd.Parameters.AddWithValue("@name",txt_name.Text);
        cmd.Parameters.AddWithValue("@image",path);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        fill_data();
    }
    public void fill_data()
    {
        SqlDataAdapter sda = new SqlDataAdapter("select * from tbl_image",con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();           
    }

    protected void ImageButton1_Command(object sender, CommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        Response.Redirect("Product_information.aspx?id="+id);
    }
}





No comments:

Post a Comment