views:

47

answers:

1

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
+1  A: 

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.

John Nicholas
Thanks, John. I do want to go through the list and retrieve values. But at first I was trying to just get the first row of data. Once this worked, I planned on getting the other rows to work also. I will try out your code though!
salvationishere
Does this code create 3 separate parameters that I can load? It looks like the same parameter name to me: p1
salvationishere
hmm .. I imagine AddWithValue constructs a new Parameter and so even though there is no p1=new Parameter(); the parameter that p1 references will be different each time the loop executes.
John Nicholas
I just updated the code to make the parameter thing clearer.
John Nicholas
spread the love mark it as an answer ;)
John Nicholas
You have been most helpful! Thank you!
salvationishere