有人可以帮我解决这个错误 [英] Can some one help me with this Error

查看:66
本文介绍了有人可以帮我解决这个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:

将varchar数据类型转换为日期时间数据类型会导致超出范围的值。该声明已被终止





Compose背后的代码是



 受保护  void  btnSubmit_Click( object  sender,EventArgs e)
{
try
{
string date,messagefrom,messageto,message;
int employeeid;
date = DateTime.Now.ToString();
messagefrom = 来自;
messageto = dropTo.SelectedItem.Text;
message = txtMessage.Text;
DataTable dt = BusinessAccessLayer.Query( 从员工中选择employeeid,其中name =' + messageto + ');
if (dt.Rows.Count > 0
{
employeeid = Convert.ToInt32(dt.Rows [ 0 ] [ employeeid]);
string sqlstr = 插入消息值(' + date + ',' + messagefrom + ',' + messageto + ',' + message + ', + employeeid + ,1);
BusinessAccessLayer.NonQuery(sqlstr);
}
Response.Redirect( managermessageoutbox.aspx);
}
catch (例外情况)
{
lblError.Text = 错误: + ex.Message;
}
}

解决方案

我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



您的问题i_s数据依赖,但没有提供数据。

调试器将允许您查看尝试转换为DateTime的值。这是解决问题的第一步。

一旦你告诉我们这个值,我们就能弄清楚出了什么问题。


问题在这里< pre lang =c#> date = DateTime.Now.ToString();





用于调试目的将此行更改为此



date = '01 -Jan-2016';



和re-执行代码,您将看到它已成功执行并且已在消息表中添加了一行。



现在您的任务是了解有什么区别先前和新的更改:)


Quote:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated



The Code behind Compose message is

protected void btnSubmit_Click(object sender, EventArgs e)
   {
       try
       {
           string date, messagefrom, messageto, message;
           int employeeid;
           date = DateTime.Now.ToString();
           messagefrom = from;
           messageto = dropTo.SelectedItem.Text;
           message = txtMessage.Text;
           DataTable dt = BusinessAccessLayer.Query("select employeeid from employees where name='" + messageto + "'");
           if (dt.Rows.Count > 0)
           {
               employeeid = Convert.ToInt32(dt.Rows[0]["employeeid"]);
               string sqlstr = "insert into Messages values('" + date + "','" + messagefrom + "','" + messageto + "','" + message + "'," + employeeid + ",1)";
               BusinessAccessLayer.NonQuery(sqlstr);
           }
           Response.Redirect("managermessageoutbox.aspx", false);
       }
       catch (Exception ex)
       {
           lblError.Text = "Error: " + ex.Message;
       }
   }

解决方案

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Your problem i_s data dependent, but didn't provide the data.
The debugger will allow you to see what is the value you try to convert to DateTime. This is the first step to solution.
Once you tell us this value, we will be able to figure out what is wrong.


The problem is here

date = DateTime.Now.ToString();



for debugging purpose change this line to this

date = '01-Jan-2016';

and re-execute the code, you will see that it gets successfully executed and a row has been added in the message table.

Now its your task to understand what is the difference between the previous and new changes :)


这篇关于有人可以帮我解决这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