0
votes

I have a grid view and i need to update it with RowUpdating event, but after updating the new values did not appear, database update with the old values. here is my code

 protected void gvContactInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvContactInfo.EditIndex = e.NewEditIndex;
        bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
    }
    protected void gvContactInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lbl = ((Label)gvContactInfo.Rows[e.RowIndex].FindControl("lblContactidno"));
        DropDownList ddl = ((DropDownList)gvContactInfo.Rows[e.RowIndex].FindControl("ddlInfoType"));
        TextBox txtinfo = ((TextBox)gvContactInfo.Rows[e.RowIndex].FindControl("txtValueE"));
        TextBox txtext = ((TextBox)gvContactInfo.Rows[e.RowIndex].FindControl("txtExt"));

        string queryContactInfo = "update tblContactInfo set ContactInfoType='"+ddl.SelectedItem.Text+"',ContactInfo='"+txtinfo.Text+"',Ext='"+txtext.Text+"' where ContactID=" + int.Parse(lbl.Text.Trim()) + "";
        Connection = new SqlConnection(ConnString);
        Connection.Open();
        SqlCommand cmd = new SqlCommand(queryContactInfo, Connection);
        cmd.ExecuteNonQuery();
        Connection.Close();
        gvContactInfo.EditIndex = -1;
        bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
    }
    protected void gvContactInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvContactInfo.EditIndex = -1;
        bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
    }

my databinding code is as follows:

public void bindingGVContacts(int contactID)
    {
        int contactID1 = contactID;
        string queryContactInfo = "SELECT * FROM tblContactInfo where ContactID=" +  contactID1 + "";
        Connection = new SqlConnection(ConnString);
        Connection.Open();
        ds = new DataSet();
        DataTable dt = new DataTable();
        ad = new SqlDataAdapter(queryContactInfo, ConnString);
        ad.Fill(ds, "queryContactInfo");
        ad.Fill(dt);
        Connection.Close();
        if (ds.Tables["queryContactInfo"].Rows.Count > 0)
        {
            gvContactInfo.Columns[0].Visible = true;
            gvContactInfo.DataSource = ds.Tables["queryContactInfo"];
            gvContactInfo.DataBind();
            gvContactInfo.Columns[0].Visible = false;

            foreach (GridViewRow grow in gvContactInfo.Rows)
            {
                Label lbl = ((Label)grow.FindControl("lblContactidno"));
                DropDownList ddl = ((DropDownList)grow.FindControl("ddlInfoType"));
                DataRow[] dr = dt.Select("ContactNoID=" + lbl.Text.Trim() + "");
                if (dr.Length != 0)
                {
                    ddl.SelectedItem.Selected = false;
                    if (ddl.Items.FindByText(dr[0]["ContactInfoType"].ToString()) != null)
                        ddl.Items.FindByText(dr[0]["ContactInfoType"].ToString()).Selected = true;
                }
            }
        }
        else
        {
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            gvContactInfo.DataSource = dt;
            gvContactInfo.DataBind();
            gvContactInfo.Rows[0].Visible = false;
        }
    }

here is my aspx code of gridview:

