473,503 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# ASP.NET App: update listbox after write delete using dataset

3 New Member
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!!!

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Web.UI.HtmlControls;
  13. using System.Xml;
  14.  
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17.  
  18.     DataSet ds = new DataSet();
  19.     XmlDataDocument xmlDataDoc = new XmlDataDocument();
  20.  
  21.     protected void Page_Load(object sender, EventArgs e)
  22.     {
  23.          if (!Page.IsPostBack)
  24.         {
  25.  
  26.             //Bind lstCategory to dsXml
  27.             LoadCategories();
  28.  
  29.         }
  30.  
  31.      }
  32.  
  33.  
  34.      protected void LoadCategories()
  35.     {
  36.  
  37.         ds = GetCategoryDs();
  38.  
  39.         DataView dv = ds.Tables["Category"].DefaultView;
  40.         dv.Sort = "CategoryText ASC";
  41.  
  42.         lstCategory.DataSource = ds;
  43.         lstCategory.DataMember = "Category";
  44.         lstCategory.DataTextField = "CategoryText";
  45.         lstCategory.DataValueField = "CategoryValue";
  46.         lstCategory.DataBind();
  47.  
  48.     }
  49.  
  50.     protected DataSet GetCategoryDs()
  51.     {
  52.  
  53.         DataSet dsCategory = new DataSet();
  54.         string filepath = Server.MapPath("xml") + "\\Categories.xml"; 
  55.  
  56.         dsCategory = xmlDataDoc.DataSet;
  57.         dsCategory.ReadXml(filepath);
  58.         dsCategory.ReadXmlSchema(filepath);
  59.  
  60.         return dsCategory;
  61.  
  62.     }
  63.  
  64.         protected void btnAddCategory_Click(object sender, EventArgs e)
  65.     {
  66.         if (txtCategroy.Text != "")
  67.         {
  68.  
  69.             ds = GetCategoryDs();
  70.             string filepath = Server.MapPath("xml") + "\\Categories.xml";
  71.  
  72.             DataRow dr = ds.Tables["Category"].NewRow();
  73.             dr["CategoryText"] = txtCategroy.Text;
  74.             dr["CategoryValue"] = txtCategroy.Text;
  75.  
  76.             ds.Tables["Category"].Rows.Add(dr);
  77.  
  78.             ds.WriteXml(filepath, XmlWriteMode.WriteSchema);
  79.  
  80.             ////Clear text box and change from xmldatasource to dataset and rebind listbox
  81.             txtCategroy.Text = "";
  82.             //LoadCategories();
  83.             lstCategory.DataBind();
  84.  
  85.         }
  86.  
  87.     }
  88.     protected void btnRemoveCategory_Click(object sender, EventArgs e)
  89.     {
  90.  
  91.         if (lstCategory.SelectedItem != null)
  92.         {
  93.  
  94.             ds = GetCategoryDs();
  95.             string filepath = Server.MapPath("xml") + "\\Categories.xml";
  96.  
  97.             DataView dv = ds.Tables["Category"].DefaultView;
  98.             dv.Sort = "CategoryText";
  99.  
  100.             int rowindex = dv.Find(lstCategory.SelectedItem.ToString());
  101.  
  102.             if (rowindex == -1)
  103.                 Response.Redirect("Error.aspx");
  104.  
  105.             else
  106.             {
  107.  
  108.                 dv.RowFilter = " CategoryText = '" + lstCategory.SelectedItem.ToString() + "'";
  109.                 dv.Delete(0);
  110.                 ds.WriteXml(filepath, XmlWriteMode.WriteSchema);
  111.  
  112.             }
  113.  
  114.             //LoadCategories();
  115.             lstCategory.DataBind();
  116.  
  117.         }
  118.  
  119.     }
  120. }
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>
Jul 7 '07 #1
2 3820
rrflore2
3 New Member
Can anyone suggest a forum where I could get some help with this issue?
Jul 8 '07 #2
rrflore2
3 New Member
I found some help for this problem! The problem was that I was not reassigning the listbox datasource. I change the protected void LoadCategories() to accept a dataset from the add and remove category functions. See code below.



Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Web.UI.HtmlControls;
  13. using System.Xml;
  14.  
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17.     DataSet ds = new DataSet();
  18.     XmlDataDocument xmlDataDoc = new XmlDataDocument();
  19.  
  20.  
  21.     protected void Page_Load(object sender, EventArgs e)
  22.     {
  23.  
  24.         if (!Page.IsPostBack)
  25.         {
  26.             //Bind lstCategory to dsXml
  27.             LoadCategories(GetCategoryDs());
  28.  
  29.         }
  30.  
  31.     }
  32.  
  33.     protected void LoadCategories(DataSet dsCategory)
  34.     {
  35.  
  36.         //ds = GetCategoryDs();
  37.  
  38.         DataView dv = dsCategory.Tables["Category"].DefaultView;
  39.         dv.Sort = "CategoryText ASC";
  40.  
  41.         lstCategory.DataSource = dsCategory;
  42.         lstCategory.DataMember = "Category";
  43.         lstCategory.DataTextField = "CategoryText";
  44.         lstCategory.DataValueField = "CategoryValue";
  45.         lstCategory.DataBind();
  46.  
  47.     }
  48.     protected DataSet GetCategoryDs()
  49.     {
  50.         DataSet dsCategory = new DataSet();
  51.         string filepath = Server.MapPath("xml") + "\\Categories.xml"; 
  52.  
  53.         dsCategory = xmlDataDoc.DataSet;
  54.         dsCategory.ReadXml(filepath);
  55.         dsCategory.ReadXmlSchema(filepath);
  56.  
  57.         return dsCategory;
  58.  
  59.     }
  60.  
  61.     protected void btnAddCategory_Click(object sender, EventArgs e)
  62.     {
  63.         if (txtCategroy.Text != "")
  64.         {
  65.  
  66.             ds = GetCategoryDs();
  67.             string filepath = Server.MapPath("xml") + "\\Categories.xml";
  68.  
  69.             DataRow dr = ds.Tables["Category"].NewRow();
  70.             dr["CategoryText"] = txtCategroy.Text;
  71.             dr["CategoryValue"] = txtCategroy.Text;
  72.  
  73.             ds.Tables["Category"].Rows.Add(dr);
  74.  
  75.             ds.WriteXml(filepath, XmlWriteMode.WriteSchema);
  76.  
  77.             //Clear text box and change from xmldatasource to dataset and rebind listbox
  78.             txtCategroy.Text = "";
  79.             LoadCategories(ds);
  80.             lstCategory.DataBind();
  81.  
  82.         }
  83.  
  84.     }
  85.     protected void btnRemoveCategory_Click(object sender, EventArgs e)
  86.     {
  87.  
  88.         if (lstCategory.SelectedItem != null)
  89.         {
  90.  
  91.             ds = GetCategoryDs();
  92.             string filepath = Server.MapPath("xml") + "\\Categories.xml";
  93.  
  94.             DataView dv = ds.Tables["Category"].DefaultView;
  95.             dv.Sort = "CategoryText";
  96.  
  97.             int rowindex = dv.Find(lstCategory.SelectedItem.ToString());
  98.  
  99.             if (rowindex == -1)
  100.                 Response.Redirect("Error.aspx");
  101.  
  102.             else
  103.             {
  104.  
  105.                 dv.RowFilter = " CategoryText = '" + lstCategory.SelectedItem.ToString() + "'";
  106.                 dv.Delete(0);
  107.                 ds.WriteXml(filepath, XmlWriteMode.WriteSchema);
  108.  
  109.             }
  110.  
  111.             LoadCategories(ds);
  112.             lstCategory.DataBind();
  113.  
  114.         }
  115.  
  116.     }
  117. }
