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
Solved

Remove row from data table (hash table) C#  vs2010

Posted on 2013-12-24
7
651 Views
Last Modified: 2014-02-23
Background:  I am working on an asp.net website (intranet) using c# that employs proprietary web methods to download students exam material from a hosted web site.  
I have no control over what gets downloaded from a hosted site.  I do have control over what is displayed.   I am displaying an exam details page that opens from a link on a course page.   The problem is the hosted site is not filtering out previous exam results (for same exam) which is bad for me.   I am comparing dates and attempting to delete the older rows.  My aim is to display only one exam.  
I need help with the syntax to delete a row from a dataset, or I can select a row to publish to the databound fields on the html page.  I feel the later might be harder than just deleting the row.

ds is the dataset downloaded from hosted website, and configured with xml stylesheet.
Table:  Lesson
Column which compare is made:  LessonStatusDate

Code:
 if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row
                lessonCount = 0;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                    DateTime dt1 = DateTime.Now;

                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount > 1) && (dt.Date > dt1.Date))
                    {
                        //Delete dt1.Date
                       dt1=dt.Date
                    }
                    lessonCount++;
                }
            }


Thanks in advance for your help.   ...and yes, some of us used up all our vacation days and are working on Christmas Eve.  ;-(
0
Comment
Question by:JonDavid
  • 6
7 Comments
 

Author Comment

by:JonDavid
ID: 39738227
I have made progress, but am now deleting the wrong date:
if (lesson.Rows.Count > 1)
            {anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                //Keep the most recent row
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to
 foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt.Date > dt1.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                        dt1 = dt.Date;
                    }

                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
          }
0
 

Author Comment

by:JonDavid
ID: 39738260
This is my third time through, and it works.   Please comment if there is a better way to do this:
if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt1.Date > dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                        dt1 = dt.Date;
                    }
                    else if((lessonCount >=1) && (dt1.Date < dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount-1].Delete();
                    }

                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
            }
0
 

Author Comment

by:JonDavid
ID: 39738298
Found on potential error.  Should I ever see three rows, I needed to set the dt1 to the max value.   My original request was for help with the syntax.  This code works, but I think it can be done much better.  Would appreciate any help I can get on this matter.

 if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row, delete all others.
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt1.Date > dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                    }
                    else if((lessonCount >=1) && (dt1.Date < dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount-1].Delete();
                        dt1 = dt.Date;
                    }
                   
                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
            }
                       }
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 39738880
Hi Jon,
It can be easily done using Linq where we can apply group by and order by  as sql server.
please find following code for this :
DataTable lession = new DataTable("lession");
            lession.Columns.Add(new DataColumn("LessionId", typeof(string)));
            lession.Columns.Add(new DataColumn("LessonStatusDate", typeof(DateTime)));
            lession.Columns.Add(new DataColumn("LessonGrade", typeof(string)));
           
            //add records for lession 1
            lession.Rows.Add(new object[] { 1, "12/12/13","A" });
            lession.Rows.Add(new object[] { 1, "12/10/13","B" });
            lession.Rows.Add(new object[] { 1, "12/22/13","A" });
            //add records for lession 2
            lession.Rows.Add(new object[] { 2, "12/15/13","B" });
            lession.Rows.Add(new object[] { 2, "11/22/13","A" });
            //add records for lession 3
            lession.Rows.Add(new object[] { 3, "12/18/13","O" });
            lession.Rows.Add(new object[] { 2, "12/05/13" ,"A"});
           
            //add datatable to dataset
            ds.Tables.Add(lession);


            // group by lession id and order by LessonStatusDate descending and add first record of each group to result table
            DataTable dt = ds.Tables["lession"].AsEnumerable()
                .GroupBy(r => new { Col1 = r["LessionId"] }) 
                .Select(g => g.OrderByDescending(r => r["LessonStatusDate"]).First()).CopyToDataTable();  

Open in new window


now, in datatatble dt will be having filtered recordset for each lession.hope this helps. Merry Christmas !!!
0
 

Accepted Solution

by:
JonDavid earned 0 total points
ID: 39742024
I can see where this would be helpful.   Since all of the data is downloaded through XML style sheets, and a string reader, I would to make a few changes, but overall, this would give more lattitude in manipulated style, and content to the page.   Thanks!  Hope you had a Merry Christmas.
0
 

Author Comment

by:JonDavid
ID: 39868037
Project is closed and passed UAT.  Will go into production next week.
0
 

Author Closing Comment

by:JonDavid
ID: 39880360
The answer given by the previous person required more time than was alotted in project.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Function after success of Jquery/AJAX action 9 28
Iteration Help (Asp.net VB) 5 24
abstract class C# 1 31
ASP.NET Content Page 3 27
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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…

856 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