473,657 Members | 2,505 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="u rn:schemas-microsoft-com:xml-msdata">
<xs:element name="Categorie s" msdata:IsDataSe t="true" msdata:UseCurre ntLocale="true" msdata:EnforceC onstraints="Fal se">
<xs:complexType >
<xs:choice minOccurs="0" maxOccurs="unbo unded">
<xs:element name="Category" >
<xs:complexType >
<xs:sequence>
<xs:element name="CategoryV alue" type="xs:string " minOccurs="0" />
<xs:element name="CategoryT ext" 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>C ase Management</CategoryText>
</Category>
Jul 7 '07 #1
2 3828
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
2114
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; using System.Collections; using System.Collections.Specialized; using System.ComponentModel; using System.Data; using System.Drawing;
4
3802
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 contained the whole Northwind database with all tables and relations. In my business logic I will retrive a populated instance of the dataset and perform some operations (adding/deleting/updating of rows in various tables...).
3
3284
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 database.(I am using an access db). The user selects one value from the listbox and clicks on one of two buttons "ThisWeek" or "LastWeek". The button click events each has its own SQL statement, pulling datarows from two different tables in the...
0
4912
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 myDataSet. The connection to the Web Service is closed at this point. I update this XML file in my application, and when I am ready to send the data back, I load the XML file back to a DataSet - mySendDataSet, using ReadXML method. Now I...
5
25451
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 Adodc1.Recordset.Update Adodc1.Recordset.MoveFirst Adodc1.Recordset.MoveNext Adodc1.Recordset.Delete
1
2549
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 the listbox that displays all records. In my form load i have Dim cn As OleDbConnection Dim cmd As OleDbCommand
4
2894
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 retrieve this large list? If so, I'm looking for code to obtain the list of computer names from app.config, and also an example of how the computer names need to be listed in app.config.
8
1622
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 just works for 1 row and as the dataset doesn't have the value I should put it after and the rowstate goes to "modified"... Any ideas ?
6
2023
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 - insert data (by dataset). 2- get data (by datareader). when i run this commands i can't get latest data from database, i get data before insert!!! but after few second i can get real data after insert, is this problem from Dataset or Datareader...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1953
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.