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 840845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:41:37+00:00 2026-05-15T05:41:37+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 error, however, on the last line of the following code. Can you give me advice on how to fix this? This compiles correctly, but I encounter this error after running it.

All that I am trying to do is to store the items from the second row of “dt” into string parameters. The first row is the header, so I don’t want these values. The second row is the first row of values. My SQL stored procedure requires these values as strings. So I want to parse the second row of data and load into 2 string parameters. I added more of my code below.

DataTable dt; 
Hashtable ht;
string[] SingleRow;
...
SqlConnection conn2 = new SqlConnection(connString);
SqlCommand cmd = conn2.CreateCommand();
cmd.CommandText = "dbo.AppendDataCT";
cmd.Connection = conn2;
SingleRow = (string[])dt.Rows[1].ItemArray;
            SqlParameter sqlParam = cmd.Parameters.AddWithValue("@" + ht[0], SingleRow[0]);
            sqlParam.SqlDbType = SqlDbType.VarChar;
            SqlParameter sqlParam2 = cmd.Parameters.AddWithValue("@" + ht[1], SingleRow[1]);
            sqlParam2.SqlDbType = SqlDbType.DateTime;

My error:

System.InvalidCastException was caught
  Message="Unable to cast object of type 'System.Object[]' to type 'System.String[]'."
  Source="App_Code.g68pyuml"
  StackTrace:
       at ADONET_namespace.ADONET_methods.AppendDataCT(DataTable dt, Hashtable ht) in c:\Documents and Settings\Admin\My Documents\Visual Studio 2008\WebSites\Jerry\App_Code\ADONET methods.cs:line 88
  InnerException: 
  • 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-15T05:41:38+00:00Added an answer on May 15, 2026 at 5:41 am

    Hmmm. You’re trying to access properties of the DataTable dt, but it looks like you might be expecting that table to contain the results of the AppendDataCT query. It doesn’t. Be careful with your row index accesses, too: Arrays in C# are 0-based, and dt.Rows[1] will retrieve the second row in the table.

    That aside, review the documentation for DataRow.ItemArray. That method returns an array of objects, not an array of strings. Even if your row contains nothing but strings, you’re still dealing with an array of objects, and you’ll have to treat it that way. You can cast each individual item in the row to a string, if that’s the proper datatype for that column:

    foreach (string s in dt.Rows[1].ItemArray)
    {
      //...
    }
    

    EDIT: Ok, in response to your edit, I see what you’re trying to do. There are many different ways to do this, and I’d particularly recommend that you move away from HashTables and towards generic equivalents like Dictionary – you’ll save yourself much runtime casting grief. That said, here’s the simplest adaption of your code:

    DataRow dr = dt.Rows[1]; // second row
    SqlParameter p1 = cmd.Parameters.AddWithValue((string)ht[0], (string)dr[0]);
    SqlParameter p2 = ...
    

    You don’t need the leading “@”; ADO.NET will add that for you. The (string) casts will work as long as there’s a string at key 0 (which is a fairly non-standard way to use hashtables – you’d usually have some kind of descriptive key), and if the first column in your datatable contains strings.

    I recommend you look at typed datasets and generic collections. The lack of them here makes your code somewhat brittle.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you get your XML from the database as a… May 16, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer It depends what the branch is for (what development effort… May 16, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer Did you want to create virtual mailboxes or forwarders for… May 16, 2026 at 4:57 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

Related Questions

I am developing a C# VS 2008 / SQL Server website application. I am
I am developing a C# VS 2008 / SQL Server 2008 ASP.NET Web Applications
I am developing a C#/SQL VS 2008 website application and I'm trying to set
I am developing a C# VS 2008 / SQL Server 2008 website, but now
I am developing a C# VS 2008 / SQL Server 2008 website. I have
I'm currently writing a website in ASP.NET MVC, and my database (which doesn't have
I am developing a financial application using SQLServer 2008 as database and I want
I'm developing a website using SiteFinity in Visual Studio 2008. This is a web
I'm developing a new admin website to update several tables. There is no complicated
I have an application that I'm developing in Visual Studio 2010 that utilizes a

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.