close

卡路里  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.Visible = false;
Label1.Text = "0";
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text != "請選擇")
{
ListBox1.Items.Add(DropDownList1.SelectedItem);
DropDownList1.Items.Remove(DropDownList1.SelectedItem);
ListBox1.SelectedIndex = -1;
ListBox1.Visible = true;
checkCaloria();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList1.Items.Add(ListBox1.SelectedItem);
DropDownList1.SelectedIndex =DropDownList1.Items.Count-1;
ListBox1.Items.Remove(ListBox1.SelectedItem);
if (ListBox1.Items.Count == 0)
{
ListBox1.Visible = false;
Label1.Text = "0";
}
else
{
checkCaloria();
}

}

protected void checkCaloria()
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
conn.Open();

int caloria = 0;
for (int i = 0; i < ListBox1.Items.Count; i++)
{
cmd.CommandText = "select food_Calorie from Food_Calorie where id=" + ListBox1.Items[i].Value;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
while (dr.Read())
{
caloria += (int)dr.GetSqlInt32(0);
}
}
dr.Close();

}
Label1.Text = caloria.ToString();
cmd.Cancel();
conn.Close();
conn.Dispose();


}
}

appenddatabound  

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ylw1125 的頭像
    ylw1125

    程式搜集分享精靈

    ylw1125 發表在 痞客邦 留言(0) 人氣()