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

新手 求助求助 C# 遇到点小问题 求各位大大帮忙
今天编写了C#的添加学生信息的窗体, 运行时 加上 try-catch 不会跳出,但不会执行添加学生信息的代码 不加 try - catch 则会在 int count = (int)comm.ExecuteNonQuery(); 这里报错,真心求大大们帮忙,小生在此谢过。


代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
  public partial class HackStuInfo : Form
  {
  public HackStuInfo()
  {
  InitializeComponent();
  }

  DBHelper db = new DBHelper();

  private void gb_2_Enter(object sender, EventArgs e)
  {

  }

  private void gb2_cb1_SelectedIndexChanged(object sender, EventArgs e)
  {

  }

  private void gb_1_Enter(object sender, EventArgs e)
  {

  }

  private void HackStuInfo_Load(object sender, EventArgs e)
  {
  BindGrade();
   
   

  }

  //填充年级数据

  public bool BindGrade()
  {
  try
  {

  //年级选择框默认
  gb2_cb1.SelectedIndex = 0;

  //向年级选择框填充数据
  string sql = "select COUNT(*) as 人数 ,StuGrade from Student Group by StuGrade";

  SqlCommand comm = new SqlCommand(sql, db.Connection);

  db.OpenConnection();
  SqlDataReader reader = comm.ExecuteReader();
  while (reader.Read())
  {
  string gradeid = reader["StuGrade"].ToString();
  this.gb2_cb1.Items.Add(gradeid);
  }
  reader.Close();
  return true;

  }
  catch (Exception)
  {

  MessageBox.Show("系统发生错误");
  return false;
  }
  }

  //验证编辑输入
  public bool CheckInput()
  {
  try
  {
  //获得输入的值
  #region
  string Pwd = gb_1_tb2.Text.Trim();
  string Pwd2 = gb_1_tb3.Text.Trim();
  Name = gb2_tb1.Text.Trim();
  string Sex;
  string Grade = gb2_cb1.Text.Trim();
  string Phone = gb2_tb2.Text.Trim();
  string Address = gb2_tb3.Text.Trim();
  string Email = gb2_tb4.Text.Trim();

  //判断返回的男女值
  if (this.gb_2_rb1_male.Checked)
  {
  Sex = "男";
  }
  else
  {
  Sex = "女";
  }

  #endregion


  //判断输入
  #region
  if (Pwd.Equals(string.Empty) && Name.Equals(string.Empty) &&
  Grade.Equals(string.Empty) && Address.Equals(string.Empty))
&nb