Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
want to update row in gridview.i had written this code.i am updating from ID and id is int type.i think so that is the problem that row is not updating.
please help me with that....

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    GridViewRow row = GridView1.Rows[e.RowIndex];

    TextBox ID = (TextBox)row.FindControl("TextBoxEditID");
    TextBox node = (TextBox)row.FindControl("TextBoxEditNode");
    TextBox title = (TextBox)row.FindControl("TextBoxEdittitle");
    TextBox source = (TextBox)row.FindControl("TextBoxEditsource");
    TextBox scope = (TextBox)row.FindControl("TextBoxEditscope");

    //int IDD = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

    int IDD = Convert.ToInt32(ID.ToString());

    string nod = node.Text;
    string tit = title.Text;
    string sou = source.Text;
    string scpe = scope.Text;

    Update(IDD, nod, tit, sou, scpe);

private void Update(int IDD, string nod, string tit, string sou, string scpe)
        string query = "UPDATE ['2013 Projects$'] SET Node =@nod, Title =@tit, Source =@sou, Scope =@scpe, WHERE ID=@IDD";
        con = new SqlConnection(connstring);
        cmd = new SqlCommand(query, con);

        cmd.Parameters.Add("@nod", SqlDbType.VarChar).Value = nod;
        cmd.Parameters.Add("@tit", SqlDbType.VarChar).Value = tit;
        cmd.Parameters.Add("@sou", SqlDbType.VarChar).Value = sou;
        cmd.Parameters.Add("@scpe", SqlDbType.VarChar).Value = scpe;


        GridView1.EditIndex = -1;
    catch (Exception ex)
        throw ex;

Updated 26-May-13 19:29pm

1 solution

There is a problem in your code.

You have forgot to assign the parameter @IDD.

Please do something like below.
cmd.Parameters.Add("@IDD", SqlDbType.Int).Value = IDD;
Share this answer
fak_farrukh 27-May-13 2:23am    
i had add this same result
Debug the code and at the line con.Open();, just hover on cmd and check what is query. See whether all the values are replaced in parameter or not. Post that query here.

One more thing, post the connection string you are using.
fak_farrukh 27-May-13 2:37am    
i have debug the code but it does not goes to rowupdating event
Can you post the GridView Markup in aspx page here ?
fak_farrukh 27-May-13 2:44am    
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound"
onrowdeleted="GridView1_RowDeleted" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="1009px"
style="margin-left: 0px" BackColor="White" BorderColor="White"
BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
<asp:TemplateField HeaderText="ID">
<asp:TextBox ID="TextBoxEditID" runat="server" Text='<%# Bind("ID") %>' />

<asp:Label ID="LabelID" runat="server" Text='<%# Bind("ID") %>' />

<asp:TextBox ID="TextBoxID" runat="server" />

<asp:TemplateField HeaderText="Node">
<asp:TextBox ID="TextBoxEditNode" runat="server" Text='<%# Bind("Node") %>' />

<asp:Label ID="LabelNode" runat="server" Text='<%# Bind("Node") %>' />

<asp:TextBox ID="TextBoxNode" runat="server" />

<asp:TemplateField HeaderText="Title">
<asp:TextBox ID="TextBoxEdittitle" runat="server" Text='<%# Bind("title") %>' />

<asp:Label ID="Labeltitle" runat="server" Text='<%# Bind("title") %>' />

<asp:TextBox ID="TextBoxrtitle" runat="server" />

<asp:TemplateField HeaderText="Source">
<asp:TextBox ID="TextBoxEditsource" runat="server" Text='<%# Bind("source") %>' />

<asp:Label ID="Labelsource" runat="server" Text='<%# Bind("source") %>' />

<asp:TextBox ID="TextBoxsource" runat="server" />

<asp:TemplateField HeaderText="Scope">
<asp:TextBox ID="TextBoxEditscope" runat="server" Text='<%# Bind("scope") %>' />

<asp:Label ID="Labelscope" runat="server" Text='<%# Bind("scope") %>' />

<asp:TextBox ID="TextBoxscope" runat="server" />

<asp:CommandField ButtonType="Button" ShowEditButton="True" />

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