Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET .NET4.5
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()
Thank-you for your help,
 
D
Posted 29-Jan-13 8:52am
D-Bar310
Edited 29-Jan-13 10:48am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
  Permalink  
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 

End IF
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,631
1 Sergey Alexandrovich Kryukov 6,345
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100