Solved

Delete record only in dataset

Posted on 2004-08-13
7
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

751 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