I am developing a C# VS 2008 / SQL Server website application. I am a newbie to ASP.NET. I am getting the above compiler error. Can you give me advice on how to fix this?
Code snippet:
public static string AppendDataCT(DataTable dt, 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;
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;
}
conn2.Open();
cmd.ExecuteNonQuery();
It errors on this last line here.
And here is that SP:
ALTER PROCEDURE [dbo].[AppendDataCT]
@col1 VARCHAR(50),
@col2 VARCHAR(50),
@col3 VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @TEMP DATETIME
SET @TEMP = (SELECT CONVERT (DATETIME, @col3))
INSERT INTO Person.ContactType (Name, ModifiedDate)
VALUES( @col2, @TEMP)
END
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
although this line
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
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.