Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can't get DefaultView.Find to work like I want it...

Posted on 2004-09-02
3
Medium Priority
?
991 Views
Last Modified: 2008-01-09
Hello experts,

I am working on a basic time card system that is linked to a trouble ticket submission system.  This morning i discovered (much to my chagrin) that there is some bug going on in the time card calculation method.  I am trying to use the DefaultView.Find method to check if an existing account code is already in a datatable.  If the account code is in the table, I merely want to add the accompanying value (number of hours) to the account in the Datatable instead of adding a whole new row.  To try and explain this better I want to see:

Acct1      2.5 hours
Acct2      3 Hours

Instead of

Acct1  2 hours
Acct2  3 hours
Acct1 .5 hours

I have some code which catches when there is a duplicate in the DB - but when I go to add it in the hours value is often put in the wrong area (generally column 0 row 0).  I would think that my code is totally wrong except sometimes - it works :/  Here is a snippet.

                //Call a stored procedure to add this Trouble Ticket to the database
      SqlCommand cmd = new SqlCommand("TURK.dbo.sp_GetHours", conn.Connection);
      cmd.CommandType = CommandType.StoredProcedure;
      SqlDataReader reader = null;
                        
      //Set up the DataTable with the display names for the Columns
      DataTable myTable = new DataTable("troubleTickets");
      myTable.Columns.Add("Grant Number");
      myTable.Columns.Add("Hours");
                        
      DataColumn[] colPk = new DataColumn[1];
      colPk[0] = myTable.Columns["Grant Number"];
      myTable.PrimaryKey = colPk;
      myTable.DefaultView.Sort = "Grant Number";
      
                ...            

while (reader.Read())
     {
                string[] myStringArray = new string[2];
      decimal totalHours = reader.GetDecimal(1);
      //Makes sure something is in the grants charged area
      if(reader.GetString(2).Length > 0)
     {
      //gets information for the specific trouble ticket bill time, total hours,
      //and percent charged that corresponds with the correct grant
      decimal percentCharged = (reader.GetDecimal(6));
      decimal billTime = CalculateHours(percentCharged,totalHours);
                                    
      //Set up the array to hold values to input into the dataTable
      //Pulls values from reader
      myStringArray[0] = reader.GetString(2);
      string showMe = reader.GetString(2);
      myStringArray[1] = System.Convert.ToString(billTime);
                                    
      //Check if row exists, if no row exists add a new row to the existing
      //DataTable, otherwise, add the extra time charged to the existing DataRow
      //"TimeCharged" column
      DataRow drFound;
                                          
      // Find the Row specified in txtFindArg
      drFound = myTable.Rows.Find(reader.GetString(2));
      if (drFound == null)
      {
            //No row with the same PK exists, add a new row to DT
            myTable.Rows.Add(myStringArray);
      }
      
else
      {
      //Otherwise, get the TimeCharged in the DataTable, and add it to
      //the TimeCharged in the reader
      int duplicateRow;
      decimal oldValue;
      decimal newValue;
                                                
      duplicateRow= myTable.DefaultView.Find(reader.GetString(2));
      //Grab the next three values for error checking - see what they are.
                string showMeHere = reader.GetString(2);
      string showMore = System.Convert.ToString(myTable.Rows[whoopie][0]);
      string evenMore = System.Convert.ToString(myTable.Rows[whoopie][1]);
      //end data checking
                oldValue = System.Convert.ToDecimal(myTable.Rows[duplicateRow][1]);
      newValue = System.Convert.ToDecimal(myStringArray[1]);
      newValue += oldValue;
      myTable.Rows[duplicateRow].BeginEdit();
      myTable.Rows[duplicateRow][1] = (System.Convert.ToString(newValue));
      myTable.Rows[duplicateRow].EndEdit();
      }                                    
      }

Sometimes the duplicateRow is the correct number, and adds the corresponding values accordingly.  Other times it is wrong and adds it to other columns (one of which is account code "none" - thank you users :P )

I will be glad to provide any more information you see fit.  Sorry for the long post wanted to be as specific as possible.
TIA
0
Comment
Question by:RuralInstitute
[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
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 11967605
why are you using the reader.GetString(2) and reader.GetDecimal(6) etc..? This sure makes it hard are hell to tell what we are looking at..

Have you seen this syntax reader["FieldName"]?

Sorry althought, I can't really help point out where this issue lies in your code, but it would seem that it should be possible to extract the data the way you want using GROUP BY and SUM in SQL server and save yourself alot of this jumping through hoops.


0
 

Author Comment

by:RuralInstitute
ID: 11967888
So summing is a good idea - don't know why I didn't do that a few months ago in the first place...I like to make things difficult i guess.

As for the GetString(2) etc, those are the corresponding columns in the current selection of the datareader.  GetString(2) would be the AccountCode to check against/add to the datatable.

Going to SQL Land right now.
0
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 1500 total points
ID: 11968016
Well just a suggestion.. if you have trouble, you can get expert help over in the Sql Server area to get this data returned to you exactly as you want it...  

I guess that reader.getString() is just a preference, I guess i was just saying it's makes the code more readable when doing reader["AccountCode"] vs. reader.GetString(2)

good luck
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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