简体   繁体   English

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

[英]Retrieving and assigning a combobox value to a stored procedure parameter

Problem from yesterday ...still couldn't get sorted. 昨天的问题 ...仍然无法解决。

Have gone through a million post and videos but couldn't find the one that points out what I'm doing wrong. 已经浏览了上百万个帖子和视频,但是找不到指出我做错事情的视频。 I basically want to retrieve and assign a combobox's value, and send it to an SQL stored procedure's parameter. 我基本上想检索并分配组合框的值,并将其发送到SQL存储过程的参数。 I was blamed not to provide code enough yesterday so here's a bit more detailed. 我被指责昨天没有提供足够的代码,所以这里有一些详细说明。

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();
    }
}

What happens: when I select the item in the combobox, it first automatically pops up the error message, then it shows the item in the box, but apparently the value of it is not getting passed to @someparameter , or at least the stored procedure is not getting triggered. 发生的情况:当我在组合框中选择项目时,它首先自动弹出错误消息,然后在框中显示该项目,但显然它的值没有传递给@someparameter ,或者至少没有传递给存储过程没有被触发。 The stored procedure, written and tested in SQL, works - the problem is in the C# code. 用SQL编写和测试的存储过程可以工作-问题出在C#代码中。 I'm aware this might be a lame question for lots of pros out there, but please considerate I've done my research. 我知道这对许多专业人士来说可能是一个la脚的问题,但请考虑一下我已经完成了研究。 Please be as specific as you can - not here to get criticized but to improve my newbie skills. 请尽可能具体-不要在这里受到批评,而是要提高我的新手技能。 Thanks. 谢谢。

C#, Windows Forms C#,Windows窗体

EDIT: 编辑:

catch block was amended as Sir Henry recommended. 捕获块已按照亨利爵士的建议进行了修改。

This is what I see now 这就是我现在看到的

and after moving _connection.Open(); 并且移动_connection.Open()之后; into the try block, 进入try块

this is what I see 这就是我所看到的

Pretty much wtf... 差不多...

EDIT 2: 编辑2:

it seems the problem is gone, thanks to Sir Henry. 多亏亨利爵士,问题似乎已经解决了。 All I need to figure out now is how to populate the second combobox, based on the stored procedure that was called by the value of the first combobox. 我现在需要弄清楚的是如何根据第一个组合框的值调用的存储过程来填充第二个组合框。 This is how the code looks like now: 现在的代码如下所示:

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);
            }
        }
    }

Have you tried combobox1.SelectedValue.ToString(); 您是否尝试过combobox1.SelectedValue.ToString(); instead of combobox1.SelectedItem.ToString(); 而不是combobox1.SelectedItem.ToString();

One of the reasons for this kind of issue is you're binding an entity to the ComboBox. 出现此类问题的原因之一是您将实体绑定到ComboBox。 Can you share the code of data binding to the ComboBox? 您可以共享将数据绑定到ComboBox的代码吗?

This should work 这应该工作

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();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM