日期:2014-05-20  浏览次数:20855 次

asp.net查询出错,大神帮解决呀!
 protected void Button3_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(TextBox1.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "",
                "<Script Language=JavaScript>alert('您的输入有误,请重新输入!')</Script>");
        }
        else
        {
            showup1.Style["Display"] = "Block";
            showup2.Style["Display"] = "Block";
            showup3.Style["Display"] = "Block";
            showup4.Style["Display"] = "Block";
            showup5.Style["Display"] = "Block";
            Label4.Visible = true;
            //读取时间信息

            string connstr = "Data Source=.\\SQLEXPRESS;Initial Catalog=db_sun;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connstr);
            connection.ConnectionString = connstr;
            {
                connection.Open();
                String sqlread = "select UL_Data from DT where ID=TextBox1.Text.Trim())";
                SqlCommand command = new SqlCommand(sqlread, connection);
                SqlDataReader dateread = command.ExecuteReader();
                if (dateread.HasRows)
                {
                    while (dateread.Read())
                    {
                        Text2.Text = dateread.GetInt32(1).ToString();
                    }
                    dateread.Close();
                }
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "",
                "<Script Language=JavaScript>alert('数据库连接出现错误,请重试!')</Script>");
            }
            finally
            {
                connection.Close();
            }
        }

      可以运行,调试,但单击按钮后只会弹出'数据库连接出现错误,请重试!',实在不知道哪出错了,哪位路过的大神给指教下,谢谢啦!
    
------解决方案--------------------
String sqlread = "select UL_Data from DT where ID=TextBox1.Text.Trim()"; 
这句最后多了个中文的右括号)
所以肯定会出错

另外你catch里应该把错误消息弹出来,这样才能一目了然知道哪儿出错了
------解决方案--------------------
引用:
String sqlread = "select UL_Data from DT where ID=TextBox1.Text.Trim())"; 
这句最后多了个中文的右括号)
所以肯定会出错

这样的字符串就是错的,不能得到文本框的值
------解决方案--------------------
不要写try...catch。只有最终发布的程序,才需要统一在系统某个最外层捕获异常。在你调试,以及发布到客户那里的beta版中,不要有try...catch。要让异常信息原原本本地打印到网页上,直接显示具体出错的代码行号。

使用正确高效率的软件工程开发策略,才能方便有利于开发。如果开发过程中写什么try...catch来自欺欺人地对自己隐瞒异常信息,这对程序员来说反而是荒唐和幼稚的。