Using Multi-dimensional array in ASP from C# Component

I executed a sql server stored procedure in C# and returned a multi-dimensional array of type string.

Used component callable wrapper to package C# component for use with ASP.

Called the component in ASP but I get a type mismatch error when trying to extract the data.

Tried casting result using cstr. Doesn't work. Please help, I'm really new at this stuff. My brain hurts from all this debugging. Thank you.

eg:

<%
set objWeb = Server.CreateObject("FredStored2.Actions")
result = objWeb.showClients(1110)   'result is a multi-D array c#: string[1,4]
response.write (cstr(result(0,0)))  
set objWeb = nothing
%>


Fred
ftengAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ftengAuthor Commented:
Here's more info. I'm extremely new to C# (less than 7 days), I made a stuct in C# and call a stored procedure to select data from a SQL server database. I've packaged the result as an array of structs. Now I want to use this array of structs in ASP. (I went through the whole tlbexp, regasm, gacutil jazz).  Please help, I think it's something to do with marshalling. I get a type mismatch error all the time.


Here's my C# code:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Runtime.InteropServices;

namespace FredStored2
{
     public class Actions
     {
          public Actions()
          {              
          }

          public struct clientData
          {
               public int f_id;
               public string f_name;              
               public string f_address;
               public byte f_isAvailable;
          }
         
          SqlConnection objConnect;

          public void makeConnection()
          {
               objConnect = new SqlConnection();
               objConnect.ConnectionString = "server=localhost; database=northwind; uid=sa; pwd=;";
               objConnect.Open();
          }

          [return: MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]
          public clientData[] showClients(int cID)
          {
               makeConnection();

               SqlCommand objCommand = new SqlCommand("viewClients",objConnect);
               objCommand.CommandType = CommandType.StoredProcedure;
               
               objCommand.Parameters.Add("@f_id",SqlDbType.Int);
               objCommand.Parameters["@f_id"].Direction = ParameterDirection.Input;
               objCommand.Parameters["@f_id"].Value = cID;
           
               SqlDataReader objReader;
               objReader = objCommand.ExecuteReader();

               ArrayList listClients  = new ArrayList();

               while (objReader.Read())
               {
                    clientData objClient = new clientData();
                    objClient.f_id = (int) objReader[0];
                    objClient.f_name = objReader[1].ToString();
                    objClient.f_address = objReader[2].ToString();
                    objClient.f_isAvailable = (byte) objReader[3];                    
                   
                    listClients.Add(objClient);                    
               }
               
               clientData[] clients = new clientData[listClients.Count];

               for (int i=0; i < listClients.Count; i++)
               {
                    clients[i] = (clientData) listClients[i];
               }

               objConnect.Dispose();

               return clients;
          }

     }
}


Here's the ASP code:

<%
set objWeb = Server.CreateObject("FredStored2.Actions")

result = objWeb.showClients(1110)

' How do I get the data out?

set objWeb = nothing

%>

0
smeggheadCommented:
trying returning the array as a jagged array rather than a rectangular array..

i.e. declared as

string[][]

This is a more portable format.

Smg.
0
ftengAuthor Commented:
I still get a type mismatch. I guess the only thing let for me to do is just return a plain string maybe with commas.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

ftengAuthor Commented:
I still get a type mismatch. I guess the only thing let for me to do is just return a plain string maybe with commas.
0
smeggheadCommented:
can you return a single dimensioned array ??
0
ftengAuthor Commented:
I still get a type mismatch. It seems that ASP only like simple data types. A string array doesn't seem that complicated, I'm surprised it's not recognizing it as a variant array.

I'm happy someone is out there trying to help me. Thank you, I really appreciate your help.
0
smeggheadCommented:
asp should work with arrays - I've done this before using the old ASP (i.e. not .net) - so I'm sure ASP.net must be able to cope with this.

Is it within the ASP code you're getting the error or is it generated from within the c# code ??

Smg.
0
ftengAuthor Commented:
The c# code works perfectly then i export the .net component to com. It works fine in regular ASP when I use a string, but craps out when i throw in an array. I get type mismatch with:

<%
set objWeb = Server.CreateObject("FredStored2.Actions")
result = objWeb.showClients(1110)
response.write (result(0))
set objWeb = nothing
%>

When I change it to:

<%
set objWeb = Server.CreateObject("FredStored2.Actions")
result = objWeb.showClients(1110)
response.write (result)
set objWeb = nothing
%>

I get "An unhandled data type was encountered"
0
ftengAuthor Commented:
here's the updated c# code:

public string[] showClients(int cID)
          {
               makeConnection();

               SqlCommand objCommand = new SqlCommand("viewClients",objConnect);
               objCommand.CommandType = CommandType.StoredProcedure;
               
               objCommand.Parameters.Add("@f_id",SqlDbType.Int);
               objCommand.Parameters["@f_id"].Direction = ParameterDirection.Input;
               objCommand.Parameters["@f_id"].Value = cID;
           
               SqlDataReader objReader;
               objReader = objCommand.ExecuteReader();

               ArrayList listClients  = new ArrayList();

               while (objReader.Read())
               {
                    clientData objClient = new clientData();
                    objClient.f_id = (int) objReader[0];
                    objClient.f_name = objReader[1].ToString();
                    objClient.f_address = objReader[2].ToString();
                    objClient.f_isAvailable = (byte) objReader[3];                    
                   
                    listClients.Add(objClient);                    
               }
               
               string[] clients = new string[listClients.Count];

               for (int i=0; i < listClients.Count; i++)
               {
                    clients[i] = ((clientData) listClients[i]).f_id.ToString() + " ";
                    clients[i] += ((clientData) listClients[i]).f_name + " ";
                    clients[i] += ((clientData) listClients[i]).f_address + " ";
                    clients[i] += ((clientData) listClients[i]).f_isAvailable.ToString() + " ";
               }

               objConnect.Dispose();

               return clients;
          }
0
smeggheadCommented:
try returning it as an object... I'm clutching at straws now you understand :-)

0
rajesh79Commented:
Hi

The problem here is a datatype mismatch because
array in .net is object where array in asp(vbscript)
is variant.

instead of creating an array in C# and returning it to asp

create an comma seperated string and  return it.

and the split it in asp code using built in split function
which creates an array for U.

like this one. suppose u want UserID and UserName in array

then create a string like :

"1_Rajesh,2_fleng,3_smegghead"

Dim array

array = split(string,",")

u will get array of 3 element

now in a loop of this 3 element u can again
aplit it like

Dim userArray = split(Array(i),"_")

u will get an array of 2 element

first element will be UserID and second will be UserName

just check whether this one helps u.

Rajesh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.