Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code. I'm trying to transfer contents of arraylist within an arraylist in to a ACCESS database. i get the following error when I run it:No value given for one or more required parameters . i can't figure out what the problem is. I'ma newbie so please forgive my ignorance if there's a stupid mistake.
C#
string con = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\\Actual1.accdb";
OleDbConnection cn = new OleDbConnection(con);
                   
for (i = 0; i <= 3; i++)
{
  for (j = 0; j <= 3; j++)
  {
    string column_name = "cl" + i;
    OleDbCommand cmd = new OleDbCommand("Update Table1 set "+column_name+"="+str[i][j]+" where ID="+i+"", cn);
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    cmd.Connection.Close();
  }
}



error occurs when cmd.executenonquery(); is executed.
column_name is cl0.
i=0.
j=0.
Posted
Updated 21-Sep-14 2:25am
v5
Comments
BillWoodruff 21-Sep-14 8:05am    
Set a break-point in the inner for loop, and single-step through the code when you hit the breakpoint. Modify your question here to include the information about where the error occurs.
[no name] 21-Sep-14 8:05am    
Debug it and find out what the values of column_name, str, i, and j are at the time of the error. We can't do that for you.

Probably, it has to do with the content on your str array.
It's very dangerous to do it that way: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The changes are that will cure your problem at the same time:
C#
OleDbCommand cmd = new OleDbCommand("Update Table1 set "+column_name+"=@CL where ID="+i+"", cn);
cmd.Parameters.AddWithValue("@CL", str[i][j]);
cmd.Connection.Open();
cmd.ExecuteNonQuery();

Technically, the ID should be a parameter as well, but since it's an int it's not a problem.But the text string for the column content could be anything.
 
Share this answer
 
Comments
Tejas Shastri 21-Sep-14 8:21am    
Now there's another error:
"Index was out of range. Must be non-negative and less than the size of the collection"

What index is the compiler talking about??
OriginalGriff 21-Sep-14 8:46am    
Either "i" or "j" in "str[i][j]" - you need to look at your str array and see how big it is. Since it's a jagged array (the "j" dimensions don't all have to be the same for the different values of "i") it's quite likely that the for loop(s) need changing to reflect the "real" sizes.
And there is a good chance that you meant to use "j" instead of "i" for your ID value as well! :laugh: (But that won't cause an error message, just rubbish in your DB.)
Tejas Shastri 21-Sep-14 9:04am    
fixed it.. thanks :)
OriginalGriff 21-Sep-14 9:21am    
You're welcome!
On SQL injection, please see: http://en.wikipedia.org/wiki/SQL_injection[^].

This is how it works: http://xkcd.com/327[^]. :-)

The idea is: the user can enter a fragment of SQL code in the UI. Please see my past answers:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

This is what you should use: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 30-Sep-14 17:07pm    
I see little Bobby is still doing well ...
BTW: a 5 :-)
Sergey Alexandrovich Kryukov 30-Sep-14 17:12pm    
Probably, little Bobby Tables keeps screwing up the data, giving a little lesson to the sloppy developers over and over.
Thank you, Espen. How have you been all this time? Glad to hear from you.
—SA
Espen Harlinn 30-Sep-14 17:28pm    
Just had a much needed 3 week holiday, and this is my last day at Goodtech :-)

Tomorrow will be my first official day as chief architect at Powel - I've been working with them for more than a year now - and it's been a great experience. So now we're looking for a decent place to live in Trondheim.

I sincerely hope all is well with you and that you're having a good time too.
Sergey Alexandrovich Kryukov 30-Sep-14 17:56pm    
Ah... Congratulations... I guess. Do you mean you're supposed to move?
I recently had one shorted vacation, some 10 days, but they give me about a month (which is very long for US, where most people have no more that 2 weeks); in an hour, will probably arrange another 2-day vacation, due to important reason: next agricultural fair which is hard to reach from home.
—SA
Espen Harlinn 30-Sep-14 18:12pm    
>> Ah... Congratulations... I guess
No need to guess: http://www.powel.com

>> Do you mean you're supposed to move?
Yes - we will be moving to Trondheim.

Powel is a spin-off from the Norwegian Electric Power Research Institute (EFI), and used to be a part of the SINTEF Group, which is, by far, the largest research organization here in Norway.

>> but they give me about a month
Good for you :-)

>> due to important reason: next agricultural fair
Now I'm getting curious ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900