简单 LINQ、C# .Net 网页上的 SqlDateTime 溢出错误
我正在创建一个简单的页面,仅使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
格雷瓜尔说得完全正确。我回去查看,发现有一个日期戳,但也有一个在 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!
如果要将日期时间插入 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.