2014-01-27 34 views
1

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

enter image description here

静止物体引用不设置为一个对象的一个​​实例。

enter image description here

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; 

    } 
+0

你试图使用'updateCmd.Parameters.AddWithValue( “@ pBloodType” ,cbpBloodType.SelectedValue.ToString());'?在值上调用ToString()方法? – AssaultingCuccos

+0

您是否在运行代码时检查了调试器中是否存在cbpBloodType和cbpBloodType.SelectedValue? – Plue

回答

1

尝试在代码初始化你的组合框的背后:

public Form1() 
{ 
    InitializeComponent(); 
    cb1.Items.Add("O"); 
    cb1.Items.Add("A"); 
    cb1.Items.Add("C"); 
} 

然后访问数据cbpBloodType.SelectedItem.ToString();

string value = cbpBloodType.SelectedItem == null ? "A" : cbpBloodType.SelectedItem.ToString(); 
updateCmd.Parameters.AddWithValue("@pBloodType", value); 
+0

omg这工作,但无论我点击,它总是给我一个在datagrid @Plue – Pony

+0

必须是这行代码。 //通过默认值,例如A updateCmd.Parameters.AddWithValue(“@ pBloodType”,“A”); 。如何重写它? @Plue – Pony

+0

更改**字符串bloodtype = cbpBloodType.Text; **到**字符串bloodtype = cbpBloodType.SelectedValue; ** – Plue

0

Mybe cbpBloodType.SelectedValue返回null。你如何约束你的下拉列表?

试试这个

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); 
    if(!String.IsNullOrEmpty(cbpBloodType.SelectedValue.ToString())) 
    { 
     updateCmd.Parameters.AddWithValue("@pBloodType", cbpBloodType.SelectedValue.ToString()); 
    } 
    else 
    { 
     // Pass default value, for example A 
     updateCmd.Parameters.AddWithValue("@pBloodType","A"); 
    } 
    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; 

} 
+0

我得到这个对象引用没有设置为一个对象的实例通过您的代码@Mohammad jouhari – Pony

+0

所以现在,而不是写在属性集合中。我写的代码中的值是什么? @Mohammad jouhari – Pony

+0

你在哪里得到确切的对象引用未设置为对象的实例?你能调试吗? – user123456

0

不知道为什么空例外,但我敢打赌,你都可以从错误的值无论如何“选定值”。先分配变量,然后逐步完成。我敢打赌,你可能会发现你没有回到“A”或“B”,而是像“System.Windows.Controls ...”

试试这个,看看你是否得到相同的错误。

string bloodtype = cbpBloodType.Text; 

if(!String.IsNullOrEmpty(bloodtype)) 
+0

同样的错误。上面更新了代码和图片。 @ K.DW – Pony

+0

当您逐步完成它时,变量是否正确分配或者当您调用AddWithValue时发生错误? –

相关问题