If your item array contains more than 1 string this will happen because you will be setting a number of parameters = 3* No Strings in the item array.
also you are not using the string s that you are pulling out in your loop.
there seems to of been some evolution going on here.
you need to sit down and figure out whether you want to execute the insert multiple times (right now you are only executing one) or not.
There are other ways to set the type of the parameters so you don't need your p1,2,3 variables btw.
I suspect that you want to do something more like this
public static string AppendDataCT(DataRow dr, Dictionary<int, string> dic)
{
string connString = ConfigurationManager.ConnectionStrings["AW3_string"].ConnectionString;
string errorMsg;
try
{
SqlConnection conn2 = new SqlConnection(connString);
SqlCommand cmd = conn2.CreateCommand();
cmd.CommandText = "dbo.AppendDataCT";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn2;
SqlParameter p1, p2, p3;
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;
conn2.Open();
cmd.ExecuteNonQuery();
conn2.Close();
}
although this line
foreach (string s in dt.Rows[1].ItemArray)
really has me puzzled ... i think your are doing something wrong here - it tells me nothing about what you are trying to do and seems confused. In 6 months time you won't have a clue how this works. Moreover the caller can probably misbehave in many many ways which will only be caught at run time.
Why not just pass in an int saying how many times the loop will occur?
Id also perform a count on the dictionary to confirm that there are 3 and only 3 items in it because no matter how many times you loop you will be inserting the same 3 items.
why not use a dictionary and use the param name as the index? or loop through the list of keys as strings and add the params that way?
soemthing like this
/// <summary>
///
/// </summary>
/// <param name="dic">key = param name, val = param value</param>
/// <returns></returns>
public static string AppendDataCT(Dictionary<string, string> dic)
{
if (dic.Count !=3 )
throw new ArgumentOutOfRangeException("dic can only have 3 parameters");
string connString = ConfigurationManager.ConnectionStrings["AW3_string"].ConnectionString;
// you probably want to do a string.IsNullOrEmpty(connString) and throw a ConfigurationException here is true to quickly identify this annoying bug ...
using(SqlConnection conn2 = new SqlConnection(connString))
{
using( SqlCommand cmd = conn2.CreateCommand())
{
cmd.CommandText = "dbo.AppendDataCT";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn2;
foreach (string s in dic.Keys)
{
SqlParameter p = cmd.Parameters.AddWithValue(s, dic[s]);
p.SqlDbType = SqlDbType.VarChar;
}
conn2.Open();
cmd.ExecuteNonQuery();
conn2.Close();
}
}
}
that code is nowhere near perfect but may be kind of what you meant.
If you do want to do multiple inserts then maybe consider a list<dictionary<string,string>>
or better yet make a simple struct to hold the parameters and have a list of the structs and pass that in. The reason for the struct/class for the args is that it can valiate the data to protect your method from all kinds of nasty data causing it to explode. Always nice to make someone else code behave rather than cope with all their bizzarre permutations.