Home > DeveloperSection > Forums > How to set TypeName in the constructor of new SqlParameter?
ben reitman
ben reitman

Total Post:96

Posted on    November-20-2014 10:07 PM

 MSSQL Server C#  ASP.Net  SQL Server 

 1 Reply(s)
 971  View(s)
Rate this:

I am using the following code to set up parameters for a call to a stored procedure:

        List<SqlParameter> parameterList = new List<SqlParameter>();

        parameterList.Add(new SqlParameter("@Title", adminTest.Title));

        parameterList.Add(new SqlParameter("@Text", adminTest.Text));

        var questionsList = new SqlParameter("@Questions", questions);

        questionsList.TypeName = "dbo.QuestionList";


The code snippet works but what I would like to know is if anyone found a way to set the TypeName in the new SqlParameter constructor? I tried looking at the documentation but the only thing I can find is adding the typename afterwards.

Alex Leblois
Alex Leblois

Total Post:67

Posted on    November-21-2014 12:13 AM

You can accomplish this with initializers. The example below specifies strongly-typed parameters and max length of variable values, which is a good practice from a SQL performance perspective.

    List<SqlParameter> parameterList = new List<SqlParameter>()


            new SqlParameter("@Title", SqlDbType.VarChar) {Size = 30, Value = adminTest.Title},

            new SqlParameter("@Text", SqlDbType.VarChar) {Size = 30, Value = adminTest.Text},

            new SqlParameter("@Questions", SqlDbType.Structured) {TypeName = "dbo.QuestionList", Value = questions}


Don't want to miss updates? Please click the below button!

Follow MindStick