2011-07-05 3 views
1

リストボックス内の複数の値をデータベースに挿入したい。C#のリストボックスの値を渡して取得する.net Windowsアプリケーション

私はC#.netでWindowsアプリケーションを使用しています。

次のコードを試しましたが、動作しませんでした。

string ProductIDList; 
SqlCommand com = new SqlCommand("Select ProductID from Product_Category where CategoryID=" + Catid +"" , con); 
      dr = com.ExecuteReader(); 

      if (dr.HasRows) 
      { 
       while (dr.Read()) 


        if (ProductIDList == "") 
        { 
         ProductIDList = dr["ProductID"].ToString(); 
        } 
        else 
        { 
         ProductIDList = ProductIDList + "," + dr["ProductID"].ToString(); 
        } 

       ProductIDList = ProductIDList.Substring(1, ProductIDList.Length - 1); 

      } 
      dr.Close(); 

      var temparry = ProductIDList.Split(new string[] { "," }, StringSplitOptions.None); 

      foreach (ListItem li in listBox1.Items) 
      { 

       foreach (string s in temparry) 
       { 
        if (li.Value == s) 
         li.Selected = true; 
       } 
      } 

このコードでは、ForeachセクションのListItemはWindowsアプリケーションでは機能しません。 私はこの前にasp.netアプリケーションで行ったことがあります。 しかし、デスクトップ(Windows)アプリケーションではどうすればいいですか?

助けてください。 (; I < listBox1.Items.Count; I = 0をint型私は++)のため

答えて

0



{
BTW
(listbox1.Items [I] .ToString()== S)であれば、私はまた、希望いくつかのリファクタリングを見てください。あまりにも多くのループがそこに進んでいますか?

+0

回答ありがとうございました。でも、まだ値が1つしかありません。私はproductid 1,2,3を選択した場合、私は1,1,1を挿入挿入すると意味します。私が何をしているのですか? – Shikhar210289

+0

を挿入している間、あなたはlistbox1.SelectedItemsをループすることができます。 – hangar18

関連する問題