在标签中显示值

时间:2016-07-19 08:11:51

标签: asp.net sql-server-2008-r2

在这段代码中,我从reader1中的一个表中获取一些值(价格),而当下一次执行reader2时,我想在标签中显示价格。我想要显示刚执行的价格价格,但总是显示所有价格的标签。

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)
{


    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();

        }
    }

    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;

        SqlDataReader reader2 = cmd1.ExecuteReader();
        if (reader2.Read())
        {
            if (reader2.GetBoolean(0))

            lbl8_fruit.Text = Fruit_price;


            if (reader2.GetBoolean(1))

            lbl10_drink.Text = Drink_price;


            if (reader2.GetBoolean(2))

            lbl11_desert.Text = Desert_price;


            if (reader2.GetBoolean(3))

            lbl12_mainfood.Text = MainFood_price;



            if (reader2.GetBoolean(4))

            lbl13_salad.Text = Salad_price;


            if (reader2.GetBoolean(5))

            lbl14_tableflower.Text = TableFlower_price;


            if (reader2.GetBoolean(6))

            lbl15_saloonlighting.Text = SaloonLighting_price;


            if (reader2.GetBoolean(7))

            lbl16_saloondesign.Text = SaloonDesign_price;



            if (reader2.GetBoolean(8))

            lbl17_salooncrew.Text = SaloonCrew_price;


            if (reader2.GetBoolean(9))

            lbl18_pastry.Text = Pastry_price;


            if (reader2.GetBoolean(10))

            lbl19_giftcard.Text = GiftCard_price;


        }
    }
 }

}

1 个答案:

答案 0 :(得分:0)

根据这两个陈述中的第二个,你正在取代lbl8_Fruit的文字属性:

lbl8_fruit.Text = reader2[0].ToString();
lbl8_fruit.Text = Fruit_price;

修改 您可以使用方法 GetBoolean(index)读取带有SqlDataReader的布尔值。例如:

if (reader2.GetBoolean(0)) { 
    lbl8_fruit.Text = Fruit_price;
}