Re: SQL query... is there a better way?
From: Chris Taylor (chris_taylor_za_at_hotmail.com)
Date: 01/23/05
- Next message: Ted: ".Net Data Provider error help"
- Previous message: ronaldlee: "re:Help! Operation is not allowed in this context."
- In reply to: Scott Reynolds: "Re: SQL query... is there a better way?"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 23 Jan 2005 16:30:52 +0200
Hi,
Sorry for the delay. The most likely reason for the exception is that one of
the values in your list is null (Nothing in VB.NET), if that is the case you
should pass DBNull.Value as the parameter value.
Try the following when adding the parameter.
oCmd.Parameters.Add(New SqlParameter(paramName, IIf(IsNothing(value),
DBNull.Value, value)))
Hope this helps
-- Chris Taylor http://dotnetjunkies.com/weblog/chris.taylor "Scott Reynolds" <scott@it-xenius.co.uk> wrote in message news:OnouqS0$EHA.3708@TK2MSFTNGP14.phx.gbl... > Hi Chris! > > Your code works well, but when I am usin parameterized query, it gives me > followin error: "No value given for one or more required parameters." > > Could you please take a look at code samples below... Do you have any idea > what may cause this error? > > > Dim paramList As New StringBuilder > Dim param As Integer = 0 > Dim paramName As String > > ' WORKING CODE > For Each value As Object In values > paramName = value.ToString > If paramId > 0 Then paramList.Append(",") > paramList.Append(paramValue) > paramId += 1 > Next > > oCmd.CommandText = String.Format("select * from Buildings where > SubwayStationId IN ({0})", paramList.ToString()) > > ' NOT WOEKING CODE > For Each value As Object In values > paramName = String.Format("@Param{0}", param) > If param > 0 Then paramList.Append(",") > paramList.Append(paramName) > oCmd.Parameters.Add(New SqlParameter(paramName, value)) > param += 1 > Next > > oCmd.CommandText = String.Format("select * from MyTable where City IN > ({0})", paramList.ToString()) > > > Thanks! > Scott > > > > "Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message > news:ezoNTua$EHA.1260@TK2MSFTNGP12.phx.gbl... > > Hi, > > > > You could build a comma separated list of the values and use the IN > > statement as an alternative to the OR not realy significant. > > > > Select * FROM MyTable WHERE City IN ('City1', 'City2') > > > > Personally I would use a parameterized query, the code might look > > something > > like this (untested) > > > > Dim paramList As New StringBuilder > > Dim param As Integer = 0 > > Dim paramName As String > > For Each value As Object In values > > paramName = String.Format("@Param{0}", param) > > If param > 0 Then paramList.Append(",") > > paramList.Append(paramName) > > oCmd.Parameters.Add(New SqlParameter(paramName, value)) > > param += 1 > > Next > > oCmd.CommandText = String.Format("select * from MyTable where City IN > > ({0})", paramList.ToString()) > > > > Hope this helps > > > > -- > >
- Next message: Ted: ".Net Data Provider error help"
- Previous message: ronaldlee: "re:Help! Operation is not allowed in this context."
- In reply to: Scott Reynolds: "Re: SQL query... is there a better way?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|