populate dropdown items via stored procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ellen89
    New Member
    • Nov 2007
    • 14

    populate dropdown items via stored procedure

    Hi All,

    I am new to this field, and is using VS.2005, language is C#.

    1) created a database "Test" in sql 2005, had a table "NameList", a stored proc "GetAllName "
    2) created a new website, on .aspx page has a dropdown control "ddlName"

    Wish could have help at following:

    Now I want to all the names from NameList will be populated at 'ddlName" when page loaded.

    There is no problem for me to complete this task using data adapter, but I want to use stored procedure.

    Thank you in advance. Your time is great appreciated.

    ellen89
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Please see the following article in our Howtos section.



    Nathan

    Comment

    • ellen89
      New Member
      • Nov 2007
      • 14

      #3
      nateraaaa,

      Thank you very much for your time.

      Now I had another problem, my code is:

      protected void Page_Load(objec t sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      string cnString = ConfigurationMa nager.Connectio nStrings["TestConnection String"].ConnectionStri ng;
      SqlConnection cnn = new SqlConnection(c nString);
      SqlCommand cmd = new SqlCommand("Get AllName", cnn);
      cmd.CommandType = CommandType.Sto redProcedure;
      cmd.CommandText = "GetAllName ";
      cnn.Open();

      SqlDataAdapter da = new SqlDataAdapter( );
      da.SelectComman d = cmd;
      DataSet ds = new DataSet();
      da.Fill(ds, "NameList") ;
      DropDownList1.D ataSource = ds;
      DropDownList1.D ataMember = "NameList";
      DropDownList1.D ataBind();

      cnn.Close();
      }
      }

      But the at Dropdown list will have "System.Data.Dat aRowView" instead of real data.

      Comment

      • saran23
        New Member
        • Mar 2008
        • 28

        #4
        Originally posted by ellen89
        nateraaaa,

        Thank you very much for your time.

        Now I had another problem, my code is:

        protected void Page_Load(objec t sender, EventArgs e)
        {
        if (!IsPostBack)
        {
        string cnString = ConfigurationMa nager.Connectio nStrings["TestConnection String"].ConnectionStri ng;
        SqlConnection cnn = new SqlConnection(c nString);
        SqlCommand cmd = new SqlCommand("Get AllName", cnn);
        cmd.CommandType = CommandType.Sto redProcedure;
        cmd.CommandText = "GetAllName ";
        cnn.Open();

        SqlDataAdapter da = new SqlDataAdapter( );
        da.SelectComman d = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds, "NameList") ;
        DropDownList1.D ataSource = ds;
        DropDownList1.D ataMember = "NameList";
        DropDownList1.D ataBind();

        cnn.Close();
        }
        }

        But the at Dropdown list will have "System.Data.Dat aRowView" instead of real data.
        Hi, The problem is in this line,
        Code:
        DropDownList1.DataSource = ds;
        ur binding a dataset to a dropdownlist, a dataset may contain more than one table also, so there comes the problem, even if u hv used Datamember,the table name to which the "NameList" column belongs is not specified,So u hv to do this,

        Code:
        DropDownList1.DataSource = ds.Tables[0]
        The '[0]' denotes the index of the datatable in the dataset.

        Im sure this works...

        Thanks
        Saran

        Comment

        • ellen89
          New Member
          • Nov 2007
          • 14

          #5
          Sarah,

          Thank you very much. I had tried, unfortunately, I have no lucky.

          I am not sure the .DataMember property, NameList is my table name, with 2 columns (firstName, lastName).

          Gusee I should not have the column name in my code because I am using stored procedure to get the result. This stored procedure is very simple and tested in sql server 2005.

          Thanks again,

          ellen89

          Comment

          • kunal pawar
            Contributor
            • Oct 2007
            • 297

            #6
            Hi, ur code correct but to display text in drop down control u have to set column name, same for value

            DropDownList1.D ataSource = ds;
            DropDownList1.D ataValueField = "Field name for selected item value"
            DropDownList1.D ataTextField = "Field name for selected item Text"
            DropDownList1.D ataBind();

            Comment

            • ellen89
              New Member
              • Nov 2007
              • 14

              #7
              Thank you!

              Now the problem is I want to show the full name on the dropdown list, but the Name is stored in databas as FirstName & LastName (2 columns).

              What should I put for DropDownList1.D ataTextField ?

              I had tried DropDownList1.D ataTextField = "FirstName+ ' '+LastName", it gave run time error.

              ellen89

              Comment

              • ellen89
                New Member
                • Nov 2007
                • 14

                #8
                I had solved this problem by using a VIEW table at SQL server to have FirstName and LastName at same column.

                Thank you for all help.

                ellen89

                Comment

                • kunal pawar
                  Contributor
                  • Oct 2007
                  • 297

                  #9
                  You not need to create view for this single reason, just add column in ur sql query
                  as FirstName + ' '+LastName as Name.

                  and

                  DropDownList1.D ataTextField = "Name"

                  Comment

                  Working...