Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Using Multi-dimensional array in ASP from C# Component

Posted on 2003-03-26
13
Medium Priority
?
519 Views
Last Modified: 2010-07-27
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
0
Comment
Question by:fteng
  • 6
  • 4
11 Comments
 

Author Comment

by:fteng
ID: 8215338
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
 
LVL 10

Expert Comment

by:smegghead
ID: 8216728
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
 

Author Comment

by:fteng
ID: 8217200
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:fteng
ID: 8217256
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
 
LVL 10

Expert Comment

by:smegghead
ID: 8217261
can you return a single dimensioned array ??
0
 

Author Comment

by:fteng
ID: 8217319
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
 
LVL 10

Expert Comment

by:smegghead
ID: 8217333
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
 

Author Comment

by:fteng
ID: 8217343
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
 

Author Comment

by:fteng
ID: 8217348
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
 
LVL 10

Expert Comment

by:smegghead
ID: 8217992
try returning it as an object... I'm clutching at straws now you understand :-)

0
 
LVL 1

Accepted Solution

by:
rajesh79 earned 900 total points
ID: 8297256
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question