Ok. I'm writing and deleting to an xml file using a dataset. I have a function in my codebehind page that binds a listbox to the dataset that performs the writes/deletes. Everything seems to be working fine except, I cannot get the listbox to update after the xml file is written to.
I've tried to rebind the listbox and a bunch of other things; nothing seems to work. I'm sure this is simple fix but, I cannot seem to figure it out.
I have provided some code below.
Thanks in advance!!!
Code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
XmlDataDocument xmlDataDoc = new XmlDataDocument();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Bind lstCategory to dsXml
LoadCategories();
}
}
protected void LoadCategories()
{
ds = GetCategoryDs();
DataView dv = ds.Tables["Category"].DefaultView;
dv.Sort = "CategoryText ASC";
lstCategory.DataSource = ds;
lstCategory.DataMember = "Category";
lstCategory.DataTextField = "CategoryText";
lstCategory.DataValueField = "CategoryValue";
lstCategory.DataBind();
}
protected DataSet GetCategoryDs()
{
DataSet dsCategory = new DataSet();
string filepath = Server.MapPath("xml") + "\\Categories.xml";
dsCategory = xmlDataDoc.DataSet;
dsCategory.ReadXml(filepath);
dsCategory.ReadXmlSchema(filepath);
return dsCategory;
}
protected void btnAddCategory_Click(object sender, EventArgs e)
{
if (txtCategroy.Text != "")
{
ds = GetCategoryDs();
string filepath = Server.MapPath("xml") + "\\Categories.xml";
DataRow dr = ds.Tables["Category"].NewRow();
dr["CategoryText"] = txtCategroy.Text;
dr["CategoryValue"] = txtCategroy.Text;
ds.Tables["Category"].Rows.Add(dr);
ds.WriteXml(filepath, XmlWriteMode.WriteSchema);
////Clear text box and change from xmldatasource to dataset and rebind listbox
txtCategroy.Text = "";
//LoadCategories();
lstCategory.DataBind();
}
}
protected void btnRemoveCategory_Click(object sender, EventArgs e)
{
if (lstCategory.SelectedItem != null)
{
ds = GetCategoryDs();
string filepath = Server.MapPath("xml") + "\\Categories.xml";
DataView dv = ds.Tables["Category"].DefaultView;
dv.Sort = "CategoryText";
int rowindex = dv.Find(lstCategory.SelectedItem.ToString());
if (rowindex == -1)
Response.Redirect("Error.aspx");
else
{
dv.RowFilter = " CategoryText = '" + lstCategory.SelectedItem.ToString() + "'";
dv.Delete(0);
ds.WriteXml(filepath, XmlWriteMode.WriteSchema);
}
//LoadCategories();
lstCategory.DataBind();
}
}
}
XML file snippet:
<?xml version="1.0" standalone="yes"?>
<Categories>
<xs:schema id="Categories" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Categories" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Category">
<xs:complexType>
<xs:sequence>
<xs:element name="CategoryValue" type="xs:string" minOccurs="0" />
<xs:element name="CategoryText" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Category>
<CategoryValue>Case Management</CategoryValue>
<CategoryText>Case Management</CategoryText>
</Category>