Jul 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2105
by: mathieu cupryk | last post by:
I have problems with listboxes in the webform2.cs, the textboxes are working well when I do a click on next. I am missing something. It works with the textboxes. Here is the file: using System;...
4
3795
by: Oscar Thornell | last post by:
Hi, I have a relativley large/complex typed dataset that contains 7-8 tables and some supporting relational tables (lookups) for many-to-many relations. A good exampel would be a dataset that...
3
3279
by: Reney | last post by:
I haven't used the VS Studio's drag&drop method to create my connection, adapters and datasets, but I typed them all. The situation is like this: There is a listbox with members pulled from the...
0
4909
by: Vijay Balki | last post by:
I am fetching data in DataSet - myDataSet, from a remote database using a Web Service in my VB.NET client..Once I fetch it I store the data in XML file (myXMLFile) using the WriteXML method of the...
5
25442
by: tony010409020622 | last post by:
I just spent 4 months taking a dotnet class where i learned very little. One of the things I did not learn is this: What are the dotnet equivilents of commands such as: Adodc1.Recordset.AddNew...
1
2523
by: erin.sebastian | last post by:
Hello Everyone, I have created a small application in vb.net to maintain items in a database the problem i am having is that once i delete/add/edit an individual item the changes don't reflect in...
4
2877
by: WB | last post by:
I'm just starting out with C# after many years with VB 6.0. I'm trying to fill a listbox with a list of computer names (almost 100) from app.config. First, is app.config the place to store and...
8
1610
by: Ornette | last post by:
Hello, I have a stored procedure which generates some values in the table. When I use update() how to populate the dataset with theses values ? For the moment I use output parameter but it...
6
2019
by: Tark Siala | last post by:
hi i'm using VS.NET 2005 +SP1 C#, and i connect access database by ADO.NET 2.0. i'm using Dataset to (insert,update,delete) and in get data i use (Datareader), but my problem when i do this: 1 -...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.