• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

Passing parameter of unknown type

Greetings all

I have a page from which the user selects a procedure to execute. Each procedure may be linked to one or more parameters (stored in a db table as a string value and a single character, indicating the type).

Once the user has selected the procedure, he or she is then able to populate the parameters.

When the user hits the "Execute" button I want to pass the procedure name and parameters to the datalayer which will do the retrieval.

My problem(s):

1.  Number of paraneters per procedure varies (some have no parameters)
2.  The parameter types vary

How can I do this?


In advance, thanks!!!


    allanmark

0
allanmark
Asked:
allanmark
  • 4
  • 4
  • 3
2 Solutions
 
p_davisCommented:
how about an object array?
0
 
p_davisCommented:
void DynamicMethod(Object[] objectArray)
{

   for(int i = 0; i<objectArray.Length;i++)
  {
          if(objectArray[i].GetType() == typeof(String))
                ProcessStringType(objectArray[i] as String);

//something like this maybe?  if not can you give a little more on what you are wanting to do.  


  }
}
0
 
williamcampbellCommented:
Would you be open to creating a snippet of XML

<procedures>
  <proc>proc_1
    <param>param_val_a</param>
    <param>param_val_b</param>
 </proc>
 <proc>proc_6
  <param>sometext</param>
 </proc>
<procedures>


Then at the other end you can unravel the XML and do what logic you have to do

Let me know if you want anymore help with this solution


   
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
allanmarkAuthor Commented:
At this stage, XML isn't an option (as determined by the powers that be!)!

There are parameter records (param name [as will be in the stored proc],  param description, param type [single char], default value [varchar]). These are read in and  the appropriate controls are dynamically created. When the user has entered the necessary parm details and clicks the execute button, I want to pass the params to a "generic" method in the DataLayer that will build the parametrs for the stored proc and then call the procedure.

My first idea was to create a class for the params (see snippet) and then create a <List> of param objects, populated with the param name [from the db record], the type [also from the db record] and the value [from the control - ie textbox, checkbox, etc].
using System;
using System.Collections.Generic;
using System.Text;
 
namespace DataTransferObjects
{
    public class DbParam
    {
        private string parmName;
        private char parmType;
        private object parmVal;
 
        public DbParam(string pParmName, char pParmType, object parmVal)
        {
            ParmName = pParmName;
            ParmType = pParmType;
            ParmVal = parmVal;
        }
 
        public string ParmName
        {
            get { return parmName;}
            set { parmName = value;}
        }
 
        public object ParmVal
        {
            get { return parmVal; }
            set { parmVal = value; }
        }
 
        public char ParmType
        {
            get { return parmType; }
            set { parmType = value; }
        }
 
    }
}

Open in new window

0
 
p_davisCommented:
so what's wrong with that approach? seems like if it fits all your needs it would be a good idea. and with creating your own object you won't be taking a performance hit of any kind.
0
 
williamcampbellCommented:
The powers that be are narrow minded :)

 With LINQ and XML this would be a snip using XElement

But as p_davis says your build a list of Params and pass those is probably the easiest at this stage.

(You could also dynamically build the Stored Proc on the client and pass that as a string, save server time)
0
 
allanmarkAuthor Commented:
Thanks, guys!

Going to take a coffee break and then get onto it. Will post back, later.
0
 
allanmarkAuthor Commented:
Ok. So far so good. I now have a list of parameter objects.

Next step is to set up some sort of structure for the dat taht comes back from the stored procedure (which ever one that might be).

Since I have no idea how many fields are being returned and also, at this stage, am not interested in the field types, would the following (see snippet) suffice as the basis for receiving the data.
List<object[]> myRecords = new List<object[]> ;
 
object[] returnedFields;
 
sqlConn.Open();
SqlDataReader drResults = sqlGetResults.ExecuteReader();
while (drResults.Read())
{
  returnFields = null;
  for (int i = 0; i < drResults.FieldCount; i++)
  {
     returnedFields[i] = drResults.getValue(i);
  }
  myRecords.Add(returnedFields);
}

Open in new window

0
 
williamcampbellCommented:
Would the below work...
while (drResults.Read())
{
     myRecords.Add(drResults.Clone());
}

Open in new window

0
 
p_davisCommented:
i believe you would have to new up the  object array item

returnedFields[i] = new Object();
returnedFiedls[i] = drREsults.GetValue(i);
0
 
allanmarkAuthor Commented:
100%!!  Many thanks!!
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now