-3

How do I store a SqlParameter in a Hashtable? I'm trying to put P in param on my example below.

Hashtable param = new Hashtable();
SqlParameter P = new SqlParameter("@Picture", SqlDbType.Varbinary, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • You should use `Dictionary`, not `HashTable`. – SLaks Jun 10 '14 at 13:05
  • 3
    @SLaks: Least of his problems :| If you ask what you're trying to do, OP, you'd get better help. –  Jun 10 '14 at 13:07
  • What's the concrete problem? – usr Jun 10 '14 at 13:13
  • In particular, do you want the parameter to be the key or the value? What have you tried? What happened? – Jon Skeet Jun 10 '14 at 13:14
  • Indeed. It would be nice to see how you want to retrieve it from your HashTable (which should be Dictionary). – onefootswill Jun 10 '14 at 13:26
  • On the "should one use a Dictionary or a Hashtable" point, this MS page gives a little more info and may help one decide upon the best collection class to use (Dictionary, Hashtable or one of a number of other possibilities), based on what behaviour you're after: http://msdn.microsoft.com/en-us/library/6tc79sx1(v=vs.100).aspx – PulseLab Jun 10 '14 at 13:45
  • Also (apologies if using this comment thread not the best place to do this, still learning my SO etiquette here) one might choose to use a Hashtable over a Dictionray due to it's thread-safe nature on all members, and it's different behaviour when requesting a key that doesn't exist...? – PulseLab Jun 10 '14 at 13:50

1 Answers1

0

The basic syntax to add a value to a Hashtable, using your variable names above, is:

param.Add("Picture", P);

Where "Picture" is a key you can later retrieve the item back out of the hashtable by, e.g.:

SqlParameter retrieved = (SqlParameter)param["Picture"];

That said, and as others have already commented on your question, it may be worth considering using a Dictionary over a Hashtable. A Dictionary is a typed instance of a hashtable, so you can specify the type of object you're inserting, which prevents any old junk being added to it (unless of course, you have a reason for not wanting it to be typed). I believe there's also a performance benefit in terms of boxing from having a typed Dictionary, too...

PulseLab
  • 1,577
  • 11
  • 15
  • 1
    Note that using `Hashtable`, you need to cast when retrieving. (And there's no boxing involved for `string` or `SqlParameter`.) – Jon Skeet Jun 10 '14 at 13:15