【问题标题】:Moved to server: an sqlparameter with parametername is not contained by this sqlparametercollection移至服务器:此 sqlparametercollection 不包含具有参数名的 sqlparameter
【发布时间】:2012-05-11 14:04:32
【问题描述】:

我将一个经过全面测试的网站移到了我的网络服务器上,并且它不断返回

此不包含具有 ParameterName 'Choices' 的 SqlParameter SqlParameterCollection。

使用 VS 2010 我无法复制此错误,一切正常并通过所有测试。

服务器正在运行 IIS 6 和 net 3.5。我过去使用过它,没有遇到任何问题。该站点是使用 vb.net 在 asp.net 3.5 中构建的。

代码:

<SelectParameters>
   <asp:Parameter Name="UserID" Type="Object" />
   <asp:ControlParameter ControlID="DropDownList1" Name="Active" 
                         PropertyName="SelectedValue" Type="Boolean" />
   <asp:Parameter Name="Choices" Type="String" />
</SelectParameters>

Protected Sub BoarListSQL_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles BoarListSQL.Selecting
    e.Command.Parameters("@UserId").Value = userId
    e.Command.Parameters("@Active").Value = DropDownList1.SelectedValue.ToString()
    e.Command.Parameters("@Choices").Value = lb_breeds.SelectedValue.ToString() 'breedChoice
End Sub

SelectCommand="If @Choices = 'ALL' BEGIN SELECT BoarID,Registration,Breed,Tag1,DOB,Name,BoarStudID,EntryDate,RemovalDate,Reason,Active,Location,BreedingHerdDate,Notes FROM [Table] WHERE ([UserID] = @UserID) AND ([Active] = @Active) END ELSE SELECT BoarID,Registration,Breed,Tag1,DOB,Name,BoarStudID,EntryDate,RemovalDate,Reason,Active,Location,BreedingHerdDate,Notes FROM [Table] WHERE ([UserID] = @UserID) AND ([Active] = @Active) AND ([Breed] = @Choices)"

我在参数列表中有选择,并且在运行选择时添加了一个值。

感谢任何帮助。谢谢。

【问题讨论】:

  • 不确定问题,但您可以像这样简化查询:SELECT BoarID, Registration, Breed, Tag1, DOB, Name, BoarStudID, EntryDate, RemovalDate,Reason,Active,Location,BreedingHerdDate,来自 [Table] 的注释 WHERE ([UserID] = @UserID) AND ([Active] = @Active) AND (@Choices = 'ALL' OR [Breed] = @Choices)。嘿,你说“感谢任何帮助”:)
  • 我不知道为什么我一开始没有这样写。感谢您的建议。

标签: asp.net sql vb.net listview parameters


【解决方案1】:

您可以尝试添加 addWithValue(应该可以正常工作)并注意,如果您不使用它,当您将一些文本放入数据库时​​,有时您可能需要 ' '。

所以另一种解决方案可能是:

e.Command.Parameters("@Choices").Value = "'" & lb_breeds.SelectedValue.ToString() & "'"  'breedChoice

您会在以下位置找到一个很好的示例来说明如何使用它: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.90).aspx

希望这会有所帮助。

【讨论】:

  • 'addWithValue' 不是 'System.Data.Common.DbParameterCollection' 的成员
  • 没错,它在 system.data.sqlclient.sqlparametercollection 中似乎是“System.Data.Common.DbParameterCollection”的子项。两者都位于 System.Data 中。参考见:msdn.microsoft.com/en-us/library/…
猜你喜欢
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多