在我的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