简体   繁体   中英

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. 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. The stored procedure, written and tested in SQL, works - the problem is in the C# code. I'm aware this might be a lame question for lots of pros out there, but please considerate I've done my research. Please be as specific as you can - not here to get criticized but to improve my newbie skills. Thanks.

C#, Windows Forms

EDIT:

catch block was amended as Sir Henry recommended.

This is what I see now

and after moving _connection.Open(); into the try block,

this is what I see

Pretty much wtf...

EDIT 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(); instead of combobox1.SelectedItem.ToString();

One of the reasons for this kind of issue is you're binding an entity to the ComboBox. Can you share the code of data binding to the 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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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