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.