I am trying to link two combo boxes in a C# winform app.
The first combobox - comboKategorija, once selected with the _SelectedIndexChanged
sends the selected value from the combobox as an argument to a stored procedure.
private void comboKategorija_SelectedIndexChanged(object sender, EventArgs e)
{
DataModel db = new DataModel();
CheckInternet net = new CheckInternet();
if (net.Available())
{
var validated = db.ChangeComboSifra(comboKategorija.Text);
comboSifra.DataSource = validated;
comboSifra.DisplayMember = "prefiks";
comboSifra.ValueMember = "prefiks";
}
else
{
MessageBox.Show("Интернет конекцијата е недостапна.", "Информација", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
I use dapper. The method is below. I pass only one argument - the selected value from the combox.
public List<Sifrarnik> ChangeComboSifra(string opis)
{
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["Cloud"].ConnectionString))
{
if (db.State == ConnectionState.Closed)
{
db.Open();
}
var response = db.Query<Sifrarnik>("dbo.spChangeComboSifra @opis",
new
{
opis = opis
}).ToList();
return response;
}
}
The stored procedure is support to return one or multiple rows, which will prepopulate the second combobox - comboboxSifra
ALTER PROCEDURE [dbo].[spChangeComboSifra]
@opis nvarchar(300)
AS
BEGIN
DECLARE @MyPrefiks nvarchar(100)
SET @MyPrefiks = (SELECT prefiks from Sifrarnik where opis LIKE '%' + @opis + '%')
SELECT prefiks,opis FROM Sifrarnik where SUBSTRING(prefiks, 1,2) = @MyPrefiks and LEN(prefiks) > 2
END
However I get the following error:
System.Data.SqlClient.SqlException: 'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.'
It says more than one value returned, which is exactly what I want. But I'm not sure why the error?