2014-01-27 4 views
1

내 BloodType 콤보 상자 속성 모음입니다. O, A 및 C 인 3 가지 값이 있습니다. 그러나 원하는 값의 옵션을 선택한 후 제출을 클릭하면이 오류가 발생합니다.콤보 박스의 값을 데이터베이스에 추가하는 방법은 무엇입니까?

매개 변수화 된 쿼리 '(@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

정지 기준은 개체의 인스턴스로 설정되지 개체.

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에서 A를 제공합니다. @Plue – Pony

+0

이 코드 줄이어야합니다. // 기본값을 전달합니다 (예 : A ). updateCmd.Parameters.AddWithValue ("@pBloodType", "A"); . 그것을 다시 쓰는 방법? @Plue – Pony

+0

변경 ** string bloodtype = cbpBloodType.Text; ** ** string bloodtype = cbpBloodType.SelectedValue; ** – Plue

0

Mybe cbpBloodType.SelectedValue의 경우는 null

enter image description here

. 드롭 다운리스트를 어떻게 묶고 있습니까?

는 널 (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를 호출 할 때 오류가 발생합니까? –

관련 문제