views:

380

answers:

6

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

You can't cast an array of objects into an array of strings, you have to cast each item in the array, as each items has to be checked if it can be cast. You can use the Cast method for that:

SingleRow = dt.Rows[1].ItemArray.Cast<string>().ToArray();
Guffa
Thanks, I tried this but it gave me a compiler error. Cast is not recognized in this context.
salvationishere
That code, I believe, requires Linq so you need to be using .net 3.5 or greater and you have to reference Linq and use System.Linq
Mystere Man
A: 

You can use LINQ for that:

var yourStringArray = dt.Rows[1].ItemArray
    .Select(o => (o ?? (object)String.Emtpy).ToString())
    .ToArray();

This converts each item of the array to a string by using ToString() and returns an empty string if the item is null.

Oliver Hanappi
I like your solution also using this line: SingleRow = dt.Rows[1].ItemArray.Select(o => (o ?? (object)String.Emtpy).ToString()).ToArray();But I am getting an error with this:Error 1 'object[]' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'object[]' could be found (are you missing a using directive or an assembly reference?) ...
salvationishere
A: 

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.

Michael Petrotta
I liked this answer because it appeared simple. But when I tried to enter contents inside this foreach statement, I got compiler error:Use of unassigned local variable 'SingleRow'My statement:SingleRow[i] = dt.Rows[1].ItemArray.ToString();
salvationishere
It's hard to answer you without having a better understanding of what you're trying to accomplish. That said, you'll need to instantiate your `SingleRow` object before you can access properties of it.
Michael Petrotta
Can you describe what you're trying to accomplish, overall? Maybe some pseudocode would help.
Michael Petrotta
Thanks, I updated my description above.
salvationishere
Thanks for the helps, Michael! I still haven't gotten my code to work just yet but I'm still learning about Dictionary.
salvationishere
I'm sorry I tried to give you one more + vote for your hard work but it reset it to zero. If you edit your answer I will reset it back to 1.
salvationishere
@salvationishere: Edited my answer. Glad I could help.
Michael Petrotta
A: 

Each column item have it's own type, that can be different from String, so if you want to store each row value into an array you have to do it one-by-one with a loop, or with LINQ (I haven't tested this snippet but should do it's job):

(from columnVal in dt.Rows[1].ItemArray
select columnVal.ToString()).ToArray();
mamoo
is this C# code or LINQ?
salvationishere
LINQ query expressions is a component of C# 3.0+.
Michael Petrotta
do I have to include any special namespaces for this?
salvationishere
System.Linq, if not included. And your project must have .net 3.5 or 4.0 as target framework.
mamoo
+1  A: 
string[] arr = Array.ConvertAll(dt.Rows[1].ItemArray, o => (string)o);

(you can also use Cast<T>().ToArray(), but this approach works on more framework versions, and gets the array size correct from the start, rather than resizing it)

Marc Gravell
A: 

How about this: make string[] SingleRow into object[] SingleRow, which will let the line

SingleRow = (object[])dt.Rows[1].ItemArray;

execute properly. Subsequently when you need to access its values as an array of strings, cast it or LINQ select it like this:

objectArray.Select<object, string>
   (c => (c != null ? c.ToString() : "")).ToArray();

Make sure to add the following usings:

using System.Linq;
using System.Collections.Generic;
code4life
I have never used LINQ before. How would I specify this second line? I tried both objectArray.Select<object, string>(c => c.ToString()).ToArray();andSingleRow.Select<object, string>(c => c.ToString()).ToArray();but both gave me compiler errors.
salvationishere
Which version of .NET are you using? Erm... I also forgot to mention, you should add using System.Linq; using System.Collections.Generic;
code4life
it's 3.5 SP1. I added these "using" statements but I am still getting compiler error:The name 'objectArray' does not exist in the current context
salvationishere