简单 LINQ、C# .Net 网页上的 SqlDateTime 溢出错误

发布于 2024-08-25 01:15:34 字数 1345 浏览 2 评论 0原文

我正在创建一个简单的页面,仅使用 C# 从 .Net 中的文本框中插入一些数据。我收到溢出错误,指出日期必须在特定范围内。在 txtBirthdate 框中输入的文本类似于:01/01/1980。

调试时,Client1 _Birthdate 对象显示 {1/1/1980 12:00:00}。据我所知,它正在做它应该做的事情。预先感谢您的任何帮助。

protected void Button1_Click(object sender, EventArgs e)
{
    DataClasses1DataContext db = new DataClasses1DataContext();

    Client client1 = new Client
    {
        FirstName = txtFirstName.Text.ToString(),
        LastName = txtLastName.Text.ToString(),
        MiddleInitial = Convert.ToChar(txtMI.Text),
        Alias = txtAlias.Text.ToString(),
        Address = txtAddress.Text.ToString(),
        City = txtCity.Text.ToString(),
        State = txtState.Text.ToString(),
        Zip = Convert.ToInt32(txtZip.Text),
        Phone = txtPhone.Text.ToString(),
        Birthdate = Convert.ToDateTime(txtBirthdate.Text.ToString()),
        SSN = Convert.ToInt32(txtSSN.Text),
        DLNumber = txtDLNumber.Text.ToString(),
        Gender = Convert.ToByte(ddGender.Text),
        PrimaryRace = Convert.ToByte(ddPrimaryRace.Text),
        SecondaryRace = Convert.ToByte(ddSecondaryRace.Text),
        Ethnicity = Convert.ToByte(ddEthnicity.Text),
        Veteran = Convert.ToBoolean(ddVeteranStatus.Text),
        HoH = Convert.ToBoolean(ddHoH.Text)
    };

    db.Clients.InsertOnSubmit(client1);
    db.SubmitChanges();
}

I have a simple page I'm creating that just inserts some data from textboxes in .Net using C#. I get the overflow error stating the date must be within a specific range. The text being entered in the txtBirthdate box would be something like: 01/01/1980.

When debugging, the Client1 _Birthdate object shows {1/1/1980 12:00:00}. So as far as I can tell it's doing what it's supposed to. Thanks in advance for any help.

protected void Button1_Click(object sender, EventArgs e)
{
    DataClasses1DataContext db = new DataClasses1DataContext();

    Client client1 = new Client
    {
        FirstName = txtFirstName.Text.ToString(),
        LastName = txtLastName.Text.ToString(),
        MiddleInitial = Convert.ToChar(txtMI.Text),
        Alias = txtAlias.Text.ToString(),
        Address = txtAddress.Text.ToString(),
        City = txtCity.Text.ToString(),
        State = txtState.Text.ToString(),
        Zip = Convert.ToInt32(txtZip.Text),
        Phone = txtPhone.Text.ToString(),
        Birthdate = Convert.ToDateTime(txtBirthdate.Text.ToString()),
        SSN = Convert.ToInt32(txtSSN.Text),
        DLNumber = txtDLNumber.Text.ToString(),
        Gender = Convert.ToByte(ddGender.Text),
        PrimaryRace = Convert.ToByte(ddPrimaryRace.Text),
        SecondaryRace = Convert.ToByte(ddSecondaryRace.Text),
        Ethnicity = Convert.ToByte(ddEthnicity.Text),
        Veteran = Convert.ToBoolean(ddVeteranStatus.Text),
        HoH = Convert.ToBoolean(ddHoH.Text)
    };

    db.Clients.InsertOnSubmit(client1);
    db.SubmitChanges();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑着哭最痛 2024-09-01 01:15:34

格雷瓜尔说得完全正确。我回去查看,发现有一个日期戳,但也有一个在 SQL 中为 getdate() 设置的默认值。我认为我仍然可以插入而不必传递日期戳值。显然不是。我选择使用 DateTime.Now 从 C# 代码中填充它,无论如何,这实际上更好!

Gregoire was exactly right. I went back and looked and I had a datestamp but also had a default value set in SQL for getdate(). I thought I could still insert without having to pass the datestamp value. Apparently not. I opted to fill it from the C# code using the DateTime.Now which is actually better anyway!

失去的东西太少 2024-09-01 01:15:34

如果要将日期时间插入 SQL-Sever,您可能需要检查其范围,因为 C# DateTime.MinValue 与 SQL DateTime.MinValue 不同。

C# DateTime MinValue = 1/1/0001

SQL DateTime MinValue = 1/1/1753

另外

SQL SmallDateTime MinValue = 1/1/1900

C# DateTime 是不可为 null 的变量类型。因此,在插入 DAL 中的表之前,请确保它不为空。如果为 NULL,则将使用 MinValue(或存储在内存中的任何随机值)。

If you are about to insert your datetime into SQL-Sever, you will probably need to check for its range because C# DateTime.MinValue is different from SQL DateTime.MinValue.

C# DateTime MinValue = 1/1/0001

SQL DateTime MinValue = 1/1/1753

Also

SQL SmallDateTime MinValue = 1/1/1900

C# DateTime is a non-nullable variable type. So make sure it's not null before inserting into your table in your DAL. If NULL, the MinValue (or whatever random value stored in memory) will be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文