问题描述
你好,
我仍在使用参数对项目过滤器SqlDataSource进行操作.
1.Button1是我的第一个开发代码,可与Dropdownlist控件配合使用.
2.但是,现在我想使用Checkboxlist项目进行过滤.我在Button2_Click事件下编写了代码.我已经结合了我目前所掌握的代码,而我脑子里已经有了它. Button3_click是我第二次尝试使用Checkboxlists过滤
我在Button2上遇到错误
错误:Sys.WebForms.PageRequestManagerServerErrorException:索引超出范围.必须为非负数并且小于集合的大小.参数名称:index
3.第二次尝试是Button3_click,我也从中得到一个错误.如果选择复选框列表中的一项,则过滤成功,但同时选中另一项,则返回错误.
错误:Sys.WebForms.PageRequestManagerServerErrorException:变量名称"@FirstName"已声明.变量名称在查询批处理或存储过程中必须唯一.
4.第三次尝试是Button4_click,我也从中得到一个错误:
错误:Sys.WebForms.PageRequestManagerServerErrorException:从类型"ListItem"到类型"String"的转换无效.
我希望有人可以帮助我实现代码:-)
Button1_Click:
Hello,
I''m still working on project filter SqlDataSource with parameters.
1.Button1 is my first develope code which is working fine with Dropdownlist controls.
2.But, now I want to move to filter with Checkboxlist items. I have write code under Button2_Click event. I have combine codes of my current knowledge which I have it in my head. Button3_click is my second try filtering with Checkboxlists
I''m getting error on Button2
Error: Sys.WebForms.PageRequestManagerServerErrorException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
3.Second try would be Button3_click from which I also getting an error. If I select one item of checkboxlist it filter sucessful, but I check also another item then it return an error.
Error: Sys.WebForms.PageRequestManagerServerErrorException: The variable name ''@FirstName'' has already been declared. Variable names must be unique within a query batch or stored procedure.
4. Third try would be Button4_click from which I also get an error:
Error: Sys.WebForms.PageRequestManagerServerErrorException: Conversion from type ''ListItem'' to type ''String'' is not valid.
I hope someone could help me with codes to reach the goal :-)
Button1_Click:
<br />
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click<br />
SqlDataSource1.SelectCommand = "Select * From test WHERE (FirstName = @FirstName Or @FirstName IS NULL) AND (SecondName = @SecondName Or @SecondName IS NULL)"<br />
SqlDataSource1.SelectParameters.Add("FirstName", DropDownListFirstName.Text)<br />
SqlDataSource1.SelectParameters.Add("SecondName", DropDownListSecondName.Text)<br />
SqlDataSource1.CancelSelectOnNullParameter = False<br />
SqlDataSource1.DataBind()<br />
End Sub<br />
Button2_click:
Button2_click:
<br />
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click<br />
Dim MyFilter As String = ""<br />
Dim Count As Integer = 0<br />
Dim i As Integer<br />
For i = 0 To CheckBoxListFirstName.Items.Count - 1<br />
If CheckBoxListFirstName.Items(i).Selected Then<br />
Count += 1<br />
If MyFilter = "" Then<br />
MyFilter = " WHERE FirstName = @FirstName Or @FirstName IS NULL"<br />
Else<br />
MyFilter = MyFilter & " OR FirstName = @FirstName Or @FirstName IS NULL"<br />
End If<br />
End If<br />
Next<br />
SqlDataSource1.CancelSelectOnNullParameter = False<br />
SqlDataSource1.SelectCommand = "SELECT * FROM test" & MyFilter<br />
SqlDataSource1.SelectParameters.Add("FirstName", CheckBoxListFirstName.Items(i).Text)<br />
SqlDataSource1.DataBind()<br />
End Sub<br />
Button3_click:
Button3_click:
<br />
Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click<br />
Dim parms As ArrayList = New ArrayList<br />
For Each li As ListItem In Me.CheckBoxListFirstName.Items<br />
If li.Selected Then<br />
parms.Add(li.Value)<br />
SqlDataSource1.CancelSelectOnNullParameter = False<br />
SqlDataSource1.SelectCommand = "SELECT * FROM test WHERE FirstName = @FirstName Or @FirstName IS NULL"<br />
SqlDataSource1.SelectParameters.Add("FirstName", li.Value)<br />
SqlDataSource1.DataBind()<br />
End If<br />
Next<br />
End Sub
Button4_click:
Button4_click:
<br />
Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click<br />
For Each strFirstName As String In CheckBoxListFirstName.Items<br />
SqlDataSource1.SelectCommand = "SELECT * FROM test WHERE FirstName = @FirstName Or @FirstName IS NULL"<br />
SqlDataSource1.SelectParameters.Add("FirstName", strFirstName)<br />
Next<br />
End Sub<br />
在此先感谢您的帮助!
Thanks in advance for help!
推荐答案
这篇关于尝试使用sql参数和checkboxlists作为sqldatasource.selectparameter控件过滤gridview/sqldatasource时出错.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!