<asp:GridView runat="server" ID="gvContactInfo" ShowHeader="true" ShowHeaderWhenEmpty="true" Enableviewstate="true"
                    AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="gvContactInfo_RowEditing" OnRowUpdating="gvContactInfo_RowUpdating" OnRowCancelingEdit="gvContactInfo_RowCancelingEdit" OnRowCommand="gvContactInfo_RowCommand"
                    CssClass=" CategoriesTable table table-striped table-bordered CategoriesTable1" 
                    onrowdatabound="gvContactInfo_RowDataBound">
                         <Columns>
                         <asp:TemplateField HeaderText="Value" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:Label ID="lblContactidno" runat="server" Text='<%#Eval("ContactNoID")%>' Font-Bold="true"></asp:Label>
                             </ItemTemplate>
                         </asp:TemplateField>

                             <asp:TemplateField HeaderText="INFO Type" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:DropDownList ID="ddlInfoType" runat="server">
                                <asp:ListItem Value="Address" Text="Address"></asp:ListItem>
                                <asp:ListItem Value="Email-Personal" Text="Email-Personal"></asp:ListItem>
                                <asp:ListItem Value="Email-Work" Text="Email-Work"></asp:ListItem>
                                <asp:ListItem Value="Phone-Home" Text="Phone-Home"></asp:ListItem>
                                <asp:ListItem Value="Phone-Work" Text="Phone-Work"></asp:ListItem>
                                <asp:ListItem Value="Phone-Mobile" Text="Phone-Mobile"></asp:ListItem>
                                </asp:DropDownList>
                             </ItemTemplate>
                                 <FooterTemplate>
                                     <asp:DropDownList ID="ddlInfoType" runat="server">
                                        <asp:ListItem Value="Address" Text="Address"></asp:ListItem>
                                        <asp:ListItem Value="Email-Personal" Text="Email-Personal"></asp:ListItem>
                                        <asp:ListItem Value="Email-Work" Text="Email-Work"></asp:ListItem>
                                        <asp:ListItem Value="Phone-Home" Text="Phone-Home"></asp:ListItem>
                                        <asp:ListItem Value="Phone-Work" Text="Phone-Work"></asp:ListItem>
                                        <asp:ListItem Value="Phone-Mobile" Text="Phone-Mobile"></asp:ListItem>
                                     </asp:DropDownList>
                                 </FooterTemplate>
                             </asp:TemplateField>

                             <asp:TemplateField HeaderText="Value" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:TextBox ID="txtValue" runat="server" Text='<%#Eval("ContactInfo")%>'></asp:TextBox>
                             </ItemTemplate>
                                 <FooterTemplate>
                                     <asp:TextBox ID="txtValue" runat="server" Text=""></asp:TextBox>
                                 </FooterTemplate>
                                 <EditItemTemplate>
                                    <asp:TextBox ID="txtValueE" runat="server" Text='<%#Eval("ContactInfo")%>'></asp:TextBox>
                                 </EditItemTemplate>
                             </asp:TemplateField>

                             <asp:TemplateField HeaderText="Extension" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:TextBox ID="txtExtension" runat="server" Text='<%#Eval("Ext")%>'></asp:TextBox>
                             </ItemTemplate>
                                 <FooterTemplate>
                                     <asp:TextBox ID="txtExtension1" runat="server" Text=""></asp:TextBox>
                                 </FooterTemplate>
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtExt" runat="server" Text='<%#Eval("Ext")%>'></asp:TextBox>
                                 </EditItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Action" ItemStyle-Width="5%">
                             <ItemTemplate>
                                <asp:LinkButton runat="server" CommandName="Edit" CausesValidation="false">Edit</asp:LinkButton>
                             </ItemTemplate>
                                 <EditItemTemplate>
                                     <asp:LinkButton runat="server" CommandName="Update" CausesValidation="false">Update</asp:LinkButton>
                                     <asp:LinkButton runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                                 </EditItemTemplate>
                                 <FooterTemplate>
                                     <asp:Button runat="server" Text="ADD" CommandName="Insert"></asp:Button>
                                 </FooterTemplate>
                             </asp:TemplateField>
                        </Columns>
                </asp:GridView>

Please provide me a solution.

4

4 Answers

0
votes

Just check whether you are getting the new values in gvContactInfo_RowUpdating change event using break points

0
votes

Set a debugger and check if you are passing the correct (new) value to your sql query string, and use try-catch block to catch any exception when updating.

Also, it is not a good idea to build sql query using string. You should use Parameter to prevent SQL-Injection.

This example shows what could happen with SQL injection.

And this example shows how you can prevent SQL injection in C# using Parameter

0
votes

Instead of reading the old values directly using the FindControl option like,

Label lbl = ((Label)gvContactInfo.Rows[e.RowIndex].FindControl("lblContactidno"));

you should be using the GridViewUpdateEventArgs.NewValues Property property to get all the new values as key/value pair.

string lblStr = e.NewValues[0].ToString(); //lblContactidno

EDIT

You are reading the existing values of your control using the FindControl method in the Row_Updating event. The problem is that your new values hasn't been updated yet and it's in the process to do so. Hence, it's pulling out the old values. Both old & New values are stored in the event GridViewUpdateEventArgs as key/value pair. So you have to get the new values from there [NewValues property]. The code I have suggested here is to read the value for your label only. check if you're getting the new value for it or not as it's based on the assumption that the label is the first control in the grid.

0
votes

i got another way to update i.e with the help of RowCommand method

 protected void gvContactInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
if (e.CommandName.Equals("Update"))
        {
            GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int RowIndex = gvr.RowIndex;
            Label lbl = ((Label)gvContactInfo.Rows[RowIndex].FindControl("lblContactidno"));
            DropDownList ddl = ((DropDownList)gvContactInfo.Rows[RowIndex].FindControl("ddlInfoType"));
            TextBox txtinfo = ((TextBox)gvContactInfo.Rows[RowIndex].FindControl("txtValueE"));
            TextBox txtext = ((TextBox)gvContactInfo.Rows[RowIndex].FindControl("txtExt"));

            string queryContactInfo = "update tblContactInfo set ContactInfoType='" + ddl.SelectedItem.Text + "',ContactInfo='" + txtinfo.Text + "',Ext='" + txtext.Text + "' where ContactNoID=" + int.Parse(lbl.Text.Trim()) + "";
            Connection = new SqlConnection(ConnString);
            Connection.Open();
            SqlCommand cmd = new SqlCommand(queryContactInfo, Connection);
            cmd.ExecuteNonQuery();
            Connection.Close();
            gvContactInfo.EditIndex = -1;
        }
}