Solved

Delete record only in dataset

Posted on 2004-08-13
7
262 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript in a user control not working 17 63
ISS migration from IIS 6 to IIS 8.5 rendering problems 4 50
asp.net mvc input file 2 32
Host asp.net pages 5 24
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…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

22 Experts available now in Live!

Get 1:1 Help Now