2016-08-03 2 views
0

, 나는 형 'System.Data.MissingPrimaryKeyException'의 예외가 system.data.dll에서 발생미스 기본 키는

오류를 얻을 사용자 코드에서 처리되지 않았습니다.

이유를 모르거나 수정하는 방법을 모르겠습니다.

string csv_file_path = @"C: \Users\xxx\Desktop\xxxx.csv"; 

DataTable csvData; 
..... 

DataColumn[] primaryKeys; 

primaryKeys = csvData.PrimaryKey; 
..... 

private void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) 
{ 
    if (args.RawSignalStrengthInDBm > -100) 
    { 
     if (csvData.Rows.Find(args.BluetoothAddress.ToString()) != null) 
        // got the error 
     { 
      DataRow infoRow = csvData.Rows.Find(args.BluetoothAddress.ToString()); 

      if (infoRow[1] != null) 
      { 
       // Display Location information 
       this.Dispatcher.Invoke((Action)(() => 
           { textBox.Text = ("Location: " + infoRow[2].ToString() + ", Time: " + DateTime.Now.ToString("h:mm:ss tt")).ToString(); })); 

      } 
      else 
      { 
       // record other information 
       ........ 
      } 
     } 
    } 
} 
+0

예외를 던지고있는 라인을 지적 할 수 있습니까? – RBT

+0

if (csvData.Rows.Find (args.BluetoothAddress.ToString())! = null). 이 줄에 오류가 있습니다. – QuickLearner

답변

1

데이터 테이블에서 기본 키로 간주해야하는 열 또는 열 그룹을 설정하지 않았습니다. 당신이 그것을 할 수있는 방법은 다음과 같습니다 :

DataTable csvData = new DataTable(); 
    DataColumn idCol = new DataColumn("Id",typeof(int)); 
    //column should be already added into the column list of datatable before setting it as primary key 
    csvData.Columns.Add(idCol); 
    //set the primary key. Here you can add multiple columns as it is a array property. This fixes your error 
    csvData.PrimaryKey = new[] { idCol }; 

    DataColumn[] primaryKeys; 

    primaryKeys = csvData.PrimaryKey; 
    //if you do not set the primary key the below count was earlier coming out to be zero. 
    var count = primaryKeys.Length; 
1

MissingPrimaryKeyException이 테이블 하지에 기본 키가 수행 할 때 발생합니다 : 나는 오류를 가지고 선은

if (csvData.Rows.Find(args.BluetoothAddress.ToString()) != null) 

내 전체 코드입니다. 예를 아래와 같이 DataTable에 정의 당신이 필요 DataRowCollectionPrimaryKeyFind을하기 위해

는 기본 키를 정의하려고합니다.

DataColumn column1=... ; // Table column 
csvData.PrimaryKey = new DataColumn[] {column1}; 
관련 문제