I'm using a user-defined table type parameter to a stored procedure in SQL Server 2016 for inserting multiple records from ASP.NET MVC.
On debugging from Visual Studio 2013(local dev system), execution gets completed successfully but wherein when the same web page is accessed from a dev server url, it throws below error.
The table type parameter '@Customers' must have a valid type name.
I have explicitly given the type name in c# code. This logic works from local dev system but not after publishing to a dev site even though the site points to same database.
Can anyone please help on this?
.NET Framework : 4.5
SQL Server Version : 2016
User-defined table type:
- CREATE TYPE [dbo].[CustomerType] AS TABLE(
- [CustId] [int] NULL,
- [CustName] [varchar](100) NULL,
- [Country] [varchar](50) NULL
- )
Stored procedure:
- Create PROCEDURE spInsertCustomers
- @Customers dbo.CustomerType READONLY
- AS
- BEGIN
- SET NOCOUNT ON;
- INSERT INTO Customers
- SELECT * FROM @Customers
- END
- public ActionResult Index()
- {
- try
- {
- TestUDFTable();
- ViewBag.Message = "SUCCESS";
- }
- catch (Exception ex)
- {
- ViewBag.Message = ex.Message;
- }
- return View();
- }
- public void TestUDFTable()
- {
- string connectionString = string.Empty;
- connectionString = ConfigurationManager.ConnectionStrings["appcon"].ToString();
- DataTable dt = new DataTable();
- dt.Columns.Add("CustId");
- dt.Columns.Add("CustName");
- dt.Columns.Add("Country");
- int counter = 1;
- while (counter < 3)
- {
- DataRow row = dt.NewRow();
- row["CustId"] = counter;
- row["CustName"] = "Customer-" + counter;
- row["Country"] = "USA";
- dt.Rows.Add(row);
- counter++;
- }
- SqlParameter Parameter = new SqlParameter("@Customers", dt);
- Parameter.TypeName = "dbo.CustomerType";
- Parameter.SqlDbType = SqlDbType.Structured;
- using (var connection = new SqlConnection(connectionString))
- {
- var cmd = new SqlCommand();
- cmd.Connection = connection;
- cmd.Parameters.Add(Parameter);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.CommandText = "spInsertCustomers";
- connection.Open();
- cmd.ExecuteNonQuery();
- connection.Close();
- }
- }
Sri RamPosted Jan 18, 2018, 4:50 PM
Hi Dharmraj Thakur,
Thanks for ur kind reply..
Actually, i have implemented the same solution on SQL Server 2008 without this registering process, everything went fine. It was a straight-forward sql scripts execution for creating the UDT and using it with a SP.
Anyways, I will try this registering process and let you know..
Laxmidhar SahooPosted Jan 17, 2018, 10:32 AM
Dharmraj ThakurPosted Jan 17, 2018, 10:29 AM
Sri RamPosted Jan 17, 2018, 10:00 AM
Hi Amit, thanks for ur reply.
Actually, i had already included that datatype change in my code and missed out the same while posting the question. It still didn't work...
As per my understanding with the exception, SQL server procedure is not able to identify the parameter being passed as the intended user-defined table type, even though below code does that mapping.
I have tried this same solution on sql server 2008 and it works pretty charm. I'm not sure why this is causing the exception in sql 2016?
Do u have any thoughts?
Amit KumarPosted Jan 11, 2018, 11:30 PM
Sri RamPosted Jan 11, 2018, 2:42 PM
Laxmidhar SahooPosted Jan 11, 2018, 11:06 AM