M_Bataineh
M_Bataineh

Reputation: 43

What is the way to populate a dropdownlist from a database in asp.net by using classes?

I am trying to populate a dropdownlist from sql server by using classes as shown below. The code breaks down when it comes to bind the data into the dropdown list. It gives an error on giving the dropdownlist the dataValueField and datatTextField.

HTML... a.aspx

<asp:DropDownList ID="NationalityDropDownList" runat="server"  >
</asp:DropDownList>

C#... a.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        Classes.Nationality PossibleNationality = new Classes.Nationality();
        if (!Page.IsPostBack)
        {              
            DataTable dataTable = PossibleNationality.getNationality();
            NationalityDropDownList.DataSource = dataTable;

            NationalityDropDownList.DataValueField = "ID";
            NationalityDropDownList.DataTextField = "Nationality";
            NationalityDropDownList.DataBind();
        }
    } 

Nationality.cs

 public class Nationality
 {
   public DataTable getNationality()
    {
        SqlConnection conn;
        SqlCommand comm;
        string  connectionString = ConfigurationManager.ConnectionStrings["InformationConnection"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("spGetAllUsers", conn);
        comm.CommandType = CommandType.StoredProcedure;
        DataTable dataTable;
        try
        {
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = comm;
            dataTable = new DataTable();
            da.Fill(dataTable);
        }
        finally
        {
            conn.Close();
        }
        return dataTable;
    }
}

SQL procedure....

ALTER PROCEDURE [dbo].[spGetNationalities] 
AS
BEGIN
    select * from Nationality;
END

Upvotes: 4

Views: 3428

Answers (2)

Just_Ice
Just_Ice

Reputation: 563

        if (!Page.IsPostBack)
        {
            try
            {
                using (SqlConnection con = new SqlConnection("Data Source = NIPOON; Initial Catalog = CustomerOrders; Integrated Security = true"))
                {
                    SqlCommand cmd = new SqlCommand("SELECT Name FROM Customer", con);
                    con.Open();

                    dropDownList.DataSource = cmd.ExecuteReader();
                    dropDownList.DataTextField = "Name";
                    dropDownList.DataValueField = "Name";
                    dropDownList.DataBind();
                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine("Error: " + Ex.Message);
            }

            GetData();
        }

Upvotes: -2

Scotty
Scotty

Reputation: 1137

This line of code in your getNationalitymethod...

comm = new SqlCommand("spGetAllUsers", conn);

...should be this instead

comm = new SqlCommand("spGetNationalities", conn);

Databinding should work if your Nationality table has columns ID and Nationality

Upvotes: 4

Related Questions