Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 7th, 2007, 10:44 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rrflore2
Default C# - update list box after write dataset xm

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>
  #2 (permalink)  
Old July 9th, 2007, 08:14 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I think the problem is that you are never reassigning the listbox datasource, you are just rebinding it.

Try changing the protected void LoadCategories() to accept a dataset, then you can call the method from either the LoadCategories method or the update method and pass in the dataset you are working with.

-Peter
  #3 (permalink)  
Old July 9th, 2007, 08:36 AM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rrflore2
Default

That did it. I was close...so simple...I could'nt even see it. I included the code below for anyone else that might need it.

Thanks!!!

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(GetCategoryDs());

        }
 
    }

    protected void LoadCategories(DataSet dsCategory)
    {

        //ds = GetCategoryDs();

        DataView dv = dsCategory.Tables["Category"].DefaultView;
        dv.Sort = "CategoryText ASC";

        lstCategory.DataSource = dsCategory;
        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(ds);
            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(ds);
            lstCategory.DataBind();

        }

    }
}


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab Values From List Box into Text Box phungleon VB How-To 2 June 19th, 2008 10:33 PM
List box bounded with dataset ganeshkuppuswamy General .NET 0 November 23rd, 2007 10:34 AM
multi-column list box values moved to 2nd list box sbmvr Access VBA 1 May 14th, 2007 01:58 PM
Binding a combo box to update a list view Amethyst1984 General .NET 0 February 28th, 2006 03:04 PM
select box/List box alphabetic sort sasidhar79 Javascript How-To 3 November 10th, 2004 03:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.