Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing parameter of unknown type

Posted on 2009-04-10
11
Medium Priority
?
603 Views
Last Modified: 2013-12-17
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
Comment
Question by:allanmark
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 24117811
how about an object array?
0
 
LVL 22

Accepted Solution

by:
p_davis earned 1200 total points
ID: 24117843
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
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24117977
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:allanmark
ID: 24118080
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
 
LVL 22

Expert Comment

by:p_davis
ID: 24118108
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
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24118220
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
 

Author Comment

by:allanmark
ID: 24118441
Thanks, guys!

Going to take a coffee break and then get onto it. Will post back, later.
0
 

Author Comment

by:allanmark
ID: 24118809
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
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24119044
Would the below work...
while (drResults.Read())
{
     myRecords.Add(drResults.Clone());
}

Open in new window

0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 1200 total points
ID: 24119048
i believe you would have to new up the  object array item

returnedFields[i] = new Object();
returnedFiedls[i] = drREsults.GetValue(i);
0
 

Author Closing Comment

by:allanmark
ID: 31568998
100%!!  Many thanks!!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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