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]
# 1

I see no problem with the supplied code and know of no bug.

Please post declarations and script.

Adamus

AdamusTurner at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

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.Text

Me._commandCollection(1) = New System.Data.OleDb.OleDbCommand

Me._commandCollection(1).Connection = Me.Connection

Me._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.Text

Me._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.OleDbCommand

Me._commandCollection(2).Connection = Me.Connection

Me._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.Text

Me._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

BradyGuy at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

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

BradyGuy at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

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.SpecificationTypeDataTable

Me.Adapter.SelectCommand = Me.CommandCollection(1)

If (Param1 Is Nothing) Then

Throw New System.ArgumentNullException("Param1")

Else

Me.Adapter.SelectCommand.Parameters(0).Value = CType(Param1, String)

End If

Dim dataTable As ADMDS.SpecificationTypeDataTable = New ADMDS.SpecificationTypeDataTable

Me.Adapter.Fill(dataTable)

Return dataTable

End Function

BradyGuy at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

The 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?

CommonGenius.com at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

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.

CommonGenius.com at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7
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.
ProudGecko at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

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

BradyGuy at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9

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.

drewrauv at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...