C# 从if循环到其他循环的变量

C# 从if循环到其他循环的变量,c#,loops,variables,if-statement,foreach,C#,Loops,Variables,If Statement,Foreach,我有两个循环,我想得到foreach循环的值“statusP”。 我怎么做? 也许解决办法很简单,但我自己找不到 public void potwierdzWybor_Click(object sender, EventArgs e) { string statusP = ""; if (platnoscT.Checked == true) { statusP = "O"; Label1.Te

我有两个循环,我想得到foreach循环的值“statusP”。 我怎么做? 也许解决办法很简单,但我自己找不到

public void potwierdzWybor_Click(object sender, EventArgs e)
    {
        string statusP = "";
        if (platnoscT.Checked == true)
        {
            statusP = "O";
            Label1.Text = statusP;
        }
        else if (platnoscP.Checked == true)
        {
            statusP = "P";
            Label1.Text = statusP;
        }
        else
        {
            Label1.Text = "Nothing choose";
        }

}
第二个循环,我希望从if循环中获得值statusP:

foreach (GridViewRow row in GridView1.Rows)
        {
            var chk = (HtmlInputCheckBox)row.FindControl("checkboxID");
            var selectedRoomID = (Label)row.FindControl("Label2");
            var cenaPokoj = (Label)row.FindControl("LabelCena");

            if (chk.Checked && chk != null)
            {


                Label1.Text = cenaPokoj.Text;


                 String CS = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
                 using (SqlConnection con = new SqlConnection(CS))
                 {

                     SqlCommand cmd = new SqlCommand("dodajRezerwacje", con);
                     cmd.CommandType = System.Data.CommandType.StoredProcedure;
                     cmd.Parameters.AddWithValue("@dataStart",Convert.ToDateTime(CalendarStart.SelectedDate.ToShortDateString()));
                     cmd.Parameters.AddWithValue("@dataStop", Convert.ToDateTime(CalendarStop.SelectedDate.ToShortDateString()));
                     cmd.Parameters.AddWithValue("@cena", Convert.ToInt32(cenaPokoj.Text));
                     cmd.Parameters.AddWithValue("@statusPlat", platnosc);
                     cmd.Parameters.AddWithValue("@id_login", Session["LOGIN"].ToString());
                     cmd.Parameters.AddWithValue("@id_pokoj",Convert.ToInt32(selectedRoomID.Text));


                     SqlParameter outputParamater = new SqlParameter("@wiadomosc", System.Data.SqlDbType.VarChar, 250);
                     outputParamater.ParameterName = "@wiadomosc";
                     outputParamater.Direction = System.Data.ParameterDirection.Output;
                     cmd.Parameters.Add(outputParamater);
                     con.Open();
                     cmd.ExecuteNonQuery();
                     string msgRezerwacja = outputParamater.Value.ToString();
                     Label1.Text = msgRezerwacja;

                 }
            }
            else if (chk == null)
            {

                Label1.Text = "Wybierz pokój";

            }




        }

我想把这个值传递给存储过程调用。

您的第一个方法不是@BradleyDotNET所说的foor循环

最好多读一些关于 和 和 现在,对于您的主要问题:通过在button范围外声明变量,使其在整个类中可见和可用,很容易做到这一点

那么您的第一种方法将是:

string statusP = ""; // will be seen in the whole class.

public void potwierdzWybor_Click(object sender, EventArgs e)
    {

        if (platnoscT.Checked == true)
        {
            statusP = "O";
            Label1.Text = statusP;
        }
        else if (platnoscP.Checked == true)
        {
            statusP = "P";
            Label1.Text = statusP;
        }
        else
        {
            Label1.Text = "Nothing choose";
        }

}

如果
不是循环构造。听起来您需要了解变量范围、类成员或方法参数。您应该颠倒这些检查的顺序:
if(chk.Checked&&chk!=null)
。现在,如果
chk
null
,第一个条件将抛出
NullReferenceException
,因为这些条件是从左到右执行的。或者,更好的方法是,只需切换
else if
if
if(chk==null){Label1.Text=“Wybierz pokój”}else if(chk.Checked){…