Gridview Row Deleting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vajrala Narendra
    New Member
    • Jun 2007
    • 73

    Gridview Row Deleting

    Hi, all
    am working on asp.net, c# 2.0
    i want to delete a row from a grid view
    for that i take datakeyname inn gris view i wrote coding as



    Code:
     1. <asp:GridView ID="GridView2" runat="server" Height="72px" Width="776px" AutoGenerateColumns="False" 
       2.                       DataKeyNames="fld_jid" >
       3. ----


    here fld_jid was numericfield which i selected in coding and bind it to gridview

    and in row deleting event i wrote

    Code:
    1. int jid = (int)GridView1.DataKeys[e.RowIndex].Value;
       2.         cn.Open();
       3.         cmd = new SqlCommand("delete from tbl_js_savedjobs where fld_jid=" + jid, cn);
       4.         cmd.ExecuteNonQuery();
       5. cn.close();

    but am getting error as
    Specified cast is not valid.

    is this coding was correct or not
    please suggest me how to write
    its urgent to me

    Thanks inadvance
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Code:
    int jid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[fld_jid]);
    or use the Values index

    Code:
    int jid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    You should also wrap this code in a try catch block in case the conversion to an integer throws an exception.

    Nathan

    Comment

    Working...