Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETVB.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-Bar306
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 9,225
1 OriginalGriff 5,395
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


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