【问题标题】:Getting user choices from multi case switch statement从多案例 switch 语句中获取用户选择
【发布时间】:2016-11-16 17:13:26
【问题描述】:

我正在使用向导控件让用户完成多个步骤。他们从第 1 步开始,但如果他们回答是或否(单选按钮),有时会跳过一些步骤。例如,某些步骤结束时的问题是:“问题解决了吗?”并且有一个是或否单选按钮选择。如果答案是肯定的,请将他们带到最后一步完成。如果答案是否定的,则进入下一步。

我的问题是从数据库中表测试的所有步骤中获取结果。

        string constring = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ClientMonitorDevices"].ConnectionString;
        SqlConnection conn = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand();

        cmd = conn.CreateCommand();
        cmd.CommandText = @"INSERT INTO test (siteid, hotelname, step1, step2)
                            VALUES (@siteid, @hotelname, @step1, @step2)";

        cmd.Parameters.AddWithValue("@siteid", siteid);
        cmd.Parameters.AddWithValue("@hotelname", hotelname);

        int activeStep = Wizard1.ActiveStepIndex;

        switch (activeStep)
        {
            case 1:
                if (step1_yes.Checked)
                {
                    step1 = "Device Can Detect Network with a Signal Strength of 50% or Greater";
                    Wizard1.ActiveStepIndex = 2;
                }
                else if (step1_no.Checked)
                {
                    step1 = "Device Cannot Detect Network with a Signal Strength of 50% or Greater";
                    Wizard1.ActiveStepIndex = 16;
                }
                else
                {
                    e.Cancel = true;
                    gen.MessagBox(this.Page, "Please choose Yes or No", "Please choose Yes or No");
                }
                cmd.Parameters.AddWithValue("@step1", step1);
                break;
            case 2:
                if (step2_unable.Checked)
                {
                    step2 = "Unable to Join Network";
                    Wizard1.ActiveStepIndex = 3;
                }
                else if (step2_unidentified.Checked)
                {
                    step2 = "Connect to Unidentified Network";
                    Wizard1.ActiveStepIndex = 7;
                }
                else if (step2_internet.Checked)
                {
                    step2 = "Connected (Internet Access)";
                    Wizard1.ActiveStepIndex = 11;
                }
                else if (step2_local.Checked)
                {
                    step2 = "Connected Local Only (No Internet Access)";
                    Wizard1.ActiveStepIndex = 15;
                }
                else
                {
                    e.Cancel = true;
                    gen.MessagBox(this.Page, "Please Choose One of the Options", "Please Choose One of the Options");
                }
                cmd.Parameters.AddWithValue("@step2", step2);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                break;

我得到的错误是必须声明标量变量“@step1”。因为 step1 超出了范围,或者这就是我的想法。我尝试了一个存储过程,但这也不起作用。

另一个尝试是使用更新语句,但我遇到的问题是获取可索引的 ID 列(索引),并且每次插入都会增加 1。如果有人有任何想法,请务必告诉我。我已经为此工作了大约 2 周,但无济于事。

【问题讨论】:

    标签: c# switch-statement wizard-control


    【解决方案1】:

    问题在于您的查询(包含@step1 参数)是在任何条件结构(即switch 块)之外定义的:

    cmd.CommandText = @"INSERT INTO test (siteid, hotelname, step1, step2)
                        VALUES (@siteid, @hotelname, @step1, @step2)";
    

    但是,您只在activeStep 开关块中的一个 案例中提供@step1 查询参数,在另一个案例中提供@step2。查询本身需要这两个参数。

    您需要在两个最外层的 case 语句中都包含这两行:

    cmd.Parameters.AddWithValue("@step1", step1);
    cmd.Parameters.AddWithValue("@step2", step2);
    

    如果在任何一种情况下,另一个值都将为空,您只需将值设置为DBNull.Value

    【讨论】:

    • 不确定我是否关注。您是说将两个语句放在 switch 语句之外,就像在 switch 语句关闭手镯之后一样?如果是这样,我这样做并得到了这个错误参数化查询'(@siteid nvarchar(6),@hotelname nvarchar(8),@step1 nvarchar(4000'需要参数'@step1',它没有提供。所有我的变量是数据库端的 varchar(max)
    • 所以您现在在底部switch 块之外有声明cmd.Parameters.AddWithValue("@step1", step1);,后跟声明cmd.Parameters.AddWithValue("@step2", step2);
    • 是的。 default: break; } cmd.Parameters.AddWithValue("@step1", step1); cmd.Parameters.AddWithValue("@step2", step2); conn.Open(); cmd.ExecuteNonQuery(); conn.Close();
    • 不知道为什么那不起作用。由于您的数据库类型在数据库中是 varchar(max) ,因此可能会遇到问题。尝试将语句更改为:cmd.Parameters.Add("@step1", SqlDbType.VarChar, -1).Value = step1;cmd.Parameters.Add("@step2", SqlDbType.VarChar, -1).Value = step2;
    • 非常感谢您的所有建议。我按照你的建议做了,得到了类似的错误The parameterized query '(@siteid varchar(max) ,@hotelname varchar(max) ,@step1 varchar(m' expects the parameter '@step1', which was not supplied.
    猜你喜欢
    • 2014-01-03
    • 2015-06-21
    • 2012-11-10
    • 2015-12-06
    • 1970-01-01
    • 2021-11-13
    • 2023-04-11
    • 2010-10-23
    • 1970-01-01
    相关资源
    最近更新 更多