データベースを更新するための次のコードがあります。更新は正常に機能しますが、データ グリッド ビューを更新しようとすると、プログラムは「条件の例外でデータ型の不一致」をスローします。コードは次のとおりです。
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();
}
どうすればこれを修正できますか。ありがとう
編集:構文を修正することで解決