In this code everything works fine without any errors, when i edit the row it goes well, but when i change the text in the textbox and press update it still shows the same in gridview row. it doesn't update the latest modified text into GridView. Can u please help me...i was struck here from many days.
Here is my code...
Design:
Expand|Select|Wrap|Line Numbers
- <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" AutoGenerateColumns="false" OnRowCommand="Gridview1_RowCommand"
- OnPageIndexChanging="GridView1_PageIndexChanging" Width="933px" HorizontalAlign="Center" OnRowEditing="GridView1_RowEditing"
- OnRowCancelingEdit="GridView1_RowCanceling" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="id" >
- <HeaderStyle Font-Names="Arial" Width="100px" BackColor="ForestGreen" ForeColor="Black" />
- <PagerSettings FirstPageText="last" NextPageText="first" />
- <Columns>
- <asp:BoundField HeaderText="Date" DataField="Date" >
- <ItemStyle Width="150px" />
- </asp:BoundField>
- <asp:TemplateField HeaderText="Update" >
- <ItemTemplate>
- <asp:Label ID="Updatelbl" runat="server" Text='<%#Eval("Update")%>'> </asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="UpdateTxt" runat="server" TextMode="MultiLine" ReadOnly="false" Text='<%#Eval("Update")%>' Height="25" Width="650"></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Action">
- <ItemTemplate>
- <asp:LinkButton ID="Editbtn" Text="Edit" runat="server" CommandName="Edit"/>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:LinkButton ID="Updatebtn" Text="Update" runat="Server" CommandName="Update" />
- <asp:LinkButton ID="Cancelbtn" Text="Cancel" runat="server" CommandName="Cancel" />
- </EditItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- showupdates();
- GridView1.DataBind();
- }
- protected void Button1_Click1(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- if (TextBox1.Text != "")
- {
- string xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\XMLfile.xml";
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(xmlFilePath);
- XmlElement newtasks = xmlDoc.CreateElement("Tasks");
- XmlElement newupdate = xmlDoc.CreateElement("Update");
- XmlElement newid = xmlDoc.CreateElement("id");
- XmlNodeList List = xmlDoc.SelectNodes("/Updations/Tasks/id");
- int nid = List.Count;
- if (nid != 0)
- nid++;
- else
- nid = 1;
- newid.InnerText = nid.ToString();
- newtasks.AppendChild(newid);
- XmlElement date = xmlDoc.CreateElement("Date");
- date.InnerText = DateTime.Now.ToString("dd-MMM-yyyy");
- newupdate.InnerText = TextBox1.Text;
- newtasks.AppendChild(date);
- newtasks.AppendChild(newupdate);
- xmlDoc.Save(xmlFilePath);
- xmlDoc.DocumentElement.InsertAfter(newtasks, xmlDoc.DocumentElement.LastChild);
- FileStream fsxml = new FileStream(xmlFilePath, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
- xmlDoc.Save(fsxml);
- xmlDoc = null;
- fsxml.Flush();
- fsxml.Close();
- fsxml.Dispose();
- showupdates();
- TextBox1.Text = "";
- }
- }
- }
- protected void showupdates()
- {
- DataSet ds = new DataSet();
- ds.ReadXml(System.AppDomain.CurrentDomain.BaseDirectory + "\\XMLfile.xml");
- GridView1.DataSource = ds.Tables[0];
- GridView1.DataBind();
- }
- protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- GridView1.PageIndex = e.NewPageIndex;
- GridView1.DataBind();
- }
- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
- {
- GridView1.EditIndex = e.NewEditIndex;
- GridView1.DataBind();
- }
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- int i = e.RowIndex;
- DataSet ds = new DataSet();
- ds.ReadXml(System.AppDomain.CurrentDomain.BaseDirectory + "\\XMLfile.xml");
- ds.Tables[0].Rows[e.RowIndex][2] = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("UpdateTxt")).Text;
- GridView1.EditIndex = -1;
- ds.WriteXml(System.AppDomain.CurrentDomain.BaseDirectory + "\\XMLfile.xml");
- showupdates();
- }