Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 829813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:56:32+00:00 2026-05-15T03:56:32+00:00

I am developing a C# VS 2008 / SQL Server website application. I am

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:56:32+00:00Added an answer on May 15, 2026 at 3:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 432k
  • Answers 432k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As per Trevor's message in this thread I'm after the… May 15, 2026 at 2:43 pm
  • Editorial Team
    Editorial Team added an answer What you describe is probably most easily implemented using some… May 15, 2026 at 2:43 pm
  • Editorial Team
    Editorial Team added an answer Flash won't get mouse events when the mouse is out… May 15, 2026 at 2:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.