Click here to Skip to main content
12,999,744 members (43,305 online)
Rate this:
 
Please Sign up or sign in to vote.
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-Bar318
Updated 29-Jan-13 10:48am
v2
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  
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2017
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