How do I access Dictionary items?

Posted by salvationishere on Stack Overflow See other posts from Stack Overflow or by salvationishere
Published on 2010-06-07T12:44:06Z Indexed on 2010/06/07 12:52 UTC
Read the original article Hit count: 239

Filed under:
|
|

I am developing a C# VS2008 / SQL Server website app and am new to the Dictionary class. Can you please advise on best method of accomplishing this? Here is a code snippet:

SqlConnection conn2 = new SqlConnection(connString);
SqlCommand cmd = conn2.CreateCommand();
cmd.CommandText = "dbo.AppendDataCT";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn2;
SqlParameter p1, p2, p3;
foreach (string s in dt.Rows[1].ItemArray)
{
    DataRow dr = dt.Rows[1]; // second row
    p1 = cmd.Parameters.AddWithValue((string)dic[0], (string)dr[0]);
    p1.SqlDbType = SqlDbType.VarChar;
    p2 = cmd.Parameters.AddWithValue((string)dic[1], (string)dr[1]);
    p2.SqlDbType = SqlDbType.VarChar;
    p3 = cmd.Parameters.AddWithValue((string)dic[2], (string)dr[2]);
    p3.SqlDbType = SqlDbType.VarChar;
}

but this is giving me compiler error:

The best overloaded method match for 'System.Collections.Generic.Dictionary<string,string>.this[string]' has some invalid arguments

I just want to access each value from "dic" and load into these SQL parameters. How do I do this? Do I have to enter the key? The keys are named "col1", "col2", etc., so not the most user-friendly. Any other tips? Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about dictionary