locked
Extracting data from two columns in a table in SQL Server and displaying in two dropdownlists in ASP.Net respectively RRS feed

  • Question

  • User-1262319794 posted

    I have two dropdownlists in Asp.Net and a "Machines" table with two columns, IPAddress and MachineName in SQL Server. I am trying to extract data from both the columns in the table in SQL Server and put it in the dropdownlists. I was able to extract the IPAddress table only and put it in the first dropdownlist. I need to find out how to extract the data from the MachineName column and put it in the second dropdownlist. Below I have the Page_Load method with the code for the first dropdownlist for reference.

    protected void Page_Load(object sender, EventArgs e)
    {
            con.Open(); 
    
            string s = "Select distinct IPAddress from Machines order by IPAddress"; 
    
            SqlDataAdapter da = new SqlDataAdapter(s, con); 
    
            DataSet ds = new DataSet();
            da.Fill(ds); 
    
            DropDownList1.DataTextField = "IPAddress";
            DropDownList1.DataValueField = "IPAddress";
            DropDownList1.DataSource = ds;
            DropDownList1.DataBind(); 
    
            con.Close();
    }
    Friday, June 8, 2018 7:29 PM

All replies

  • User409696431 posted

    Can you elaborate on what you don't know how to do?  You are showing code that pulls one column's data into a dropdownlist.  Do the same for the other column, and the other dropdownlist.

    Friday, June 8, 2018 8:20 PM
  • User1120430333 posted

    Maybe you need to learn how to load the dropdownlist on the fly by reading the records in the datatable manually and loading the necessary data from the datatable manually. 

    "Creating dropdownlist on the fly"

    http://www.dotnetfunda.com/articles/show/30/several-ways-to-populate-dropdownlist-controls

    It means that you get all the data needed into the datatable,  and you read the datatable loading the dropdownlist manually from the datatable. Sometimes you can depend upon data binding on controls.

    Friday, June 8, 2018 11:27 PM
  • User-330142929 posted

    Hi Vijaylakshmi,

    From your description, I think we could achieve what you want by filtering datatable.

    As far as I know, The SQL statement distinct keyword is often used to filter individual field duplicate.

    For multiple field, I suggest you could filter datatable by datatable.defaultview.totable() method.

    Here is my workaround, wish it could be useful to you.

    Aspx.

    <div>
                <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
            </div>

    Code Behind.

      protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDataStore"].ConnectionString);
                    SqlCommand command = new SqlCommand();
                    command.CommandText = "select OrgID,CategoryID from tblFoods";
                    command.CommandType = System.Data.CommandType.Text;
                    command.Connection = connection;
                    connection.Open();
                    DataTable dt = new DataTable();
                    SqlDataAdapter sda = new SqlDataAdapter(command);
                    sda.Fill(dt);
                    this.DropDownList1.DataSource = dt.DefaultView.ToTable(true, "OrgId");
                    this.DropDownList1.DataTextField = "OrgID";
                    this.DropDownList1.DataValueField = "OrgID";
                    this.DropDownList1.DataBind();
                    this.DropDownList2.DataSource = dt.DefaultView.ToTable(true, "CategoryID");
                    this.DropDownList2.DataTextField = "CategoryID";
                    this.DropDownList2.DataValueField = "CategoryID";
                    this.DropDownList2.DataBind();
                }
            }

    How it works.

    Please feel free to let me know if you have any question.

     

    Best Regards,

    Abraham

    Thursday, June 14, 2018 10:33 AM