检索并将组合框值分配给存储过程参数

时间:2016-11-24 12:00:22

标签: c# sql-server stored-procedures combobox

Problem from yesterday ...仍然无法排序。

已经浏览了一百万个帖子和视频但却无法找到指出我做错了什么的帖子。我基本上想要检索并分配组合框的值,并将其发送到SQL存储过程的参数。我被指责不要提供足够的代码,所以这里更详细一点。

SqlConnection _connection;
SqlCommand _command;

_connection = new SqlConnection(@"Data Source=someserver;Initial Catalog=sometable;Integrated Security=True");

_connection.Open();

_command = _connection.CreateCommand();
_command.CommandType = CommandType.StoredProcedure;

private void SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        _command.CommandText = "someprocedurename";
        _command.Parameters.Add("@someparameter", SqlDbType.NVarChar).Value = combobox1.SelectedItem.ToString();

        _command.ExecuteNonQuery();
    }
    catch (Exception)
    {
        MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        _connection.Close();
    }
}

会发生什么:当我在组合框中选择项目时,它首先会自动弹出错误信息,然后它会在框中显示该项目,但显然它的值未传递给@someparameter,或者至少存储过程没有被触发。在SQL中编写和测试的存储过程可以工作 - 问题在于C#代码。我知道这对于很多专业人士来说可能是一个蹩脚的问题,但请体谅我做了我的研究。请尽可能具体 - 不要在这里受到批评,而是要提高我的新手技能。感谢。

C#,Windows Forms

编辑:

按照亨利爵士的建议修改了捕获块。

This is what I see now

并且在移动_connection.Open()之后;进入try块,

this is what I see

非常重要...

编辑2:

由于亨利爵士,似乎问题已经消失了。我现在需要弄清楚的是如何根据第一个组合框的值调用的存储过程来填充第二个组合框。这就是代码现在的样子:

private void SelectedIndexChanged(object sender, EventArgs e)
    {

        using (SqlConnection _connection =
                new SqlConnection(@"Data Source=someserver;Initial Catalog=sometable;Integrated Security=True"))

            try
            {

                _connection.Open();

                SqlCommand _command  = new SqlCommand("someprocedurename", _connection);

                _command.CommandType = CommandType.StoredProcedure;

                _command.Parameters.Add("@someparameter", SqlDbType.NVarChar).Value = combobox1.SelectedValue.ToString();

                _command.ExecuteNonQuery();

/* i guess this is the part where i should "inform" combobox2 about the
 result of the stored procedure, based on combobox1. have no idea though,
 how it could be done. maybe i need a dataset? clueless. just to be clear:
if value "a" was selected in combobox1, i want to populate combobox2 with 
the value "1". if value "b" was selected in combobox1, i want to populate
combobox2 with the value "2". etc. */

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您是否尝试过combobox1.SelectedItem.ToString();而不是cell6.myButton.addTarget(self, action: #selector(ViewController6.buttonAction(_:)), for: .touchUpInside) cell6.myButton.addGestureRecognizer(self.longPressGesture()) func longPressGesture() -> UILongPressGestureRecognizer { let lpg = UILongPressGestureRecognizer(target: self, action: #selector(ViewController6.longPress)) lpg.minimumPressDuration = 0.5 return lpg } func longPress(_ sender: UILongPressGestureRecognizer) { if let url = Bundle.main.url(forResource: soundArrayRiflesUzun[sender.view.tag], withExtension: "mp3") { player.removeAllItems() player.insert(AVPlayerItem(url: url), after: nil) player.play() } }

此类问题的原因之一是您将实体绑定到ComboBox。你能分享一下绑定到ComboBox的数据代码吗?

答案 1 :(得分:0)

这应该有效

SqlConnection _connection;
SqlCommand _command;

_connection = new SqlConnection(@"Data Source=someserver;Initial Catalog=sometable;Integrated Security=True");

_connection.Open();

_command = _connection.CreateCommand();
_command.CommandType = CommandType.StoredProcedure;

private void SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        _command.CommandText = "someprocedurename";
        _command.Parameters.Add("@someparameter", SqlDbType.NVarChar).Value = combobox1.SelectedValue.ToString();

        _command.ExecuteNonQuery();
}
catch (Exception)
{
    MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
 finally
 {
    _connection.Close();
  }
}