Reputation: 942
I am currently using a databound CheckBoxList and I need to identify the status of what checkbox was checked, and unchecked, etc. Basically when a user presses a checkbox it autopostbacks, I read some data from the database, and have a running Session with totals. What I need is when a checkbox is unchecked to identify this change, also make a database call, and substract the amount that product is supposed to cost. Or maybe just read every checkbox everytime a checkbox is checked/unchecked but I'd think that's less optimal.
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
decimal CompraTotal = 0;
MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "QUERY";
command.Parameters.AddWithValue("?GUID", Session["GUID"]);
command.Parameters.AddWithValue("?Name", CheckBoxList1.SelectedValue);
MySqlDataReader reader = command.ExecuteReader();
decimal Price = 0;
while (reader.Read())
{
Price = Convert.ToDecimal(reader["Price"]);
}
Total = Convert.ToDecimal(Session["Total"]);
Total = Total + Price;
Session["Total"] = Total;
}
I just need to identify what checkbox was unchecked and run a similar query to substract from the total. THanks for any help :)
EDIT: Updated code (made some adjustments to variables and stuff as well):
if (Session["previouslySelected"] != null && Session["previouslySelected"].ToString() != "-1")
{
decimal CompraTotal2 = 0;
MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "QUERY";
command.Parameters.AddWithValue("?GUID", Session["GUID"]);
command.Parameters.AddWithValue("?Nombre", CheckBoxList1.SelectedValue);
MySqlDataReader reader = command.ExecuteReader();
decimal Precio = 0;
while (reader.Read())
{
// reader.GetString(0);
Precio = Convert.ToDecimal(reader["Precio"]);
}
CompraTotal2 = Convert.ToDecimal(Session["CompraTotal"]);
CompraTotal2 = Precio - CompraTotal2;
Session["CompraTotal"] = CompraTotal2;
}
if (CheckBoxList1.SelectedIndex != -1)
{
decimal CompraTotal = 0;
MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "QUERY";
command.Parameters.AddWithValue("?GUID", Session["GUID"]);
command.Parameters.AddWithValue("?Nombre", CheckBoxList1.SelectedValue);
MySqlDataReader reader = command.ExecuteReader();
decimal Precio = 0;
int i = 0;
while (reader.Read())
{
// reader.GetString(0);
Precio = Convert.ToDecimal(reader["Precio"]);
}
CompraTotal = Convert.ToDecimal(Session["CompraTotal"]);
CompraTotal = CompraTotal + Precio;
Session["CompraTotal"] = CompraTotal;
}
Session["previouslySelected"] = CheckBoxList1.SelectedIndex;
Label3.Text = Convert.ToString(Session["CompraTotal"]);
}
The problem I am facing know is that the first part of the code also executes at the same time as the other one, giving me bad values. I have two items databound to my CheckList, I have Option 1 and Option 2. I concatenated the values for the text since there are prices, so what I am displaying is Option 1 $1.00 Option 2 $0.50. With the query I get a specific GUID and since the name is unique I look up the Price value. If the checkbox was unchecked it should check for that value, then subtract from the current total. If the checkbox was checked it should then look for that value and add it.
FINAL FIXED CODE FOR THOSE WHO ARE IN THE SAME SITUATION
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
values += CheckBoxList1.Items[i].Value + ",";
\\COMMANDS EXECUTE HERE
}
}
Upvotes: 0
Views: 480
Reputation: 2954
if (Session["previouslySelected"] != null && Session["previouslySelected"].ToString() != "-1")
{
decimal CompraTotal2 = 0;
MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "QUERY";
command.Parameters.AddWithValue("?GUID", Session["GUID"]);
command.Parameters.AddWithValue("?Nombre", Session["previouslySelected"].ToString());
MySqlDataReader reader = command.ExecuteReader();
decimal Precio = 0;
while (reader.Read())
{
// reader.GetString(0);
Precio = Convert.ToDecimal(reader["Precio"]);
}
CompraTotal2 = Convert.ToDecimal(Session["CompraTotal"]);
CompraTotal2 = Precio - CompraTotal2;
Session["CompraTotal"] = CompraTotal2;
}
if (CheckBoxList1.SelectedIndex != -1)
{
decimal CompraTotal = 0;
MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "QUERY";
command.Parameters.AddWithValue("?GUID", Session["GUID"]);
command.Parameters.AddWithValue("?Nombre", CheckBoxList1.SelectedValue);
MySqlDataReader reader = command.ExecuteReader();
decimal Precio = 0;
int i = 0;
while (reader.Read())
{
// reader.GetString(0);
Precio = Convert.ToDecimal(reader["Precio"]);
}
CompraTotal = Convert.ToDecimal(Session["CompraTotal"]);
CompraTotal = CompraTotal + Precio;
Session["CompraTotal"] = CompraTotal;
}
Session["previouslySelected"] = CheckBoxList1.SelectedIndex;
Label3.Text = Convert.ToString(Session["CompraTotal"]);
}
Upvotes: 1