I'm paging data using an ObjectDataSource
and I have the following method:
public int GetNumberOfArticles(string employeeIds)
{
System.Data.DataTable dataTable;
System.Data.SqlClient.SqlDataAdapter dataAdapter;
System.Data.SqlClient.SqlCommand command;
int numberOfArticles = 0;
command = new System.Data.SqlClient.SqlCommand();
command.Connection = Classes.Database.SQLServer.SqlConnection;
command.CommandText = @"SELECT COUNT(*)
FROM
[Articles]
WHERE
[Articles].[EmployeeID] IN (@EmployeeIds)";
command.Parameters.AddWithValue("@EmployeeIds", employeeIds);
numberOfArticles = (int)command.ExecuteScalar();
return numberOfArticles;
}
EmployeeID is an integer and because of that, anything I place inside employeeIds
will be converted to an integer. However, because I'm using the IN
keyword, it is obvious that i want to replace employeeIds
with a list of ids separated by commas:
1, 2, 3
But when I replace the line:
command.Parameters.AddWithValue("@EmployeeIds", employeeIds);
with something like:
command.Parameters.AddWithValue("@EmployeeIds", "1, 2, 3");
I receive an exception because I provided a string while EmployeeIds
is an integer. So, how would i go about doing that?
thanks.
Edit:
From the replies, I understand that this has to be done manually, however the class which contains this method would be created automatically by the ObjectDataSource
. So how can i provide the values of employeeIds
at runtime?