?
Solved

Using Multi-dimensional array in ASP from C# Component

Posted on 2003-03-26
13
Medium Priority
?
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
13 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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