简体   繁体   中英

Issue passing comma delimited value to SelectCommand

I have the following code in my .cs file:

SqlDataSource1.SelectParameters["LocID"].DefaultValue = "5957,5958";

I have the following code in my .aspx file

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KuzSQL %>" OnSelecting="DataSourceSelecting"
    SelectCommand="Select ID, RoomNum from [dbo].[Mbiology] WHERE LocId IN (@LocId)" >
    <SelectParameters>
        <asp:Parameter Name="LocId" />
    </SelectParameters>        
</asp:SqlDataSource>

I get the following error:

Conversion failed when converting the nvarchar value '5957,5958' to data type int.

Note that LocId is of type Int.

Parameters are not just "inserted" into the SQL like you seem to think they are. It is tricky to try and pass a list of values into a query and use them in an IN clause.

Some options:

  1. Pass the values as a string into a stored procedure that parses the string into a temp table, then use IN
  2. Setup your command with several parameters ( LocId1 , LocId2 , etc.) and change your query to:

     Select ID, RoomNum from [dbo].[Mbiology] WHERE LocId IN (@LocId1, @LocId2, @LocId3, @LocId4, @LocId5, @LocId6, ...) 
  3. Save your paramteres to a different table (maybe with a "sessionID" key) and pull from that in your IN clause.
  4. Don't support multiple values in one query.

The SqlDataSource control does not support this type of operation, instead you have, at least, the following options:

  1. Build the query in code-behind.

     protected void SomeEventHandler(object sender, EventArgs e) { // Create list of strings to hold ID values List<string> locIDs = new List<string>(); // Build list of actual string values foreach (var item in SomeControl.Items) { locIDs.Add(value); } // Build SELECT command SqlDataSource1.SelectCommand = String.Format("Select ID, RoomNum from [dbo].[Mbiology] WHERE LocId IN ({0})", String.Join(", ", productIDs.ToArray())); } 
  2. Create a stored procedure and pass the list of ID values as a parameter.

You can create a procedure and create your sql query dynamically inside it. your procedure would be something like this -

create procedure p1_proc
    @val varchar(250)
as
begin
    declare @sql as varchar(500)
    set @sql = 'Select ID, RoomNum from [dbo].[Mbiology] WHERE LocId IN ('+@val+')'
    exec(@sql)
end

so your sqldatasource will call this procedure. something like this

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KuzSQL %>" OnSelecting="DataSourceSelecting"
    SelectCommand="p1_proc" >
    <SelectParameters>
        <asp:Parameter Name="val" />
    </SelectParameters>        
</asp:SqlDataSource>

and you can pass the LocId s as a comma separated string.

SqlDataSource1.SelectParameters["val"].DefaultValue = "5957,5958";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM