2016-07-23 8 views
0

私は顧客に販売結果を示すためのコードを持っています。最終的には、選択した商品の価格だけを合計し、 "lblTotalCost"ラベルに表示します。私の問題は:どのようにすべての価格ではなく、選択された商品の価格だけを合計することができますか?どのように合計だけを選んだ商品の価格

public partial class SingleSeatSaleResult : System.Web.UI.Page 
{ 
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; 
protected void Page_Load(object sender, EventArgs e) 
{ 


    /////Fetching Reservation Date and Reservation Time 
    using (SqlConnection connection3 = DBConnection.getConnection()) 
    { 
     string strquery3 = "SELECT SaloonReservationDate,SaloonReservationTime FROM SaloonGeneralReservation"; 
     connection3.Open(); 
     SqlCommand cmd3 = new SqlCommand(); 
     cmd3.Connection = connection3; 
     cmd3.CommandText = strquery3; 
     SqlDataReader reader3 = cmd3.ExecuteReader(); 
     if (reader3.Read()) 
     { 
      lbl4_ResDate.Text = reader3[0].ToString(); 
      lbl6_ResTime.Text = reader3[1].ToString(); 

     } 
    } 


    /////Fetching Service Prices 
    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 reader1 = cmd2.ExecuteReader(); 
     if (reader1.Read()) 
     {        
      Fruit_price = reader1[0].ToString(); 
      Drink_price = reader1[1].ToString(); 
      Desert_price = reader1[2].ToString(); 
      MainFood_price = reader1[3].ToString(); 
      Salad_price = reader1[4].ToString(); 
      TableFlower_price = reader1[5].ToString(); 
      SaloonLighting_price = reader1[6].ToString(); 
      SaloonDesign_price = reader1[7].ToString(); 
      SaloonCrew_price = reader1[8].ToString(); 
      Pastry_price = reader1[9].ToString(); 
      GiftCard_price = reader1[10].ToString(); 

     } 
    } 

    ///converting service prices to Double for Calculating 
    double fruit_final_price = Convert.ToDouble(Fruit_price); 
    double drink_final_price = Convert.ToDouble(Drink_price); 
    double desert_final_price = Convert.ToDouble(Desert_price); 
    double mainfood_final_price = Convert.ToDouble(MainFood_price); 
    double salad_final_price = Convert.ToDouble(Salad_price); 
    double tableflower_final_price = Convert.ToDouble(TableFlower_price); 
    double saloonlighting_final_price = Convert.ToDouble(Desert_price); 
    double saloondesign_final_price = Convert.ToDouble(SaloonDesign_price); 
    double salooncrew_final_price = Convert.ToDouble(SaloonCrew_price); 
    double pastry_final_price = Convert.ToDouble(Pastry_price); 
    double giftcard_final_price = Convert.ToDouble(GiftCard_price); 

