how to delete the row in gridview using sqlserver database

Posted by Kishore22 under ASP.NET on 9/19/2013 | Points: 10 | Views : 1894 | Status : [Member] | Replies : 11
SqlCommand cmd1 = new SqlCommand("delete from tab1 where firstname='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'");




Responses

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer "Deleting existing record with Confirmation" under the link http://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
--May be try this in RowDeleting event of GridView

GridViewRow row = GridView1.Rows[e.RowIndex];

Label firstnamelable = (Label)row.FindControl("lblFirstNameValue");
string firstname = firstnamelable.Text;

SqlCommand cmd1 = new SqlCommand("delete from tab1 where firstname='" + firstname + "'");


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Use the following DELETE query
SqlCommand cmd1 = new SqlCommand("delete from tab1 where firstname='" + GridView1.DataKeys[e.RowIndex].Value.ToString()+ "'"); 



Sample example by using DataKeys to delete a row from DB
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)

{
int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from Employee_Details where UserId=" + userid, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Red;
lblresult.Text = username + " details deleted successfully";
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/19/2013 [Member] Starter | Points: 25

Up
0
Down
this are not getting exact output

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Post us back the RowDeleting event code which you have now...

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 9/19/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
protected void ResultGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM Vendor WHERE VendorId='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

}

If this post helps you mark it as answer
Thanks

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
SqlCommand cmd2=new SqlCommand();
cmd2.CommandText = "DELETE FROM tab WHERE id='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'";
con.Open();
cmd2.ExecuteNonQuery();
con.Close();



Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
1) Did you specify DataKey name in the <asp:GridView> declaration?
2) Did you have PostBackUrl for EDIT/DELETE link buttons?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Alternate fix for DataKey outofrange error:
http://stackoverflow.com/questions/5381670/index-was-out-of-range-error-in-gridview
http://www.codeproject.com/Questions/414868/Index-was-out-of-range

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
I think you sorted out the issue...
So mark it as answer

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saanjhi on: 10/2/2013 [Member] Starter | Points: 25

Up
0
Down
Add this coding in source code of your grid view:-
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton CommandArgument='<%#Eval("idno") %>' ID="btn" runat="server" >
Delete
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

int the above code there is an idno there u can repace it with any numeric content of ur grid view. :)):):)


Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response