C #: passing dataTable to stored procedure: problem with column order

I have a stored procedure that takes a user - defined table type called SeasonTable, which looks like this:

Seasontable

    [SeasonId] [int] NOT NULL,
    [SeasonName] [varchar](50) NOT NULL,
    [Month] [int] NULL, 
    [IsDeleted] [bit] NOT NULL

The saved procedure looks like below

CREATE PROCEDURE [dbo].[udpTest] 
(
    @STable dbo.SeasonTable READONLY
)   
AS
BEGIN
SELECT [SeasonName] as SeasonName,[SeasonId],[Month],[IsDeleted] 
    FROM @AdminBillingSeasonsTable
END

When I call this from a C # application

private DataTable TestCreateDataTable()
{
            DataTable dt = new DataTable();

            dt.Columns.Add("SeasonName", typeof(string));
            dt.Columns.Add("SeasonId", typeof(int));                
            dt.Columns.Add("Month", typeof(int));
            dt.Columns.Add("IsDeleted", typeof(bool));
            dt.Rows.Add("season1",1, 4,  false);
            dt.Rows.Add("season2",2, 9, false);
            dt.Rows.Add("season3",3, 11,  false);
            return dt;
}

When I use the above table as a parameter for SP from a C # application, it throws an error:

cannot convert "season1" to int.

Does this mean that the column order in a C # application should be the same as the column order in SQL?

Any help in this regard is much appreciated.

Thanks in advance

+5
source share
1 answer

 private DataTable TestCreateDataTable()
            {
                DataTable dt = new DataTable();

                dt.Columns.Add("SeasonId", typeof(int));     // seasonId and then
                dt.Columns.Add("SeasonName", typeof(string)); // seasonName
                dt.Columns.Add("Month", typeof(int));
                dt.Columns.Add("IsDeleted", typeof(bool));
                dt.Rows.Add("season1",1, 4,  false);
                dt.Rows.Add("season2",2, 9, false);
                dt.Rows.Add("season3",3, 11,  false);
                return dt;
            }
+5

All Articles