
May 8th, 2008, 10:31 AM
|
|
|
SqlException: The parameterized query ## expects parameter ##, which was not supplied
I have the following code:
PHP Code:
1 public void UpdateTableInfo(string command, string dbTableName, DataTable dataTable, IList<string> parameters)
2 {
3 _dataAdapter.InsertCommand = new SqlCommand(@command);
4 IList<SqlDbType> tableColumnTypes = GetTableColumnTypes(dbTableName);
5 for (int i = 0; i < parameters.Count; i++)
6 {
7 _dataAdapter.InsertCommand.Parameters.Add(new SqlParameter("@" + parameters[i], tableColumnTypes[i]));
8 }
9 SqlConnection conn = DbConnection;
10 _dataAdapter.InsertCommand.Connection = conn;
11 using (conn)
12 {
13 _dataAdapter.Update(dataTable.DataSet, dbTableName);
14 }
15 }
...which produces the following SqlException exception, on Line 13:
Quote: | {"The parameterized query '(@Value0 varchar(8000),@Value1 ntext,@Value2 decimal(29,0))INSER' expects the parameter '@Value0', which was not supplied."} |
I can't figure out what the problem is here.
The connection string (DbConnection) is valid. The 'command' argument of the method evaluates to "INSERT INTO Catalog VALUES (@Value0,@Value1,@Value2)". The 'parameters' collection (in the method argument) returns {"Value0","Value1","Value2"}. The collection 'tableColumnTypes' returns a valid list of SqlDbType objects, that correspond correctly to the respective parameter names in the _dataAdapter.InsertCommand.Parameters collection (the SqlDbType objects also correspond correctly to the column data types in my database table). The dataTable and dbTableName arguments of the method are also valid.
Am I forgetting anything here? ;-)
I did consider the possibility of one of my parameter names being an SQL keyword (i.e, "Value"). But by using "Value0","Value1", etc., I think I have avoided this problem?
Also, the SQL table column names are "ID" [varchar(50)], "Description" [ntext], and "Price" [decimal(18,2)]. There is no primary key, but "ID" could easily be made the primary key.
Thanks for any help.
|