尝试使用下拉列表作为过滤器 [英] Trying to use drop down list as filter

查看:87
本文介绍了尝试使用下拉列表作为过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,感谢您查看我的帖子。我试图使用下拉列表作为过滤器,然后在gridview中显示值。我浏览了整个互联网并找到了几种方法。我无法让他们中的任何一个工作。这是我正在使用的代码。我收到错误Fill:SelectCommand.Connection属性尚未初始化。

Hello, Thanks for looking at my post. I am trying to use a dropdown list to act as a filter then display the values in a gridview. I looked all over the internet and found several ways. I have not been able to get any of them to work. Here is the code I am using. I am getting an error "Fill: SelectCommand.Connection property has not been initialized."

Dim con As New SqlConnection(my connection to the database)
Dim command As New SqlCommand("select * from grades where test = "      Convert.ToString(Me.DropDownList2.SelectedValue))
Dim dataAadpter As New SqlDataAdapter(command)
con.Open()
Dim ds As New DataSet()
dataAadpter.Fill(ds)
GridView2.DataSource = ds
GridView2.DataBind()
GridView2.Visible = True
con.Close()



感谢您的帮助,



D


Thank-you for your help,

D

推荐答案

您还没有添加连接属性。将其添加到您的代码中:

You have not added the connection property. Add this to your code:
Dim command As New SqlCommand("select * from grades where test = ''" + Convert.ToString(Me.DropDownList2.SelectedValue) + "''", con)


试试gridview绑定的正确方法



try this right way of gridview bound

DataTable dt = null;
        using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tempdbConn"].ConnectionString))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select row_number() OVER (ORDER BY id) AS sno,id,uname,totalMarks from tmp_table";
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    dt = new DataTable();
                    da.Fill(dt);
                }
            }
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();



谢谢


Thanks


首先检查下拉条件是否为空。< br $> b $ b



First check condition for dropdown whether it is null or not.


if (DropDownList2 isnot nothing) then


  If (DropDownList2.SelectedValue.ToString.Length > 0) Then
       // put all your fetch code here.
  End If



< br $> b $ b

结束IF




End IF


这篇关于尝试使用下拉列表作为过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