Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I provide a string of Ids in a Select statement's IN clause even though the column is an integer
    text
    copied!<p>I'm paging data using an <code>ObjectDataSource</code> and I have the following method:</p> <pre><code>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; } </code></pre> <p><strong>EmployeeID</strong> is an integer and because of that, anything I place inside <code>employeeIds</code> will be converted to an integer. However, because I'm using the <code>IN</code> keyword, it is obvious that i want to replace <code>employeeIds</code> with a list of ids separated by commas:</p> <pre><code>1, 2, 3 </code></pre> <p>But when I replace the line:</p> <pre><code>command.Parameters.AddWithValue("@EmployeeIds", employeeIds); </code></pre> <p>with something like:</p> <pre><code>command.Parameters.AddWithValue("@EmployeeIds", "1, 2, 3"); </code></pre> <p>I receive an exception because I provided a string while <code>EmployeeIds</code> is an integer. So, how would i go about doing that?</p> <p>thanks.</p> <p><strong>Edit:</strong></p> <p>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 <code>ObjectDataSource</code>. So how can i provide the values of <code>employeeIds</code> at runtime?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload