Thursday, 21 February 2013

Delete Insert update image or with out image with the help of grid view

 edit image








Insert value


Delete value in gride view

This is the code which is i use




using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
public partial class BannerManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
               filldata();
              hope();
        }
        Panel1.Visible = false;
        Panel3.Visible = false;
    }
    SqlDataReader dr;
    SqlCommand cmd;
    protected void Button1_Click(object sender, EventArgs e)
    {
        filldata();
        string f = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("images/" + f));
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
        //SqlConnection con = new SqlConnection("Data Source=MNISH-PC\\SQLEXPRESS;Initial Catalog=Rakesh;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("INSERT INTO ImagesPath (Name,ImagePath,Link,Status)VALUES ('" + TextBox2.Text + "','" + "images/" + f + "','" + TextBox3.Text + "', '" + CheckBox1.Checked + "' )", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    public void filldata()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
        //SqlConnection con = new SqlConnection("Data Source=MNISH-PC\\SQLEXPRESS;Initial Catalog=Rakesh;Integrated Security=True");
        con.Open();
        string ret = "select * from ImagesPath";
        cmd = new SqlCommand(ret, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
        TextBox2.Text = "";
        TextBox3.Text = "";
               
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        filldata();
        TextBox2.Text = "";
        TextBox3.Text = "";
        Panel3.Visible = false;
        Panel1.Visible = false;

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //Label t = (Label)GridView1.Rows[e.RowIndex].FindControl("lable1");
        int id = (int)GridView1.DataKeys[e.RowIndex].Value;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
        //SqlConnection con = new SqlConnection("Data Source=MNISH-PC\\SQLEXPRESS;Initial Catalog=Rakesh;Integrated Security=True");
        con.Open();
        string r = "delete from ImagesPath where ID='" + id + "'";
        cmd = new SqlCommand(r, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
       
    }
    public void update()
    {
   
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
       
    }
    public void hope()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());

        SqlCommand cmd = new SqlCommand("Select * from ImagesPath", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataTextField = "ID";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataSource = ds.Tables[0];
        DropDownList1.DataBind();
    }
  
    protected void Button5_Click(object sender, EventArgs e)
    {
        Panel3.Visible = true;
        Panel1.Visible = false;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        filldata();

        if (FileUpload2.HasFile)
        {
            string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
            FileUpload2.SaveAs(Server.MapPath("images/" + f));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
            con.Open();
            string str = "update ImagesPath set ImagePath='" + "images/" + f + "' where ID='" + DropDownList1.SelectedItem.Text + "'";
            cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();

        }
        else
        {
            //string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
            //FileUpload2.SaveAs(Server.MapPath("images/" + f));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
            con.Open();
            string str = "update ImagesPath set Name='" + TextBox4.Text + "',Link='" + TextBox5.Text + "',Status='" + DropDownList2.SelectedItem.Text + "' where ID='" + DropDownList1.SelectedItem.Text + "'";
            cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
  
    protected void Button4_Click2(object sender, EventArgs e)
    {
        Panel3.Visible = true;
    }
   
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Panel1.Visible = true;
        Panel3.Visible = false;
        //hope();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());

        con.Open();
        string str = "select Name,ImagePath,Link,Status from ImagesPath where ID ='" + DropDownList1.SelectedItem.Text + "'";
        cmd = new SqlCommand(str, con);

        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            TextBox4.Text = dr["Name"].ToString();
            Image2.ImageUrl = "~/" + dr["Imagepath"].ToString();
            TextBox5.Text = dr["Link"].ToString();
            //DropDownList2.SelectedValue = dr["Status"].ToString();
           
        }
        con.Close();
    }
}

No comments :

Post a Comment