标识符预期。

时间:2016-05-25 13:16:39

标签: c# asp.net .net entity-framework

此代码为我提供了预期的#34;标识符"信息。我突出了有问题的行(BirthDate.Text):

// Add user data to DB.
AddUser myUser = new AddUser();
bool addSuccess = myUser.AddUsr
(
    UserName.Text,
    PrefUserName.Text,
    OtherNames.Text,
    MaritalStatus.SelectedValue,
    SpouseName.Text,
    **BirthDate.Text,**
    Profession.Text,
    PostalAddress.Text,
    ResAddress.Text,
    Email.Text,
    PhoneNumber.Text,
    Church.Text,
    Gender.SelectedValue,
    HomeTown.Text,
    Area.Text,
    City.Text,
    Region.Text,
    Country.Text,
    Approved.Checked,
    ApprovedBy.Text,
    **DateCreated.Text,**
    RoleName.SelectedValue,
    Photos.FileName
);

这是类代码本身:

using System;
using MemberMgr.Models;
namespace MemberMgr.Logic
{
    public class AddUser
    {
        public bool AddUsr
        (
            string UserName, 
            string PrefUserName, 
            string OtherNames, 
            string MaritalStatus, 
            string SpouseName, 
            DateTime BirthDate, 
            string Profession, 
            string PostalAddress, 
            string ResAddress, 
            string Email, 
            string PhoneNumber, 
            string Church, 
            string Gender, 
            string HomeTown, 
            string Area, 
            string City, 
            string Region, 
            string Country, 
            bool Approved, 
            string ApprovedBy, 
            DateTime DateCreated, 
            string RoleName, 
            string Photos
        )
        {
            var myUser = new IdentityModels();
            myUser.UserName = UserName;
            myUser.PrefUserName = PrefUserName;
            myUser.OtherNames = OtherNames;
            myUser.MaritalStatus = MaritalStatus;
            myUser.SpouseName = SpouseName;
            myUser.BirthDate = BirthDate;
            myUser.Profession = Profession;
            myUser.PostalAddress = PostalAddress;
            myUser.ResAddress = ResAddress;
            myUser.Email = Email;
            myUser.PhoneNumber = PhoneNumber;
            myUser.Church = Church;
            myUser.Gender = Gender;
            myUser.HomeTown = HomeTown;
            myUser.Area = Area;
            myUser.City = City;
            myUser.Region = Region;
            myUser.Country = Country;
            myUser.Approved = Approved;
            myUser.ApprovedBy = ApprovedBy;
            myUser.DateCreated = DateCreated;
            myUser.RoleName = RoleName;
            myUser.Photos = Photos;

            using (IdentityUserContext _db = new IdentityUserContext())
            {
                // Add User data to DB.
                _db.users.Add(myUser);
                _db.SaveChanges();
            }
            // Success.
            return true;
        }

    }
}

我的高级程序员在那里,请帮助我。我知道TextBoxes接受字符串数据类型,它们的内容应该是Text。我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:0)

DateCreatedBirthDate类型为DateTimeBirthDate.TextDateCreated.Text类型为string。您需要将string转换为DateTime,例如用:

DateTime.Parse(DateCreated.Text)

或者格式是否已知:

DateTime.ParseExact(DateCreated.Text, "dd.MM.yyyy",
    System.Globalization.CultureInfo.InvariantCulture);

答案 1 :(得分:0)

您的BirthDate属性实际上定义为DateTime对象而不是字符串,因此您需要从TextBox中显式解析Date,以便通过{{3 }或DateTime.Parse()方法:

bool addSuccess = myUser.AddUsr
(
     /* Omitted for brevity */
     DateTime.Parse(BirthDate.Text),
     /* Omitted for brevity */
);

或者如果您明确知道它将传入的格式,您可以使用:

// You can define a formatting string like "MM/dd/yyyy" that cooresponds to the format that
// you know your TextBox will use
DateTime.ParseExact(BirthDate.Text,"{format-string}",null);

同样的问题适用于您的DateCreated属性,该属性也希望接收DateTime对象,而不是您当前传入的字符串