How to pass XML from C# to a stored procedure in SQL Server 2008?

For part 2 of your question, see my answer to Stored procedure: pass XML as an argument and INSERT (key/value pairs) for an example of how to use XML within a stored procedure. EDIT: Sample code below is based on the specific example given in the comments. declare @MyXML xml set @MyXML = ‘<booksdetail> <isbn_13>700001048</isbn_13> … Read more

WHERE IN (array of IDs)

You can’t (unfortunately) do that. A Sql Parameter can only be a single value, so you’d have to do: WHERE buildingID IN (@buildingID1, @buildingID2, @buildingID3…) Which, of course, requires you to know how many building ids there are, or to dynamically construct the query. As a workaround*, I’ve done the following: WHERE buildingID IN (@buildingID) … Read more

Assign null to a SqlParameter

The problem is that the ?: operator cannot determine the return type because you are either returning an int value or a DBNull type value, which are not compatible. You can of course cast the instance of AgeIndex to be type object which would satisfy the ?: requirement. You can use the ?? null-coalescing operator … Read more

An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.Data.dll?

You should either use this method: SqlCommand cmd = new SqlCommand(“dbo.workScheduleDataGrid”, sqlcon); or this method SqlCommand cmd = sqlcon.CreateCommand(); to create the command, but not both (the second assignment in your code overwrites the first one). With the second options, you need to specify the command to execute separarely: cmd.CommandText = “dbo.workScheduleDataGrid”; Also, do not … Read more