Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my html:-

XML
<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();
}
}
Posted
Comments
Deepak Kanswal Sharma 21-Dec-14 12:20pm    
While doing the above code I am getting an error:-

System.Data.DataRowView' does not contain a property with the name 'RefrenceNo'
syed shanu 21-Dec-14 22:52pm    
In yuour gridview DataKeyNames="RefrenceNo"
Check in your select query "RefrenceNo" is avaibale or not.

1 solution

The problem is due to in your GridView2 you have added the DataKeyNames="RefrenceNo" but in your select query you didnt use the "RefrenceNo" field to select .

in Gridview design change the DataKeyNames or include the "RefrenceNo" in your select query where you bind your Gridview2.

check your code

ASP.NET
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
            DataKeyNames="RefrenceNo" onrowcancelingedit="GridView2_RowCancelingEdit"
            onrowediting="GridView2_RowEditing" onrowupdating="GridView2_RowUpdating">


C#
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();


The above code should be like this

C#
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,RefrenceNo from date1", con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, "date1");
GridView2.DataSource = ds1;
//GridView2.DataBind();
con1.Close();
 
Share this answer
 
v2
Comments
sasanka sekhar panda 22-Dec-14 3:05am    
agreed...one more suggestion you do not need to open and close the db connections when u r using SqlDataAdapter ..
Deepak Kanswal Sharma 23-Dec-14 0:58am    
Thanks brother. It's done.

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