更新datagridview时出现“数据类型不匹配异常”

时间:2012-12-25 22:56:06

标签: c# ms-access datagridview oledb

我有以下代码来更新我的数据库。更新工作正常,但当我尝试刷新数据网格视图时,程序抛出“标准异常中的数据类型不匹配” 这是代码

string gen = "";
        if (male_rdbtn.Checked)
            gen = "M";
        if (female_rdbtn.Checked)
            gen = "F";
        //Updates the database
        OleDbDataAdapter da = new OleDbDataAdapter();

        da.UpdateCommand = new OleDbCommand();
        da.UpdateCommand.Connection = GetConnection();
        da.UpdateCommand.CommandText =
           "UPDATE patients SET " +
           "firstlastname ='" + nametxt.Text +
           "', birthdate='" + birthtxt.Text +
           "', birthplace='" + birthcmb.SelectedItem +
           "', gender='" + gen +
           "', bloodtype='" + bloodcmb.SelectedItem +
           "', telnum='" + teltxt.Text +
           "', address='" + addresstxt.Text +
           "' WHERE patientid=" + txt;

        da.UpdateCommand.ExecuteNonQuery();

        da.UpdateCommand.Connection.Close();

        //My code to refresh data grid view
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = GetConnection();
        cmd.CommandText = "SELECT * FROM patients WHERE patientid = '"+ txt +"'";


        OleDbDataAdapter dad = new OleDbDataAdapter();
        dad.SelectCommand = cmd;

        DataSet ds = new DataSet();
        dad.Fill(ds, "patients");//mismatch exception is thrown here
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "patients";



        cmd.Connection.Close();



    }

我该如何解决这个问题。感谢

编辑:通过更正语法解决

0 个答案:

没有答案