简体   繁体   中英

Adding data to dropdown list from Azure database using ADO.net c#

I need to take that data that I get from the select statement and put it in a dropdown box, but I do not know what to do after I have the select statement. This is my current code. I am using c# and ADO.net

protected void Possition1_SelectedIndexChanged(object sender, EventArgs e)
{
    using (var connection = new QC.SqlConnection("Server=CONNECTIONINFO"))
    {
        connection.Open();

        using (var command = new QC.SqlCommand())
        {
            command.Connection = connection;
            command.CommandType = DT.CommandType.Text;
            command.CommandText = @"SELECT * [firstName], [lastName]
                                    FROM [dbo].[Players]
                                    WHERE [position] = 'QB'
                                    ORDER BY [firstName] ASC";
        }
    }
}

Any help will be greatly appreciated.

The query can be executed using ExecuteReader() function , thus the result can be placed in a dataSet to fill the dropdown.. if its an Asp Mvc doprdown the list can be placed in form of SelectListItem as value an text in the similar form mentioned below

 string constr = ConfigurationManager.ConnectionStrings["Server=CONNECTIONINFO"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * [firstName], [lastName]
                                    FROM [dbo].[Players]
                                    WHERE [position] = 'QB'
                                    ORDER BY [firstName] ASC"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                ddlPlayers.DataSource = cmd.ExecuteReader();
                ddlPlayers.DataTextField = "firstName";
                ddlPlayers.DataValueField = "PlayerId";
                ddlPlayers.DataBind();
                con.Close();
            }
        }
        ddlCustomers.Items.Insert(0, new ListItem("--Select Player--", "0"));

I hope this would help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM