Link to home
Start Free TrialLog in
Avatar of satmisha
satmishaFlag for India

asked on

How to Pass object having values to Sql store procedure using Asp.Net MVC C#

Hi Experts,

I have an object containing lots of public values ( dropDown,checkbox, radioButton, txtBox etc..) that I want to send across to sql store procedure. Could you please provide some sample code to achieve this in ASP.Net MVC using c# ?

I know there is simple way exists where you can pass on different parameters that I want to avoid.
Avatar of satmisha
satmisha
Flag of India image

ASKER

Just to add that the object that I have used is of Type 'System.Collections.Generic.List'. Appreciate your early reply.
Avatar of ste5an
I have an object containing lots of public values [..]
Bad design.

[..] I want to send across to sql store procedure [..]
Why? A relational database is not an object store. So this is not possible.

Thus:
What is your use-case? Any actual problem?

But, your wording sounds like you may have already some serious architectural problems..
Problem unclear

Do you want to pass entire controls to the database or just the values?
Why do you not want to pass different parameters?

Also to point out, that what ste5an is mentioning is absolutely correct. This is bad design.

From what I am understanding, this is the what you are expecting.

SqlCommand sqlcmd = new SqlCommand();

            List<string> param = new List<string>();

            foreach (var item in param)
            {
                sqlcmd.Parameters.Add(item);
            }

Open in new window


Note: For this to work, the sequence of the sql parameters and the values in the Generic list should match.
Thanks experts and apologies for putting less info. Let me try to put full perspective now.

Yes, Architecture is extremely poor of this as this is migration application.

Lets say you have an 'Employee' Model in MVC application and this Employee Model is consist of many values like

Class Employee
{
      Name
      Code
      Region
      Country
      Salary
}

In db you have table called  StorageTable having following columns

ID
Feature
FeatureValue

So what I am expecting is to push Employee model mentioned above in the StorageTable, so my final table would become like:

ID      Feature      FeatureValue
1      Name      Tom
1      Code      101
1      Region      Ch
1      Country      USA
1      Salary       20,000
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
aha, an EAV model. How do you read your entities (Employee)?
Just a note:
The data model containing
ID
Feature
FeatureValue
seems to be universal for any data but you have to also think about data queries from such model. Such queries is not easy to optimize and if you decide to have each feature value in its own column on the query output then it results in a bunch of LEFT JOINS.

Of course if we are talking about thousands of rows then the speed does not matter but millions of rows is something different.

And that was the reason ste5an is asking "How do you read your entities (Employee)?".

So we may continue in the discussion or close the question.
Thanks guys.