Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i click on edit button in gride view task is performe but the actual value is nox show when i refresh the page then after actual value is come

this is the code

HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="ID,Name,ImagePath,Status"

                    Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None"
                    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
                    PageIndex="5" PageSize="4" >
                    <RowStyle BackColor="#EFF3FB" />
                    <Columns>
                        <asp:TemplateField HeaderText="NO">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="NAME">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Label ID="Label2" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="IMAGE">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ImagePath") %>' Width="50px" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="STATUS">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Label ID="Label3" runat="server" Text='<%#Bind("Status") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="EDIT">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:ImageButton ID="ImageButton2" runat="server"  CommandName='edit'
                                    ImageUrl="~/Img/icon-delete.gif" onclick="ImageButton2_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="DELETE">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:ImageButton ID="ImageButton1" runat="server" CommandName='delete' ImageUrl="~/Img/icon-delete.gif" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#990033" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>


C#
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();
    }
   
}
Posted
Updated 21-Feb-13 20:28pm
v2

If You're Inserting Updating or Deleting Data from gridview then call the Fill()<br />
Method on Each Event.<br />
<br />
To Get Updated Data.
 
Share this answer
 
Comments
M@anish 22-Feb-13 2:05am    
thank you so much
Surendra0x2 22-Feb-13 2:19am    
My Pleasure :)
Happy Coding :)
After Editing the Grid View bind the data base once again to grid view ...
C#
SqlCommand comm = new SqlCommand("Select required fields from YourTable", conn);
conn.Open();
SqlDataReader rdr = comm.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
rdr.Close();
 
Share this answer
 
v2
Comments
M@anish 22-Feb-13 1:58am    
i just want when i delete data from grid view data show that time when page is postback but data is not show
Harikamalina 23-Feb-13 6:11am    
just use the updatepanel

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