Click here to Skip to main content
15,895,667 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....

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

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

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


}
Posted
Updated 26-May-13 19:29pm
v2

1 solution

There is a problem in your code.

You have forgot to assign the parameter @IDD.

Please do something like below.
C#
cmd.Parameters.Add("@IDD", SqlDbType.Int).Value = IDD;
 
Share this answer
 
Comments
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"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound"
onrowdeleted="GridView1_RowDeleted" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated"
onrowupdating="GridView1_RowUpdating"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="1009px"
style="margin-left: 0px" BackColor="White" BorderColor="White"
BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
GridLines="None">
<columns>
<asp:TemplateField HeaderText="ID">
<edititemtemplate>
<asp:TextBox ID="TextBoxEditID" runat="server" Text='<%# Bind("ID") %>' />

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

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


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

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

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


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

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

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


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

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

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


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

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

<footertemplate>
<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