    /////Fetching Selected Items by User 
    using (SqlConnection connection1 = DBConnection.getConnection()) 
    {   
     string strquery1 = "SELECT (select top 1 'Fruit' FROM WeedingSalonGeneralRes where Fruit=1) as fruit, (select top 1 'Drink' FROM WeedingSalonGeneralRes where Drink=1) as drink, (select top 1 'Desert' FROM WeedingSalonGeneralRes where Desert=1) as desert,(select top 1 'MainFood' FROM WeedingSalonGeneralRes where MainFood=1) as MainFood,(select top 1 'Salad' FROM WeedingSalonGeneralRes where Salad=1) as salad,(select top 1 'TableFlower' FROM WeedingSalonGeneralRes where TableFlower=1) as TableFlower,(select top 1 'SaloonLighting' FROM WeedingSalonGeneralRes where SaloonLighting=1) as SaloonLighting,(select top 1 'Saloondesign' FROM WeedingSalonGeneralRes where Saloondesign=1) as Saloondesign,(select top 1 'SloonCrew' FROM WeedingSalonGeneralRes where SloonCrew=1) as SloonCrew,(select top 1 'Pastry' FROM WeedingSalonGeneralRes where Pastry=1) as Pastry,(select top 1 'GiftCard' FROM WeedingSalonGeneralRes where GiftCard=1) as GiftCard "; 
     connection1.Open(); 
     SqlCommand cmd1 = new SqlCommand(); 
     cmd1.Connection = connection1; 
     cmd1.CommandText = strquery1; 
     string cis = Session["customerID"].ToString(); 
     lbl2_customerid.Text = cis; 

     Double totalcost; 

     SqlDataReader reader2 = cmd1.ExecuteReader(); 
     if (reader2.Read()) 
     { 
      if (reader2[0].ToString() != null && reader2[0].ToString() != "") 
       lbl8_fruit.Text = reader2[0].ToString() + Fruit_price; 
      else 
       fruit_final_price = 0; 


      if (reader2[1].ToString() != null && reader2[1].ToString() != "") 
       lbl10_drink.Text = reader2[1].ToString() + Drink_price; 
      else 
       drink_final_price = 0; 


      if (reader2[2].ToString() != null && reader2[2].ToString() != "") 
       lbl11_desert.Text = reader2[2].ToString() + Desert_price; 
      else 
       desert_final_price = 0; 

      if (reader2[3].ToString() != null && reader2[3].ToString() != "") 
       lbl12_mainfood.Text = reader2[3].ToString() + MainFood_price; 
      else 
       mainfood_final_price = 0; 

      if (reader2[4].ToString() != null && reader2[4].ToString() != "") 
       lbl13_salad.Text = reader2[4].ToString() + Salad_price; 
      else 
       salad_final_price = 0; 

      if (reader2[5].ToString() != null && reader2[5].ToString() != "") 
       lbl14_tableflower.Text = reader2[5].ToString() + TableFlower_price; 
      else 
       tableflower_final_price = 0; 


      if (reader2[6].ToString() != null && reader2[6].ToString() != "") 
       lbl15_saloonlighting.Text = reader2[6].ToString() + SaloonLighting_price; 
      else 
       saloonlighting_final_price = 0; 


      if (reader2[7].ToString() != null && reader2[7].ToString() != "") 
       lbl16_saloondesign.Text = reader2[7].ToString() + SaloonDesign_price; 
      else 
       saloondesign_final_price = 0; 


      if (reader2[8].ToString() != null && reader2[8].ToString() != "") 
       lbl17_salooncrew.Text = reader2[8].ToString() + SaloonCrew_price; 
      else 
       salooncrew_final_price = 0; 


      if (reader2[9].ToString() != null && reader2[9].ToString() != "") 
       lbl18_pastry.Text = reader2[9].ToString() + Pastry_price; 
      else 
       pastry_final_price = 0; 


      if (reader2[10].ToString() != null && reader2[10].ToString() != "") 
       lbl19_giftcard.Text = reader2[10].ToString() + GiftCard_price; 
      else 
       giftcard_final_price = 0; 

      totalcost = fruit_final_price + drink_final_price + desert_final_price+mainfood_final_price+salad_final_price+tableflower_final_price+saloonlighting_final_price+saloondesign_final_price+salooncrew_final_price+pastry_final_price+giftcard_final_price; 
      lblTotalCost.Text = totalcost.ToString(); 



     } 
    } 
} 

    } 
+0

選択肢の特定方法は?任意のチェックボックスなど? – Sami

+0

はいユーザーが別のWebフォームのアイテムをチェックボックスで選択し、テーブルに格納されている選択されたアイテムを真の偽値で選択した後、ボタンをクリックすると、ユーザーはSaleResultページに行き、結果をユーザーに表示します。最初のクエリでは価格をフェッチし、2番目のクエリでは真の値だけをフェッチします。今私は選択された価格だけを合計し、ラベルでそれらを示したい – meysam

答えて

0

リファクタリングとクエリをストアドプロシージャに移動する必要があります。しかし、ここでは、既存のコードのためのヒントをいくつか紹介します。他の列のように

string strquery1 = "SELECT 
(select top 1 'Fruit' FROM WeedingSalonGeneralRes where Fruit=1) as fruit, 
(select COALESCE(MAX(Fruit_Price),0) From GenReservationServicePrice) as fruit_price, 
(select top 1 'Drink' FROM WeedingSalonGeneralRes where Drink=1) as drink, 
(select COALESCE(MAX(Drink_price,0)) From GenReservationServicePrice) as drink_price, 
(select top 1 'Desert' FROM WeedingSalonGeneralRes where Desert=1) as desert, 
(select COALESCE(MAX(Desert_price,0)) From GenReservationServicePrice) as desert_price, 

と: あなたは次のように2つのクエリをマージすることはできません。

代わりの

if (reader2[0].ToString() != null && reader2[0].ToString() != "") 

使用後

if (!string.IsNullOrEmpty(reader[0].ToString()) 

if (reader2.Read()) 
    { 
     if (!string.IsNullOrEmpty(reader["fruit"].ToString()) 
     lbl8_fruit.Text = reader2["friut"].ToString() + reader2["Fruit_price"].ToString(); 

...最後に

//COALESCE in query ensure you don't get Null but 0 instead 
selectedItemsSum = Convert.ToDouble(reader2["fruit_price"]) + ... 

希望すると便利です。

+0

私は理解しません。あなたは私の質問が間違っていると言います、それでどうすればいいのですか?この行 "if(reader2 [0] .ToString()!= null && reader2 [0] .ToString()!=" ")"はreader2に書き込んだので、reader2から書き出します。明確に説明してください。ありがとう – meysam

+0

あなたの質問は間違っていません。それは私の提案です。それに従う場合は、/////サービス価格を取得する - セクションをスキップしてください。これは、回答 – Sami

+1

に記載されているようにクエリで選択できる価格を選択しているため、私の問題は単純な解決策で解決されました。私の更新されたコードを見てください。 else条件を追加して、最終価格を0以外に設定してください。 – meysam

関連する問題