Solved

Delete record only in dataset

Posted on 2004-08-13
7
258 Views
Last Modified: 2010-04-07
I have datagrid with XML file. I want to delete a record but not save back the XML file, I want to delete records only in dataset . I want to let the user keep deleting files without really touching the database and at end the user should hit a button and the modified table should be saved  as XML data file. Here is my codes for better clarity. I have comment the part where i save XML file. This code works , the only problem is that my dataset clears after pressing "delete", that is why in my doDelete function i have to open the XML file and then i can delete. So basically i am doing an extra connection to the XML file.I would like to re-use the same dataset over and over until the user hit submit button. How would i do this? Here are my codes. Maybe you will see somethin which i am not doing right.
Thanks,

protected System.Web.UI.WebControls.DataGrid XmlDataGrid;
            protected System.Web.UI.WebControls.DataGrid mygrid;
            protected System.Web.UI.HtmlControls.HtmlForm Form1;
            DataSet ds = new DataSet();

            private DataSet CreateDataSource()
            {
                  XmlDataDocument mydata=new XmlDataDocument();
                  mydata.DataSet.ReadXml(this.Server.MapPath("RPT_1.xml"));
                  return mydata.DataSet;
            }
            string SortField
            {
                  get
                  {
                        object o = ViewState["SortField"];
                        if (o == null)
                        {
                              return String.Empty;
                        }
                        return (string)o;
                  }
                  set
                  {
                        if (value == SortField)
                        {
                              //if ascending change to descending or vice versa.
                              SortAscending = !SortAscending;
                        }
                        ViewState["SortField"] = value;
                  }
            }

            // using ViewState for SortAscending property
            bool SortAscending
            {
                  get
                  {
                        object o = ViewState["SortAscending"];
                        if (o == null)
                        {
                              return true;
                        }
                        return (bool)o;
                  }
                  set
                  {
                        ViewState["SortAscending"] = value;
                  }
            }
   
            protected void Page_Load(Object src, EventArgs e)
            {
                  if(!IsPostBack)
                  {
                        BindGrid();
                  }
            }
      
            protected void BindGrid()
            {
                  XmlDataDocument mydata=new XmlDataDocument();
                  mydata.DataSet.ReadXml(Server.MapPath("RPT_1.xml"));
                  this.ds=mydata.DataSet;            
                  DataView dv = new DataView(ds.Tables[0]);
                  DataTable dt = dv.Table;
                  dv.Sort = SortField;
                  if (!SortAscending)
                  {
                        dv.Sort += " DESC";      //append "DESC" to the sort field name in order to sort descending
                  }
       
                  XmlDataGrid.DataSource = dv;
                  XmlDataGrid.DataBind();
            }
      
            protected void SortGrid(Object src, DataGridSortCommandEventArgs e)
            {
                  XmlDataGrid.CurrentPageIndex = 0;
                  SortField = e.SortExpression;      //get the requested sorting field name and set it to SortField
                  BindGrid();
            }
            
            public void doDelete(Object sender, DataGridCommandEventArgs e)
            {
                  XmlDataGrid.EditItemIndex=-1;
                  DataSet ds=CreateDataSource();
                  DataRow row=ds.Tables[0].Rows[e.Item.ItemIndex];
                  row.Delete();
                  //ds.WriteXml(Server.MapPath("RPT_1.xml"));
                  BindGrid();
            }
            
0
Comment
Question by:Carl3003
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
I believe this describes why it is not working for you and offers the appropriate solution:

http://www.theserverside.net/discussions/thread.tss?thread_id=23370

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
also are you specifiying the key for the record?

http://aspnet.4guysfromrolla.com/articles/090402-1.aspx

Regards,

Aeros
0
 

Author Comment

by:Carl3003
Comment Utility
Thanks for the help,
Yes i am specifying the key. It delete the record, i dont have a problem with that. The problem comes from my  function
 public void doDelete(Object sender, DataGridCommandEventArgs e)
          {
               XmlDataGrid.EditItemIndex=-1;
               DataSet ds=CreateDataSource(); //Here i am opening my data twice
               DataRow row=ds.Tables[0].Rows[e.Item.ItemIndex];
               row.Delete();
               //ds.WriteXml(Server.MapPath("RPT_1.xml"));
               BindGrid();
          }

As you can see I need to open a new dataset but i dont want to open same data twice, i want to be able to re-use the old one, keep deleting until probably i hit the submit button and then the dataset will be saved.If i dont open a new dataset, i get an error message saying "Table[0]" could not be located.I beleve one way to do is to use session( Session of the dataset ) and pass the session on postback. I wanted to do it in an easier way as i thought that there might be some way to do it..
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
Comment Utility
Then save your ds to a session value and reload like this:

From Page1:

Session("PassedDS") = ds


From Page2:

Dim ds As DataSet = Session("PassedDS")

Regards,

Aeros

0
 

Author Comment

by:Carl3003
Comment Utility
Could it be done on same page like this..
if(!postback) then create a session("ds-sn")=ds
else ds=session("ds-sn");
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Honestly I'm not sure I would assume so, but give it a whirl and let me know
0
 

Author Comment

by:Carl3003
Comment Utility
thanks, i am gonna keep trying  
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now