Solved

Delete record only in dataset

Posted on 2004-08-13
7
268 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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