2016-12-10 21 views
1

コードを実行すると、エラーは発生せず、コードは正しく実行されているように見えますが、データベースのデータはリストビューに表示されません。ここでローカルの.mdfのデータがリストビューに表示されない

は、私のデザイナーのフォームのコードは次のとおりです。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Configuration; 
using System.Data.SqlClient; 

namespace Cookbook 
{ 
    public partial class frmMain : Form 
    { 
     SqlConnection connection; 
     string connectionString; 

     public frmMain() 
     { 
      InitializeComponent(); 
      connectionString = ConfigurationManager.ConnectionStrings["Cookbook.Properties.Settings.CookbookConnectionString"].ConnectionString; 
     } 

     public void frmMain_Load(object sender, EventArgs e) 
     { 
      PopulateRecipes(); 

     } 

     public void PopulateRecipes() 
     { 
      using (connection = new SqlConnection(connectionString)) 
      using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Recipe", connection)) 
      { 
       DataTable recipeTable = new DataTable(); 
       adapter.Fill(recipeTable); 

       lstRecipes.DisplayMember = "Name"; 
       lstRecipes.ValueMember = "Id"; 
       lstRecipes.DataSource = recipeTable; 
      }  
     } 


     public void PopulateIngrediants() 
     { 
      string query = "SELECT a.Name FROM Ingrediants a" + 
      "INNER JOIN RecipeIngrediant b ON a.Id = b.IngrediantId" + 
      "WHERE b.RecipeId = @RecipeId"; 

      using (connection = new SqlConnection(connectionString)) 
      using (SqlCommand command = new SqlCommand(query, connection)) 
      using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection)) 
      { 
       command.Parameters.AddWithValue("@RecipeId", lstRecipes.SelectedValue); 
       DataTable ingrediantsTable = new DataTable(); 
       adapter.Fill(ingrediantsTable); 

       lstIngrediants.DisplayMember = "Name"; 
       lstIngrediants.ValueMember = "Id"; 
       lstIngrediants.DataSource = ingrediantsTable; 
      } 
     } 

     public void lstRecipes_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      PopulateIngrediants(); 
     } 

     private void lblRecipes_Click(object sender, EventArgs e) 
     { 

     } 

     private void frmMain_Load_1(object sender, EventArgs e) 
     { 

     } 
    } 
} 

マイアプリの設定:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
     <add name="Cookbook.Properties.Settings.CookbookConnectionString" 
      connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Cookbook.mdf;Integrated Security=True" 
      providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
</configuration> 

これは私のコードの構造であり、私が実行したときにビューが空白です: enter image description here

注:Visual Studio Community 2015アップデート3を使用しています。私はVSとC#を初めて使用しています。

+0

詳細情報が必要な場合はお知らせください。 – Inconnu

答えて

0

コードに複数の問題があります。あなたがかかわりを開く(およびその優れたを閉じる)することを忘れ

  • あなたのクエリあなたは
  • をCONCAT際のスペースを持っていないあなたは、IDを選択することを忘れて、あなたはあなたがコマンドパラメータを使用し
  • たValueMemberでIDを使用し、あなたは私のコード整流仕事をSURが、この

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.Configuration; 
    using System.Data.SqlClient; 
    
    namespace Cookbook 
    { 
        public partial class frmMain : Form 
        { 
         SqlConnection connection; 
         string connectionString; 
    
         public frmMain() 
         { 
          InitializeComponent(); 
          connectionString = ConfigurationManager.ConnectionStrings["Cookbook.Properties.Settings.CookbookConnectionString"].ConnectionString; 
         } 
    
         public void frmMain_Load(object sender, EventArgs e) 
         { 
          PopulateRecipes(); 
    
         } 
    
         public void PopulateRecipes() 
         { 
          using (connection = new SqlConnection(connectionString)) 
          using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Recipe", connection)) 
           { 
           connection.Open(); //Open connexion 
           DataSet ds = new DataSet(); 
           adapter.Fill(ds); 
    
           lstRecipes.DisplayMember = "Name"; 
           lstRecipes.ValueMember = "Id"; 
           lstRecipes.DataSource = ds.Tables[0]; 
           connection.Close(); //close connexion 
           } 
    
    
         } 
    
    
         public void PopulateIngrediants() 
         { 
          //Donc forget space in your query when you concat string for create query, add column a.id if you want use in valuemember 
          string query = "SELECT a.Name, a.Id FROM Ingrediants a " + 
          "INNER JOIN RecipeIngrediant b ON a.Id = b.IngrediantId " + 
          "WHERE b.RecipeId = @RecipeId"; 
    
          //Remove your command 
          using (connection = new SqlConnection(connectionString)) 
          using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection)) 
          { 
           connection.Open() //open connexion 
           adapter.SelectCommand.Parameters.AddWithValue("@RecipeId", lstRecipes.SelectedValue);//modify parameter of select command to dataadapter (verify selectvalue) 
           DataSet ds = new DataSet(); 
           adapter.Fill(ds); 
    
           lstIngrediants.DisplayMember = "Name"; 
           lstIngrediants.ValueMember = "Id"; 
           lstIngrediants.DataSource = ds.Tables[0]; 
           connection.Close() //close connexion 
          } 
         } 
    
         public void lstRecipes_SelectedIndexChanged(object sender, EventArgs e) 
         { 
          PopulateIngrediants(); 
         } 
    
         private void lblRecipes_Click(object sender, EventArgs e) 
         { 
    
         } 
    
         private void frmMain_Load_1(object sender, EventArgs e) 
         { 
    
         } 
        } 
    } 
    
    0123を試してみませんリンク
  • なし

をデータアダプターを使用します

+0

これを試しましたが、うまく動作せず、エラーも表示されません。 – Inconnu

+0

データセットの代わりにデータセットを使用するコードを変更しました。 – Esperento57

+0

それはまだ同じです:( – Inconnu

関連する問題