Dataset designer problem using sql IN clause
I have a query that works just fine agains the database using an in clause.
where id in (5,6,7)
However, when I try to use the dataset design and build a query using a in clause the designer is apparently looking at the data type of id and only allow me to pass in a valid integer for the parameter for the in clause.
where id in (?)
I have tried change the parameter definition and actually specify that the parameter is a string but it still complains. Anyone know how to fix this?
Thanks
[528 byte] By [
BradyGuy] at [2007-12-25]
I see no problem with the supplied code and know of no bug.
Please post declarations and script.
Adamus
I was not exactly sure what to post for the code. I am using the dataset designer in visual studion 2005 for a vb .net project. I opened the datasets designer.vb code and found what I think is actually being executed. See code below highlighted in yellow is the sql statement that fails. Note that the application compiles and runs when I pass to the parameter a single integer value as a string it works fine when I pass in something like '23,96' it fails saying it can not convert to integer.
The sql is fine in that I have cut and pasted it and run it against the database directly with both 23 and 23,96 as parameters. Somewhere along the line the dataset code appears to be checking the parameter data type against the data type of the field used in the where clause. I good check for an = operator but provides a false negative for the in operator.
I tried to paste in the xml for the schema for the dataset in question but I kept getting an error when trying to post it. If that would help perhaps I can find a way to post it as well.
Thanks
Private Sub InitCommandCollection()
Me._commandCollection = New System.Data.OleDb.OleDbCommand(2) {}
Me._commandCollection(0) = New System.Data.OleDb.OleDbCommand
Me._commandCollection(0).Connection = Me.Connection
Me._commandCollection(0).CommandText = "SELECT spec_type_id, spec_parent_id, spec_type_name, sortorder, property_groups, "& _
"form_format_string, Check_security_flg, displaylist, OwningKey, traceOptions, tr"& _
"aceSQL, itemColumnName, tracePropertyGroup, description, help_url, expand_flg FR"& _
"OM SpecificationType"
Me._commandCollection(0).CommandType = System.Data.CommandType.TextMe._commandCollection(1) = New System.Data.OleDb.OleDbCommandMe._commandCollection(1).Connection = Me.ConnectionMe._commandCollection(1).CommandText = "SELECT spec_type_id, spec_parent_id, spec_type_name, sortorder, form_format_s"& _ "tring, property_groups, Check_security_flg, OwningKey, displaylist, "&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "& _ " traceOptions, traceSQL, itemColumnName, tracePropertyGroup, descript"& _ "ion, help_url, expand_flg"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"FROM SpecificationType"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"WHERE (spec_paren"& _ "t_id IN (?))"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"ORDER BY sortorder"Me._commandCollection(1).CommandType = System.Data.CommandType.TextMe._commandCollection(1).Parameters.Add(New System.Data.OleDb.OleDbParameter("Param1", System.Data.OleDb.OleDbType.VarChar, 0, System.Data.ParameterDirection.Input, CType(0,Byte), CType(0,Byte), "", System.Data.DataRowVersion.Current, false, Nothing))Me._commandCollection(2) = New System.Data.OleDb.OleDbCommandMe._commandCollection(2).Connection = Me.ConnectionMe._commandCollection(2).CommandText = "SELECT spec_type_id, spec_parent_id, spec_type_name, sortorder, form_format_s"& _ "tring, property_groups, Check_security_flg, OwningKey, displaylist, "&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&" "& _ " traceOptions, traceSQL, itemColumnName, tracePropertyGroup, descript"& _ "ion, help_url, expand_flg"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"FROM SpecificationType"&Global.Microsoft.VisualBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW(10)&"WHERE (spec_type_"& _ "id = ?)"Me._commandCollection(2).CommandType = System.Data.CommandType.TextMe._commandCollection(2).Parameters.Add(New System.Data.OleDb.OleDbParameter("spec_type_id", System.Data.OleDb.OleDbType.[Integer], 4, System.Data.ParameterDirection.Input, CType(0,Byte), CType(0,Byte), "spec_type_id", System.Data.DataRowVersion.Current, false, Nothing))End Sub
I thought it might help to show the stack trace for the error that I get when running the sql with the offending parameter.
Conversion failed when converting the varchar value '23,96' to data type int.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at ADSM.ADMDSTableAdapters.SpecificationTypeTableAdapter.GetDataByParentID(String Param1) in C:\Documents and Settings\bchamb\My Documents\A\Design Methodology\DesignTool\ADSM\ADSM\ADMDS.Designer.vb:line 11773
at ADSM.DataAccess.InitSpecTree(Grid l_grid, String spec_type_id, String owningObject, String parentID, Boolean expandDataNodes) in C:\Documents and Settings\bchamb\My Documents\A\Design Methodology\DesignTool\ADSM\ADSM\DataAccess.vb:line 451
I stripped out some of the directives that prevent the debugger from stepping in to the code that is generated by the designer and found the line of code where the exception is raised.
Not sure if this will help or confuse matters. Thanks
Public Overridable Overloads Function GetDataByParentID(ByVal Param1 As String) As ADMDS.SpecificationTypeDataTableMe.Adapter.SelectCommand = Me.CommandCollection(1)If (Param1 Is Nothing) ThenThrow New System.ArgumentNullException("Param1")ElseMe.Adapter.SelectCommand.Parameters(0).Value = CType(Param1, String)End IfDim dataTable As ADMDS.SpecificationTypeDataTable = New ADMDS.SpecificationTypeDataTableMe.Adapter.Fill(dataTable)Return dataTableEnd FunctionThe TableAdapter is parameterizing your select statement. When it looks at the field you are filtering on, and sees that it is a numeric field, it creates a numeric parameter. But you are passing in a string, causing the cast exception.
I am not exactly sure how to solve this problem, as I don't have a lot of experience with TableAdapters. Perhaps a TA guru knows how to get around this?
Sorry, I just re-read your original post and realized you already knew that :)
This appears to be a limitation of the TableAdapter designer. My best suggestion is to create the Select statement as a stored procedure which takes the varchar parameter with the delimited list of values and inserts into a dynamic SQL string. Not very pretty, but I can't come up with anything else.
I too am having this same problem. It seems like not being able to use the IN clause with the query designer would be a huge oversight on the part of the Visual Studio team.
I think I'll open up a ticket with microsoft and see if I can find anything out. I'll post something here if I do. Thanks for taking the time to read and respond.
I agree a stored procedure would work but I hate to go to all that trouble just to make an in clause work.
Brady
I also wanted to use the IN clause in my tableadapter queries but was unable to find a way to do it in the designer.
I came up with this way to get around the problem:
Example:
DataTable dt1 = tableAdapter1.GetData(); //get a filled datatable using your table adapter
DataRow[] dr1 = dt1.Select("id IN (5,6,7,8)"); //use the Select method to get the rows you need via the IN clause
This isn't the most efficient way to do it, but I haven't found any other way.