Problem adding and saving changes to a dataRow in DataTable

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 7
Reputation: petya.ivanova is an unknown quantity at this point 
Solved Threads: 0
petya.ivanova petya.ivanova is offline Offline
Newbie Poster

Problem adding and saving changes to a dataRow in DataTable

 
0
  #1
Jun 15th, 2009
Hi guys,

Im pretty new to C# and I have a problem hoping that most of you could solve cause you are quite an experienced developers
Here's my problem: I have a simple Windows Application Form with a dataGridView and a button. I want when the button is clicked to fill a simple dataTable and bind it to the dataGridView. The problem is that it always add the same row I mean it adds nine identical rows. But when I put a breakpoint at the end of the outer foreach() everything works great and i have different rows with different values that originlly comes from generateRandomValue(). I tried with row.AcceptChanges() because I think that it's caching row data but no luck again


Here's my code:
  1. private void generateRandomGenes_Click(object sender, EventArgs e)
  2. {
  3.  
  4.  
  5. //header row
  6. foreach (DataColumn item in adapter.Table.Columns)
  7. {
  8. DataColumn header = new DataColumn();
  9. header.ColumnName = item.ColumnName;
  10. header.DataType = item.DataType;
  11. randomGenes.Columns.Add(header);
  12.  
  13. }
  14.  
  15.  
  16.  
  17. // Make data line
  18. int row_number = 1;
  19. for (int rows = 0; rows < 10; rows++)
  20. {
  21.  
  22. DataRow row = randomGenes.NewRow();
  23. row["Name"] = "artificial gene " + row_number;
  24.  
  25. int col_number = 1;
  26.  
  27. foreach (DataColumn column in randomGenes.Columns)
  28. {
  29.  
  30. if (col_number < 21)
  31. {
  32. row[col_number] = generateRandomValue(adapter.Table, col_number);
  33.  
  34. col_number++;
  35.  
  36. }
  37.  
  38.  
  39. }
  40.  
  41. randomGenes.Rows.Add(row);
  42. row.AcceptChanges();
  43.  
  44. randomGenes.AcceptChanges();
  45.  
  46. if (row_number <= 9)
  47. row_number++;
  48.  
  49. }
  50.  
  51.  
  52. dataGridView1.DataSource = randomGenes;
  53.  
  54.  
  55.  
  56.  
  57. }

Please, any suggestions. I'll really appreciate your help!
Last edited by Tekmaven; Jun 15th, 2009 at 4:29 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Problem adding and saving changes to a dataRow in DataTable

 
0
  #2
Jun 16th, 2009
Maybe posting the rest of the code (or entire project) will help. The adapter.table is a bit of a mystery.
The acceptchanges is not really needed until later after the rows have all been added. I think the use of the adapter object in the for loop may be at the root of the problem, but until it is more obvious as to what that object is... it is just an assumption.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: petya.ivanova is an unknown quantity at this point 
Solved Threads: 0
petya.ivanova petya.ivanova is offline Offline
Newbie Poster

Re: Problem adding and saving changes to a dataRow in DataTable

 
0
  #3
Jun 17th, 2009
Originally Posted by JerryShaw View Post
Maybe posting the rest of the code (or entire project) will help. The adapter.table is a bit of a mystery.
The acceptchanges is not really needed until later after the rows have all been added. I think the use of the adapter object in the for loop may be at the root of the problem, but until it is more obvious as to what that object is... it is just an assumption.
No, it's not neccesary you can try with some random data. You'll always get identical rows. adapter.table is fine, I use it in some other methods and everything works great. As I told you when you put a breakpoion at the end of the outer foreach and continue with F5, it's ok. Maybe the the changes of the row doesn't commit in runtime. I don't know, really
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Problem adding and saving changes to a dataRow in DataTable

 
0
  #4
Jun 17th, 2009
Paste the code for your generateRandomValue(); method. The problem is likely that when you have a break point that one second passes on your computers clock that changes the seed value for generateRandomValue(). You probably should use another GetRandomValue() method:

  1. private void simpleButton4_Click(object sender, EventArgs e)
  2. {
  3. List<int> lst = new List<int>();
  4. for (int i1 = 0; i1 < 10; i1++)
  5. {
  6. lst.Add(GetRandom(10));
  7. }
  8. System.Diagnostics.Debugger.Break();
  9. }
  10.  
  11.  
  12. public static int GetRandom(int Maxvalue)
  13. {
  14. byte[] randomNumber = new byte[1];
  15. System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
  16. Gen.GetBytes(randomNumber);
  17. int rand = Convert.ToInt32(randomNumber[0]);
  18. return rand % Maxvalue + 1;
  19. }

I suspect you are using the "Random" class. This is a quote from the help file:
However, because the clock has finite resolution, creating different Random objects in close succession creates random number generators that produce identical sequences of random numbers. This problem can be avoided by creating a single Random object rather than multiple ones.
This would explain why debugger breaks would change the values since the clock would change in time between calls to generator.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: petya.ivanova is an unknown quantity at this point 
Solved Threads: 0
petya.ivanova petya.ivanova is offline Offline
Newbie Poster

Re: Problem adding and saving changes to a dataRow in DataTable

 
0
  #5
Jun 17th, 2009
Originally Posted by sknake View Post
Paste the code for your generateRandomValue(); method. The problem is likely that when you have a break point that one second passes on your computers clock that changes the seed value for generateRandomValue(). You probably should use another GetRandomValue() method:

  1. private void simpleButton4_Click(object sender, EventArgs e)
  2. {
  3. List<int> lst = new List<int>();
  4. for (int i1 = 0; i1 < 10; i1++)
  5. {
  6. lst.Add(GetRandom(10));
  7. }
  8. System.Diagnostics.Debugger.Break();
  9. }
  10.  
  11.  
  12. public static int GetRandom(int Maxvalue)
  13. {
  14. byte[] randomNumber = new byte[1];
  15. System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
  16. Gen.GetBytes(randomNumber);
  17. int rand = Convert.ToInt32(randomNumber[0]);
  18. return rand % Maxvalue + 1;
  19. }

I suspect you are using the "Random" class. This is a quote from the help file:


This would explain why debugger breaks would change the values since the clock would change in time between calls to generator.
Here's mu function :

private double generateRandomValue(DataTable table, int col_number)
{
int index;
Random randomValue = new Random();
index = randomValue.Next(1, 3258);

DataRow row = table.Rows[index];
return ((double)row.ItemArray[col_number]);

}

I just use it cause I have to choose a random value from the current column. To be more clear I have a table with 3000 genes and based on that table I have to generate artificial profiles of new genes.
and i call it when i fill the data table like this :

row[col_number] = generateRandomValue(adapter.Table, col_number);
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Problem adding and saving changes to a dataRow in DataTable

 
0
  #6
Jun 17th, 2009
OK, then that is your problem. Change your random function to the one I posted and it should work fine.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: petya.ivanova is an unknown quantity at this point 
Solved Threads: 0
petya.ivanova petya.ivanova is offline Offline
Newbie Poster

Re: Problem adding and saving changes to a dataRow in DataTable

 
0
  #7
Jun 17th, 2009
Originally Posted by sknake View Post
OK, then that is your problem. Change your random function to the one I posted and it should work fine.
Thank you, I will try it now
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: petya.ivanova is an unknown quantity at this point 
Solved Threads: 0
petya.ivanova petya.ivanova is offline Offline
Newbie Poster

Re: Problem adding and saving changes to a dataRow in DataTable

 
0
  #8
Jun 17th, 2009
yes, it worked. Thank you sooo much
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC