943,912 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1567
  • C# RSS
Jun 15th, 2009
0

Problem adding and saving changes to a dataRow in DataTable

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
petya.ivanova is offline Offline
7 posts
since Jun 2009
Jun 16th, 2009
0

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

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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Jun 17th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by JerryShaw ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
petya.ivanova is offline Offline
7 posts
since Jun 2009
Jun 17th, 2009
0

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

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:

c# Syntax (Toggle Plain Text)
  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:
Quote ...
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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 17th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by sknake ...
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:

c# Syntax (Toggle Plain Text)
  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);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
petya.ivanova is offline Offline
7 posts
since Jun 2009
Jun 17th, 2009
0

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

OK, then that is your problem. Change your random function to the one I posted and it should work fine.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 17th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by sknake ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
petya.ivanova is offline Offline
7 posts
since Jun 2009
Jun 17th, 2009
0

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

yes, it worked. Thank you sooo much
Reputation Points: 10
Solved Threads: 0
Newbie Poster
petya.ivanova is offline Offline
7 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Password Types?(md5)
Next Thread in C# Forum Timeline: How can I refresh my discovered (using bluetooth windows stack) devices?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC