开发者

C# Populate Gridview from Dropdown choice

开发者 https://www.devze.com 2023-03-10 11:31 出处:网络
I have tried everything but can not figure out how to populate a gridview with my dropdown selection.I have checked the value of the dropdown and it is correct and the gridview will display all items

I have tried everything but can not figure out how to populate a gridview with my dropdown selection. I have checked the value of the dropdown and it is correct and the gridview will display all items but not the items that I want. I am using a web form. Below is my code.

protected void Page_Load(object sender, EventArgs e)
        {
            Populate();
        }

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
            myConnection.Open();

            try
            {
                SqlDataReader reader = null;
                string serverIP = drpChoose.SelectedItem.Value.ToString();
                SqlCommand cmd = new SqlCommand("SELECT * from ScheduledTasks WHERE ServerIP = " + serverIP, myConnection);

                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    GridView1.DataSource = reader;
                    GridView1.DataBind();
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            myConnection.Close();
        }

        public void Populate()
        {
            SqlConnection myConnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
            myConnection1.Open();

            SqlCommand cmd1 = new SqlCommand("SELECT ServerIP FROM Servers", myConnection1);
            SqlDataReader dropReader;
            dropReader = cmd1.ExecuteReader();

            drpChoose.DataSource = dropReader;
            drpCho开发者_C百科ose.DataTextField = "ServerIP";
            drpChoose.DataValueField = "ServerIP";
            drpChoose.DataBind();
        }
    }

Any help would be greatly appreciated.


You should also apply sql parameterization. It will help prevent SQL injection attacks. Which your code is currently vulnerable to.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx


You don't need rebind the dropdownlist after each post back otherwise you will loose the selected item. Try the below:

protected void Page_Load(object sender, EventArgs e)
        {
        if(!Page.IsPostBack)
            {
            Populate();                
            }

    }

    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
        myConnection.Open();

        try
        {
            SqlDataReader reader = null;
            string serverIP = drpChoose.SelectedItem.Value.ToString();
            SqlCommand cmd = new SqlCommand("SELECT * from ScheduledTasks WHERE ServerIP = " + serverIP, myConnection);

            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                GridView1.DataSource = reader;
                GridView1.DataBind();
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        myConnection.Close();
    }

    public void Populate()
    {
        SqlConnection myConnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
        myConnection1.Open();

        SqlCommand cmd1 = new SqlCommand("SELECT ServerIP FROM Servers", myConnection1);
        SqlDataReader dropReader;
        dropReader = cmd1.ExecuteReader();

        drpChoose.DataSource = dropReader;
        drpChoose.DataTextField = "ServerIP";
        drpChoose.DataValueField = "ServerIP";
        drpChoose.DataBind();
    }
}


Just a guide: Why not use the Ajaxtoolkit UpdatePanel control. Put the Gridview in the UpdatePanel, then set the dropdown selection as the Updatetrigger for the Gridview.Then you won't have to worry about postback and values.


Change your Page_Load method to:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack()) {
        Populate();
    }
}

On every page load, you are rebinding the dropdown selection so that the default "all" value will always be set. By using the Page.IsPostBack property, you're telling the page to only bind on the first load and not subsequent postbacks. By using this property, the selected item in the dropdown list will not change on postbacks.

MSDN: Page.IsPostBack

Update

Regarding your comment about the "Where" clause in your SQL statement, is serverIP stored in the database as text? Try wrapping serverIP in apostrophes like:

SqlCommand cmd = new SqlCommand("SELECT * from ScheduledTasks WHERE ServerIP = '" + serverIP + "'', myConnection);
0

精彩评论

暂无评论...
验证码 换一张
取 消