2016-11-24 99 views
0

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窗体

编辑:

catch块被修改为亨利爵士建议。

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

首先你应该找出异常。我想这可能是命令或连接的问题。对于这个重写catch块catch(Exception ex) MessageBox.Show(ex.ToString(),“Error”,MessageBoxButtons.OK,MessageBoxIcon.Error); }然后在您的问题 –

+0

中发布例外情况,对于迟到的回复感到抱歉 - 星期五关闭了,但现在我回来了,并且已经做了必要的更改。请参阅问题中的编辑。谢谢 – stuckedagain

+0

你的问题是_connection.Close();在终于阻止。连接通常有两种类型的全局或本地(工作单元)。环球始终是开放的,以当地方式关闭它是不好的做法。本地已在方法主体中初始化,您应该关闭连接或使用(){} insructon –

回答

1

你试过combobox1.SelectedValue.ToString();,而不是combobox1.SelectedItem.ToString();

其中的一个原因,这种问题是你的实体结合到ComboBox。你能分享数据绑定到ComboBox的代码吗?

+0

我刚才尝试过,但无济于事,同样的问题。数据绑定是Metro框架自动生成的代码,您基本上为组合框提供了一个数据源/表,并且完成了。感谢您尝试帮助。 – stuckedagain

+0

您可以在该行放置断点并调试ComboBox中的属性。可以点亮一些。 – Nitesh

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

sooo ....这个解决方案与我的完全不同?你能否指出你所做的改变?每一行都与问题中的相同,不幸的是不能正常工作。谢谢 – stuckedagain