Reputation: 33
Im creating an small Windows application program in c#, and using HASHTABLES i've got this error when I want to add values in a strored procedure. Error: "The non-invocable Hashtable.keys member can not be used as a method"
the error appears after hs.keys(n)
This is the code that im writting:
public int EscribirBackup(string nombre, Hashtable hs)
{
SqlCommand cm = new SqlCommand();
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = nombre;
for (var n = 0; n <= hs.Values.Count - 1; n++)
cm.Parameters.AddWithValue(hs.Keys(n), hs.Values);
cm.Connection = ABRIR("NORMAL");
int i = cm.ExecuteNonQuery();
CERRAR();
return i;
}
Now, when I run the program im getting this error:
'There is no assignment of object type System.Collections.Hashtable + ValueCollection to a native type of a known managed provider.'
on int i = cm.ExecuteNonQuery();
Upvotes: 0
Views: 141
Reputation: 24957
Hashtable.Keys
is a collection property (member of System.Collections
) which has this definition:
public virtual System.Collections.ICollection Keys { get; }
Note that ICollection
properties doesn't expose indexer, you should convert it to other collection which exposes indexer.
Hence, you should convert it to an array or List
instance first using Cast<string>()
, then use square brackets to access the specified index as in example below:
var keys = hs.Keys.Cast<string>().ToArray();
var values = hs.Values.Cast<string>().ToArray();
for (var n = 0; n <= hs.Values.Count - 1; n++)
{
cm.Parameters.AddWithValue(keys[n], values[n]);
}
Upvotes: 2