2014-01-27 30 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時發生錯誤? –

相關問題