日期:2014-05-18  浏览次数:20973 次

Button按钮突然单击无效
想实现按班级名称查询班级信息的功能,btnSearch开始单击挺正常,后来单击没反应,各种调试,新建按钮都无效,求高手指点?

C# code

       private void btnSearch_Click(object sender, EventArgs e)
        {
            //获取待搜索的信息
            string name = txtClassName.Text.Trim();
            //连接Sql server 数据库
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=gsInfo;Integrated Security=True");
            string sql = String.Format("select * from T_classInfo where 班级名称='{0}' ", name);
            try
            {
                con.Open();//打开与数据库的链接
                SqlCommand cmd = new SqlCommand(sql, con);//定义Command对象
                SqlDataReader reader = cmd.ExecuteReader();
                //string className = reader.GetString(reader.GetOrdinal("班级名称"));
                if (reader.Read())
                {
                    string classShortName = reader.GetString(reader.GetOrdinal("班级简称"));
                    string grade = reader.GetString(reader.GetOrdinal("年级"));
                    string classNo = reader.GetString(reader.GetOrdinal("班级编号"));
                    string classNumber = reader.GetString(reader.GetOrdinal("人数"));
                    string teacher = reader.GetString(reader.GetOrdinal("辅导员"));
                    txbClassName.Text = name;
                    txbShortName.Text = classShortName;
                    txbGrade.Text = grade;
                    txbClassNumber.Text = classNo;
                    txbNo.Text = classNumber;
                    txbTeacher.Text = teacher;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                con.Close();
            }



------解决方案--------------------
先MessageBox.Show("xxx");测试一下。。?

如果不行试着重启一下VS,
------解决方案--------------------
btnSearch_Click有没有绑定到按钮的事件

可能是SqlDataReader没有即使关闭的问题
try
{
con.Open();//打开与数据库的链接
MessageBox.Show("数据库打开正常");
SqlCommand cmd = new SqlCommand(sql, con);//定义Command对象
SqlDataReader reader = cmd.ExecuteReader();
//string className = reader.GetString(reader.GetOrdinal("班级名称"));
if (reader.Read())
{
string classShortName = reader.GetString(reader.GetOrdinal("班级简称"));
string grade = reader.GetString(reader.GetOrdinal("年级"));
string classNo = reader.GetString(reader.GetOrdinal("班级编号"));
string classNumber = reader.GetString(reader.GetOrdinal("人数"));
string teacher = reader.GetString(reader.GetOrdinal("辅导员"));
txbClassName.Text = name;
txbShortName.Text = classShortName;
txbGrade.Text = grade;
txbClassNumber.Text = classNo;
txbNo.Text = classNumber;
txbTeacher.Text = teacher;
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
con.Close();
}
------解决方案--------------------
SqlDataReader没有关闭的缘故
加上这个reader.Close();
------解决方案--------------------
感觉应该加上一个判断吧,读的时候为不为空,如果不为空在绑定啊,