I'm trying to execute the following query but getting an exception.
using (SqlConnection con = new SqlConnection(UserDatabase.getConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Order_Header where Status IN (@Values)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
try
{
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
// This is for test purposes
List<int> yourValues = new List<int>() { 1, 2, 3, 4, 5 };
//Get values for IN
string x = String.Join(",", yourValues.Select(s => String.Format("'{0}'", s)).ToArray());
// Add parameter
cmd.Parameters.AddWithValue("@Values", x);
DataTable dt = new DataTable();
sda.Fill(dt);
order_details.SetDataSource(dt);
SalesReport.ReportSource = order_details;
}
catch (Exception ex)
{
scriptMessage(ex.ToString);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
}
}
On executing this query, I get the following exception:
Conversion failed when converting the nvarchar value ''1','2','3','4','5'' to data type int.
Why is this happening? Help please