Hi,
I am trying to delete a row in gridview in asp.net using c# language,
but it is deleting entire table.
can any one please tell me the drawback in my code.
I wrote the code like this..
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlCommand cmd = new SqlCommand("delete emp where eno=eno", con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
}
Loading
Dipa AhujaPosted May 27, 2010, 2:04 AM
try this:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView1.DeleteRow((int)GridView1.DataKeys[e.RowIndex].value[0]);
GridView1.DataBind();
}
n i read on net that your getting this index out of range error coz you are dynamically binding your data
Since you are binding your GridView dynamically
then you have to BIND your gridview in PageLoad or PreRender events with !Page.IsPostBack method
ajay rajuPosted May 27, 2010, 1:51 AM
try this.
In source view:
<asp:GridView ID="Gridview1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Eno" HeaderText="Eno" />
<asp:BoundField DataField="Ename" HeaderText="Ename" />
<asp:BoundField DataField="Esal" HeaderText="Esal" />
<asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />
Columns>
asp:GridView>
in rowdeleting event: you take eno datatype as int then write like this:
int eno = int.Parse(gridview.Rows[e.RowIndex].Cells[0].Text); // here cells[0] is eno column. if in your project eno column is 2nd column then write cells[1]
String str = "DELETE FROM emp WHERE eno=" + eno ;
SqlCommand cmd = New SqlCommand(str, cn);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
BindGrid();
if u take eno datatype is varchar then that time write like this:
String eno = Gridview1.Rows[e.RowIndex].Cells[0].Text.ToString();
String str = "DELETE FROM emp WHERE eno='" + eno + "'" ;
SqlCommand cmd = New SqlCommand(str, cn);
cn.Open();
Int i = cmd.ExecuteNonQuery();
cn.Close();
BindGrid();
finally write bindgrid method:
private void bindgrid()
{
SqlDataAdapter da = new SqlDataAdapter("select* from emp", con);
DataSet ds = new DataSet();
da.Fill(ds, "empdetails");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
it may helps you, if it helps you please check mark as accept answer.
Thanks.
srikanth nalluriPosted May 26, 2010, 10:09 AM
i am sorry,based on your suggestion i modify my code but it is displaying an error like
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
i wrote the total code like this..
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("server=.;user id=sa;password=sri;database=nalluri");
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select* from emp", con);
DataSet ds = new DataSet();
da.Fill(ds, "empdetails");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlCommand cmd = new SqlCommand("delete emp where eno='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'",con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
}
}
is it right or not?
please tell me where i did the mistake.
Thanks,
Srikanth
ajay rajuPosted May 26, 2010, 9:13 AM
"delete emp where eno=eno". // this statement deletes the total table.
so you write like this
"delete emp where eno='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'";
it may solve your problem.
if it helps you please mark as accept answer.
Thanks.