Solved

Delete record only in dataset

Posted on 2004-08-13
7
274 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
ID: 11796183
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
ID: 11796194
also are you specifiying the key for the record?

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

Regards,

Aeros
0
 

Author Comment

by:Carl3003
ID: 11796514
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 11796604
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
ID: 11796755
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
ID: 11796815
Honestly I'm not sure I would assume so, but give it a whirl and let me know
0
 

Author Comment

by:Carl3003
ID: 11796906
thanks, i am gonna keep trying  
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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