2013-03-02 7 views
0

콤보 상자에서 선택한 항목에 따라 텍스트 상자를 채우려고합니다. 문제는로드시 다음 오류가 발생하고 visual studio에서 next..next를 누릅니다. 실제로 수행 할 작업을 수행합니다.콤보 상자가 선택되어서 양식로드시 오류가 발생했습니다. 인덱스

enter image description here 로드시 문제를 어떻게 해결할 수 있습니까? 양식 부하

코드는 콤보 상자의 선택된 인덱스에 대한

private void UpdateProduct_Load(object sender, EventArgs e) 
     { 
      DataSet ds = GetAllItems(); 
      comboBox2.DataSource = ds.Tables[0]; 
      comboBox2.DisplayMember = "Product Name"; 
      comboBox2.SelectedIndex = 0; 


     } 

코드입니다 난 단지 문제가

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      // string selectedText = this.comboBox2.GetItemText(this.comboBox2.SelectedItem); 
      DataSet d = GetProductInfo(comboBox2.Text); 
      if (d.Tables.Count > 0) 
      { 
       textBox2.Text = d.Tables[0].Rows[0]["Quantity"].ToString(); 
       textBox3.Text = d.Tables[0].Rows[0]["Color"].ToString(); 
       textBox4.Text = d.Tables[0].Rows[0]["Size"].ToString(); 
       textBox5.Text = d.Tables[0].Rows[0]["Price"].ToString(); 
      } 

     } 

때 처음으로 양식로드합니다.

GetProductInfo 코드

public DataSet GetProductInfo(string product) 
     { 
      DataSet dataSet = new DataSet(); 
      OleDbConnection oleConn = new OleDbConnection(connString); 

      try 
      { 
       oleConn.Open(); 
       string sql = "SELECT [Quantity], [Color], [Size], [Price] FROM [Product] WHERE [Product Name]= '" + product + "'"; 
       OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn); 
       dataAdapter.Fill(dataSet, "Product"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("An exception has been occured\n" + ex.ToString()); 
       Console.WriteLine(ex.ToString()); 
      } 
      finally 
      { 
       oleConn.Close(); 
      } 
      if (dataSet.Tables["Product"].Rows.Count <= 0) 
       return null; 

      return dataSet; 
     } 

스택 추적은

System.NullReferenceException occurred 
    HResult=-2147467261 
    Message=Object reference not set to an instance of an object. 
    Source=Purchase Management 
    StackTrace: 
     at Purchase_Management.UpdateProduct.comboBox2_SelectedIndexChanged(Object sender, EventArgs e) in c:\Users\Amrit\Desktop\Purchase Management\Purchase Management\UpdateProduct.cs:line 99 
    InnerException: 
+0

당신은'GetProductInfo()'에 대한 코드를 보여줄 수 있습니까? –

+0

(Visual Studio의 디버그 모드에서) 오류를 다시 복제하고 예외 설명 팝업이 표시되면 "예외 세부 정보를 클립 보드로 복사"링크를 클릭하십시오. 중간 단계 (메모장 등 사용)에서 세부 정보를 위조하여 회사에서 알려주지 못하는 비밀 정보를 아무도 알 수 없으면 해당 세부 정보에서 StackTrace (부분적으로 검열 된 경우) 섹션을 보내주십시오. –

답변

0

을 대신의 SelectedIndexChanged를 사용하지 않고, 이벤트를 SelectionChangeCommitted 사용합니다. 이렇게하면 사용자이 콤보 박스를 초기화하는 경우가 아니라 선택한 항목을 변경할 때만 발생하는 모든 문제를 피할 수 있습니다.

+0

어떻게 활성화 할 수 있습니까? SelectionChangeCommitted –

0

당신의 메서드는 null을 반환. 오류가 다음

if(dstProduct.Tables.Count>0 && dstProduct.Tables[0].Rows.Count >0) 
{ 
// then do stuff. 
} 
+0

'GetProductInfo'가'null'을 리턴해야한다면'dstProduct'에 저장된 이전의 모든 할당은'dstProduct'에 저장된 null이 아닌 처음 참조를 덮어 쓸 것이기 때문에 무의미합니다 –

0

.Count 때문에 화재 d = null의 경우에 exeption을 시도

DataSet dstProduct = new DataSet(); 
dstProduct = GetProductInfo(comboBox2.Text); 

를 던진 이유입니다.

0

DataSet d = new DataSet();는 그냥 if (!string.IsNullOrEmpty(productName)) 문을 추가 선언하고 당신은 괜찮을 것 :

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    var productName = comboBox2.Text; 
    if (!string.IsNullOrEmpty(productName)) { 

     DataSet d = GetProductInfo(productName); 
     if (d.Tables.Count > 0) 
     { 
      textBox2.Text = d.Tables[0].Rows[0]["Quantity"].ToString(); 
      textBox3.Text = d.Tables[0].Rows[0]["Color"].ToString(); 
      textBox4.Text = d.Tables[0].Rows[0]["Size"].ToString(); 
      textBox5.Text = d.Tables[0].Rows[0]["Price"].ToString(); 
     } 
    } 
} 
+0

fomr의로드에서 여전히 같은 오류가 발생했습니다 –

+0

업데이트를 확인하십시오. 1 번째로 가벼운 오류가 발생했습니다. –

+0

죄송하지만 여전히 같은 오류가 발생했습니다. –

관련 문제