2016-07-18 11 views
0

vsこの行にエラーがあります "lbl10_drink.Text = reader [1] .ToString();" 私の接続が正しく動作し、接続文字列と読者[0] ...の値が正しく観察され、......私のquerysも正しく動作します。どうしたの?あなたはリターン1列のみです、あなたの最初のクエリでインデックスがasp.netの配列境界外にありました

{ 

    using (SqlConnection connection1 = DBConnection.getConnection()) 
    { 
     string strquery1 = " with distinctvalueyes (typearticle) as (select top 1 'Fruit' FROM WeedingSalonGeneralRes where Fruit=1 union all select top 1 'Drink' FROM WeedingSalonGeneralRes where Drink=1 union all select top 1 'Desert' FROM WeedingSalonGeneralRes where Desert=1 union all select top 1 'MainFood' FROM WeedingSalonGeneralRes where MainFood=1 union all select top 1 'Salad' FROM WeedingSalonGeneralRes where Salad=1 union all select top 1 'TableFlower' FROM WeedingSalonGeneralRes where TableFlower=1 union all select top 1 'SaloonLighting' FROM WeedingSalonGeneralRes where SaloonLighting=1 union all select top 1 'Saloondesign' FROM WeedingSalonGeneralRes where Saloondesign=1 union all select top 1 'SloonCrew' FROM WeedingSalonGeneralRes where SloonCrew=1 union all select top 1 'Pastry' FROM WeedingSalonGeneralRes where Pastry=1 union all select top 1 'GiftCard' FROM WeedingSalonGeneralRes where GiftCard=1) select * from distinctvalueyes "; 
     connection1.Open(); 
     SqlCommand cmd1 = new SqlCommand(); 
     cmd1.Connection = connection1; 
     cmd1.CommandText = strquery1; 
     string cis = Session["customerID"].ToString(); 
     lbl2_customerid.Text = cis; 

     SqlDataReader reader = cmd1.ExecuteReader(); 
     if (reader.Read()) 
     { 
      lbl8_fruit.Text = reader[0].ToString(); 
      lbl10_drink.Text = reader[1].ToString(); 
      lbl11_desert.Text = reader[2].ToString(); 
      lbl12_mainfood.Text = reader[3].ToString(); 
      lbl13_salad.Text = reader[4].ToString(); 
      lbl14_tableflower.Text = reader[5].ToString(); 
      lbl15_saloonlighting.Text = reader[6].ToString(); 
      lbl16_saloondesign.Text = reader[7].ToString(); 
      lbl17_salooncrew.Text = reader[8].ToString(); 
      lbl18_pastry.Text = reader[9].ToString(); 
      lbl19_giftcard.Text = reader[10].ToString(); 


      using (SqlConnection connection2 = DBConnection.getConnection()) 
      { 
       //query for fetch service prices 
       string strquery2 = "SELECT Fruit_price,Drink_price,Desert_price,MainFood_price,Salad_price,TableFlower_price,SaloonLighting_price,SaloonDesign_price,SaloonCrew_price,Pastry_price,GiftCard_price FROM GenReservationServicePrice"; 
       connection2.Open(); 
       SqlCommand cmd2 = new SqlCommand(); 
       cmd2.Connection = connection2; 
       cmd2.CommandText = strquery2; 
       SqlDataReader reader2 = cmd2.ExecuteReader(); 
       if (reader2.Read()) 
       { 
        string Fruit_price; 
        string Drink_price; 
        string Desert_price; 
        string MainFood_price; 
        string Salad_price; 
        string TableFlower_price; 
        string SaloonLighting_price; 
        string SaloonDesign_price; 
        string SaloonCrew_price; 
        string Pastry_price; 
        string GiftCard_price; 


        Fruit_price = reader[0].ToString(); 
        Drink_price = reader[1].ToString(); 
        Desert_price = reader[2].ToString(); 
        MainFood_price = reader[3].ToString(); 
        Salad_price = reader[4].ToString(); 
        TableFlower_price = reader[5].ToString(); 
        SaloonLighting_price = reader[6].ToString(); 
        SaloonDesign_price = reader[7].ToString(); 
        SaloonCrew_price = reader[8].ToString(); 
        Pastry_price = reader[9].ToString(); 
        GiftCard_price = reader[10].ToString(); 


        lbl8_fruit.Text = Fruit_price; 

       } 
      } 
     } 
    } 
} 

答えて

0

、それがインデックスに達したときに、[1]第二カラムである、それはあなたがあなたのために1つの列のみを持っている配列

の境界の外にありますあなたはC#のコードを変更したくない場合のみ、互いに

の上にデータを積み重ねているUNIONをALL使用していると、あなたはそれがあること

SELECT 
    (select top 1 'Fruit' FROM WeedingSalonGeneralRes where Fruit=1) as fruit, 
    (select top 1 'Desert' FROM WeedingSalonGeneralRes where Desert=1) as desert, 

のようなクエリを書くことができ醜いですが、うまくいくでしょう。もちろん、すべてのあなたのオプションMainFood、サラダなどのためにそれを繰り返す...

+0

それで、最初のクエリでは、私はsqlserverから真の値だけを取って欲しいかもしれない、多分複数の値が真です、今私は何をすべきですか? – meysam

+0

申し訳ありませんが、あなたのクエリは機能しませんでした。私は管理スタジオでテストしました。それにはより多くのエラーがあります – meysam

+0

soryしかし、私に正しいクエリを書くことができますか? – meysam

関連する問題