在我的BloodType組合框屬性的集合中。我有3個值,即O,A和C.但是在選擇了我想要的值的選項後,點擊submit我得到這個錯誤。如何在數據庫中添加組合框的值?
的參數化查詢 '(@pFirstName nvarchar的(5),@ pLastName nvarchar的(6),@ pContact nvarch' 預計參數 '@pBloodType',但未提供。
如果使用將工作我的血型,而不是一個文本框
對於我的文本框的代碼是前
updateCmd.Parameters.AddWithValue(「@ pBloodType」,txtpBloodType.Text);
對於組合框的代碼,我使用現在,
updateCmd.Parameters.AddWithValue(「@ pBloodType」,cbpBloodType.SelectedValue);
靜止物體引用不設置爲一個對象的一個實例。
public patient()
{
InitializeComponent();
this.cbpBloodType = new System.Windows.Forms.ComboBox();
this.cbpBloodType.Items.Add("O");
this.cbpBloodType.Items.Add("A");
this.cbpBloodType.Items.Add("C");
}
private int AddPatientRecord()
{
int result = 0;
// TO DO: Codes to insert customer record
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
String strCommandText = "INSERT PATIENT(pFirstName, pLastName, pContact, pAddress, pCity, pZip, pNationality, pRace, pIC, pGender, pDOB, pBloodType, pEmail) "
+ " VALUES (@pFirstName,@pLastName,@pContact,@pAddress,@pCity,@pZip,@pNationality, @pRace, @pIC, @pGender, @pDOB, @pBloodType, @pEmail)";
SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);
updateCmd.Parameters.AddWithValue("@pFirstName", txtpFirstName.Text);
updateCmd.Parameters.AddWithValue("@pLastName", txtpLastName.Text);
//updateCmd.Parameters["@clientid"].Direction = ParameterDirection.Output;
updateCmd.Parameters.AddWithValue("@pContact", txtpContact.Text);
updateCmd.Parameters.AddWithValue("@pAddress", txtpAddress.Text);
updateCmd.Parameters.AddWithValue("@pCity", txtpCity.Text);
updateCmd.Parameters.AddWithValue("@pZip", txtpZip.Text);
updateCmd.Parameters.AddWithValue("@pNationality", txtpNationality.Text);
updateCmd.Parameters.AddWithValue("@pRace", txtpRace.Text);
updateCmd.Parameters.AddWithValue("@pIC", txtpIC.Text);
if (rbMale.Checked)
{
updateCmd.Parameters.AddWithValue("@pGender", "M");
}
else
{
updateCmd.Parameters.AddWithValue("@pGender", "F");
}
updateCmd.Parameters.AddWithValue("@pDOB", dtppDOB.Value);
string value = cbpBloodType.SelectedItem == null ? "A" : cbpBloodType.SelectedItem.ToString();
updateCmd.Parameters.AddWithValue("@pBloodType", value);
updateCmd.Parameters.AddWithValue("@pEmail", txtpEmail.Text);
// STEP 3 open connection and retrieve data by calling ExecuteReader
myConnect.Open();
// STEP 4: execute command
// indicates number of record updated.
result = updateCmd.ExecuteNonQuery();
// STEP 5: Close
myConnect.Close();
return result;
}
你試圖使用'updateCmd.Parameters.AddWithValue( 「@ pBloodType」 ,cbpBloodType.SelectedValue.ToString());'?在值上調用ToString()方法? – AssaultingCuccos
您是否在運行代碼時檢查了調試器中是否存在cbpBloodType和cbpBloodType.SelectedValue? – Plue