private void cbService1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=Salon Primik; Integrated Security = True";
SqlConnection cn = new SqlConnection(str);
string Name = cbService1.SelectedValue.ToString();
cn.Open();
string Sql = "select Amount from Service where Name='" + Name + "'";
SqlCommand cmd = new SqlCommand(Sql, cn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
lblService1.Text = dr["Amount"].ToString();
int i1 = Convert.ToInt32(lblService1.Text);
total(i1,0);
}
dr.Close();
cn.Close();
}
private void cbService2_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=Salon Primik; Integrated Security = True";
SqlConnection cn = new SqlConnection(str);
string Name = cbService2.SelectedValue.ToString();
cn.Open();
string Sql = "select Amount from Service where Name='" + Name + "'";
SqlCommand cmd = new SqlCommand(Sql, cn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
lblService2.Text = dr["Amount"].ToString();
int i2 = Convert.ToInt32(lblService2.Text);
total(0,i2);
}
dr.Close();
cn.Close();
}
private int total(int i,int i2)
{
int Total = i + i2;
string total = Total.ToString();
lblTotal.Text = total;
答案 0 :(得分:0)
您需要从方法中返回一些内容:
private int total(int i,int i2)
{
int Total = i + i2;
return Total;
}
和
中的设置private void cbService2_SelectedIndexChanged(object sender, EventArgs e)
{
// your other code
lblTotal.Text = total(i1, i2).ToString();
}
或者,如果您想要从方法中更改标签而不返回任何内容,请将其更改为
private void total (int i1, int i2)
答案 1 :(得分:0)
在两个事件上都尊重地编辑代码。
lblService1.Text = dr["Amount"].ToString();
int i1 = Convert.ToInt32(lblService1.Text);
int i2 = Convert.ToInt32(lblService2.Text);
total(i1,i2);
lblService2.Text = dr["Amount"].ToString();
int i1 = Convert.ToInt32(lblService1.Text);
int i2 = Convert.ToInt32(lblService2.Text);
total(i1,i2);
SQL SUM()函数也是一个很好的尝试。