简体   繁体   中英

DataTable not populating a datatype fully in sql server

I am having a very strange I am getting null values in a stored procedure that I've created.

I have created a type something like this:

CREATE TYPE [EAC].[PersonTableType] AS TABLE(
    [Title] [nvarchar](5) NULL,
    [FirstName] [nvarchar](20) NULL,
    [MiddleName] [nvarchar](20) NULL,
    [LastName] [nvarchar](25) NULL,
    [DisplayName] [nvarchar](50) NULL,
    [Suffix] [nvarchar](5) NULL,
    [WorkPhone] [nvarchar](25) NULL,
    [Extension] [nvarchar](8) NULL,
    [HomePhone] [nvarchar](25) NULL,
    [CellPhone] [nvarchar](25) NULL,
    [Pager] [nvarchar](25) NULL,
    [Fax] [nvarchar](25) NULL,
    [EMail] [nvarchar](75) NULL,
    [SecondaryEMail] [nvarchar](75) NULL,
    [Notes] [nvarchar](2000) NULL,
    [Department] [nvarchar](50) NULL,
    [Position] [nvarchar](120) NULL,
    [Building] [nvarchar](50) NULL,
    [Office] [nvarchar](50) NULL,
    [LicensePlateNumber] [nvarchar](20) NULL,
    [EmployeeId] [nvarchar](20) NULL,
    [Company] [nvarchar](50) NULL,
    [ExternalId] [nvarchar](400) NULL,
    [ZoneID] [int] NULL,
    [LastModifiedUTCOffset] [smallint] NULL
)

I want to say that the stored procedure works if I create a type and pass it in via SSMS. So I don't think that its the PROC .

I've also noticed that all of the fields are populated except for LastName.

foreach (var contract in people)
{
     var row = dataTable.NewRow();
     row["Title"] = contract. Title;
     row["FirstName"] = contract.FirstName ;
     row["MiddleName"] = contract.MiddleInitial ;
     row["LastName"] = contract.LastName ;
     row["DisplayName"] = contract.DisplayName ;
     row["Suffix"] = contract.Suffix ;
     row["WorkPhone"] = contract.WorkPhone ;
     row["Extension"] = contract.Extension ;
     row["HomePhone"] = contract.HomePhone ;
     row["CellPhone"] = contract.CellPhone ;
     row["Pager"] = contract.Pager ;
     row["Fax"] = contract.Fax ;
     row["EMail"] = contract.EmailAddress ;
     row["SecondaryEMail"] = contract.SecondaryEmailAddress ;
     row["Notes"] = contract.Notes ;
     row["Department"] = contract.Department ;
     row["Position"] = contract.Position ;
     row["Building"] = contract.Building ;
     row["Office"] = contract.Office ;
     row["LicensePlateNumber"] = contract.LicensePlateNumber ;
     row["EmployeeId"] = contract.EmployeeId ;
     row["Company"] = contract.Company ;
     row["ExternalId"] = contract.ExternalId ;
     row["ZoneID"] = contract.ZoneID ;
     row["LastModifiedUTCOffset"] = contract.LastModifiedUTCOffset ;


       dataTable.Rows.Add(row);
}

I actually have figured out the reason this happened. When you're passing in the data from the datatable the columns actually have to be in the same order as the type definition that you created.

Hope this helps others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM