This is my html:-
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="RefrenceNo" onrowcancelingedit="GridView2_RowCancelingEdit"
onrowediting="GridView2_RowEditing" onrowupdating="GridView2_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Pics">
<ItemTemplate>
<img src='Img/<%#Eval("image") %>' width="80px" height="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("detail") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="text" runat="server" Text='<%#Eval("detail") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
And this is my code:-
public partial class editgall : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack != true)
{
bindgrid();
}
}
public void bindgrid()
{
GridView2.Visible = false;
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("Select distinct ddate from date1", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "date1");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
string str1 = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter("Select image,detail from date1", con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, "date1");
GridView2.DataSource = ds1;
//GridView2.DataBind();
con1.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlCommand cmd = new SqlCommand("Delete from date1 where ddate='" + s + "'",con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Deleted Sucessfully");
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("Select * from date1", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "date1");
GridView2.EditIndex = e.NewEditIndex;
GridView2.DataSource = ds;
GridView2.DataBind();
con.Close();
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("Select * from date1", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "date1");
GridView2.EditIndex = -1;
GridView2.DataSource = ds;
GridView2.DataBind();
con.Close();
}
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
TextBox s = (TextBox)(GridView2.Rows[e.RowIndex].FindControl("text"));
string id = GridView2.DataKeys[e.RowIndex].Value.ToString();
Here I want to UPDATE my data:-
// Response.Write("update date1 set detail='" + s.Text + "'where RefrenceNo='" + id + "'");
//SqlCommand cmd = new SqlCommand("update date1 set detail='" + s + "'", con);
//cmd.ExecuteNonQuery();
con.Close();
bindgrid();
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
GridView2.Visible = true;
LinkButton lnk = sender as LinkButton;
TextBox1.Text = lnk.Text;
string str1 = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter("Select * from date1 where ddate='"+TextBox1.Text+"'", con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, "date1");
GridView2.DataSource = ds1;
GridView2.DataBind();
//LinkButton lnk = sender as LinkButton;
//TextBox1.Text = lnk.Text;
con1.Close();
}
}