1

I have an application where I need to store a value in a column. The value is a string in the code and the column data type is nvarchar.

foreach (DataRow row in dt.Rows)
{
    //Generate a random length for randomKey range between 3 and 6 characters
    Random randKey = new Random();
    int randKeyLength = randKey.Next(3, 6);

    //calculate randomKeyString
    String randomKeyString = md5Prepare.gen_randomKeyString(randKeyLength);

    //add randomKeyString to database
    row["randomKey"] = randomKeyString;
}

When I check the database, the column "randomKey" is unchanged. What is wrong here?

4
  • When I check the database, the column "randomKey" - I see no code for update in database, how are you updating the database ? Commented Nov 28, 2012 at 6:42
  • 1
    You're just updating the DataRow in your DataTable (all in-memory representations of your data) - you're not yet storing it back to the database! Commented Nov 28, 2012 at 6:42
  • Doesn't row["randomKey"] = randomKeyString; change the column value for that specific row? Commented Nov 28, 2012 at 6:42
  • 4
    It changes the value for that column in that DataRow - in memory (RAM) - but NOT in the database table. Check out something like this basic ADO.NET tutorial to study the basics of accessing a database using "raw" ADO.NET Commented Nov 28, 2012 at 6:43

3 Answers 3

1

You didn't call any Commit methods on DataRow or DataTable.
You need the DataAdapter and DataSet to actually update database.

foreach (...)
{
    // your code here
}
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dt);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.Update(dataSet);
Sign up to request clarification or add additional context in comments.

5 Comments

Your solution gives the following error: Update unable to find TableMapping['Table'] or DataTable 'Table'.
@user1109363 Well, it is up to you to create DataAdapter in your connection, and call him right way.
I am sorry, I don't quite understand.
@user1109363 read the tutorial marc_s mentioned. After that - use methods for your own database.
@user1109363 Good luck with your projects :)
0

There is nothing wrong with what you are doing to set the value. Strings are unicode in C# as is nvarchar. You simply need to write the values back/insert them into the database as what you are doing at the moment is manipulating the in memory representations of what you have read from the database.

Comments

0

Do not create the instance of the Random object inside the foreach: Rather create it before.

The sequence of random numbers generated by a single Random instance is supposed to be uniformly distributed. By creating a new Random instance for every random number in quick successions, you are likely to seed them with identical values and have them generate identical random numbers. Of course, in this case, the generated sequence will be far from uniform distribution.

For the sake of completeness, if you really need to reseed a Random, you'll create a new instance of Random with the new seed:

rnd = new Random(newSeed);

Thanks to Mehrdad Afshari

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.