2016-04-01 12 views
0

私は現在、ビジュアルスタジオ2015とSQLサーバーのビジネス社会向けの小さなプロジェクトに取り組んでいます。私は2つのコンボボックス "AccountCode"と "AccountNo"を持っています。 "AccountNo"コンボボックスを "AccountCode"ドロップダウンからのテキストの選択に対応してその値を変更するようにしたいと思います。しかし、私は次のエラーを取得する:コンボボックスをドロップダウンする方法は、別のコンボボックスに対応してそのリストを変更しますか?

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: The multi-part identifier "System.Data.DataRowView" could not be bound.

private void cmBxAccountCode_DataEntry_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     cmBxAccountNo_DataEntry.ResetText(); 
     cmBxAccountNo_DataEntry.Refresh(); 
     DataSet ds = new DataSet(); 
     da.SelectCommand = new SqlCommand("SELECT ID,ACCOUNTNO FROM AccountHolder WHERE ACCOUNTCODE =" + cmBxAccountCode_DataEntry.Text, SQLConnection.con); 
     da.Fill(ds); 
     cmBxAccountNo_DataEntry.DataSource = ds.Tables[0]; 
     cmBxAccountNo_DataEntry.DisplayMember = "ACCOUNTNO"; 
     cmBxAccountNo_DataEntry.ValueMember = "ID"; 
    } 

答えて

0

あなたのクエリは次のようにする必要があります:

SELECT ID,ACCOUNTNO FROM AccountHolder WHERE ACCOUNTCODE ='" + cmBxAccountCode_DataEntry.Text + "'" 

が、私は強くあなたが注入を回避するためにパラメータ化クエリを使用することをお勧めします。また、Textの代わりに選択した値を取る(ただし、データベースの値による)。パラメータ化されたクエリは、次のようになります。

// Code here 
SqlCommand selectCommand= new SqlCommand("SELECT ID,ACCOUNTNO FROM AccountHolder WHERE ACCOUNTCODE [email protected]", SQLConnection.con); 
selectCommand.Parameters.Add("@accountCode", SqlDbType.VarChar).Value = cmBxAccountCode_DataEntry.Text; 
// execute here 
+0

ありがとう@ un-lucky。私はそれを得て、私の問題は解決した! :) –

関連する問題