Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Access Database, ASP/C# Drop Down List populating a Drop Down List

Access Database, ASP/C# Drop Down List populating a Drop Down List

Scheduled Pinned Locked Moved ASP.NET
csharpdatabasesysadminlounge
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WickedFooker
    wrote on last edited by
    #1

    Ok this has sort of been asked before but also somewhat differently here. I have a regular drop down (not connected to a database) that just has the topics of subjects. This is a sample of what I have:

                       Please Select
                       English
                       Math
                       Social Studies
                       Science
                       History
    

    I want to make it so that when it changes the next part will search and select teachers that teach that specific subject. I am using an Access Database for connection. I have pieced together some of what others did in an attempt to get it to work but no.

    protected void ddlcourseType_SelectedIndexChanged(object sender, EventArgs e)
    {
    OleDbConnection dbConn = null;
    OleDbCommand dbCmd;
    OleDbDataReader dr;
    String strConnection;
    String strSQL;
    string path = Server.MapPath("eAcademy_DB.mdb");

        string Teachable = Convert.ToString(ddlcourseType.SelectedValue);
        strConnection = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
        dbConn = new OleDbConnection(strConnection);
        dbConn.Open();
        strSQL = "select \* from tblEmployee where canTeach=" + Teachable;
    
        dbCmd = new OleDbCommand(strSQL, dbConn);
        DataSet ds = new DataSet();
                
        dbConn.Close();
        ddlTeacher.DataSource = ds;
        ddlTeacher.DataTextField = "emp\_ID";
        ddlTeacher.DataValueField = "emp\_ID";
        ddlTeacher.DataBind();
        ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0"));
        if (ddlTeacher.SelectedValue == "0")
        {
            ddlTeacher.Items.Clear();
            ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }
    

    I think what I really need to do is make a for loop and have it iterate for the amount of teachers that are in the list, and then have it populate the list, but not sure how exactly to do thi

    Richard DeemingR C 2 Replies Last reply
    0
    • W WickedFooker

      Ok this has sort of been asked before but also somewhat differently here. I have a regular drop down (not connected to a database) that just has the topics of subjects. This is a sample of what I have:

                         Please Select
                         English
                         Math
                         Social Studies
                         Science
                         History
      

      I want to make it so that when it changes the next part will search and select teachers that teach that specific subject. I am using an Access Database for connection. I have pieced together some of what others did in an attempt to get it to work but no.

      protected void ddlcourseType_SelectedIndexChanged(object sender, EventArgs e)
      {
      OleDbConnection dbConn = null;
      OleDbCommand dbCmd;
      OleDbDataReader dr;
      String strConnection;
      String strSQL;
      string path = Server.MapPath("eAcademy_DB.mdb");

          string Teachable = Convert.ToString(ddlcourseType.SelectedValue);
          strConnection = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
          dbConn = new OleDbConnection(strConnection);
          dbConn.Open();
          strSQL = "select \* from tblEmployee where canTeach=" + Teachable;
      
          dbCmd = new OleDbCommand(strSQL, dbConn);
          DataSet ds = new DataSet();
                  
          dbConn.Close();
          ddlTeacher.DataSource = ds;
          ddlTeacher.DataTextField = "emp\_ID";
          ddlTeacher.DataValueField = "emp\_ID";
          ddlTeacher.DataBind();
          ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0"));
          if (ddlTeacher.SelectedValue == "0")
          {
              ddlTeacher.Items.Clear();
              ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0"));
          }
      }
      

      I think what I really need to do is make a for loop and have it iterate for the amount of teachers that are in the list, and then have it populate the list, but not sure how exactly to do thi

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      WickedFooker wrote:

      strSQL = "select * from tblEmployee where canTeach=" + Teachable;

      Your first problem is SQL Injection[^]. Never use string concatenation to insert parameters into a query; use a parameterized query instead.

      WickedFooker wrote:

      dbCmd = new OleDbCommand(strSQL, dbConn); DataSet ds = new DataSet(); dbConn.Close();

      Creating an OleDbCommand object isn't going to execute the query, let alone store the results in another variable. You could use ExecuteReader, iterate through the results, and add them to a DataTable, but it's much simpler to use a DataAdapter to do the work for you.

      WickedFooker wrote:

      if (ddlTeacher.SelectedValue == "0") {    ddlTeacher.Items.Clear();    ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0")); }

      Since you've just inserted an item with a value of "0" as the first item, this will most likely be the selected item. Even if your data-binding was working, this block of code will most likely throw away the results from the database and leave you with an empty list. Try something like this:

      protected void ddlcourseType_SelectedIndexChanged(object sender, EventArgs e)
      {
      string path = Server.MapPath("eAcademy_DB.mdb");
      string connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
      string commandText = "SELECT * FROM tblEmployee WHERE canTeach = ?";

      var ds = new DataSet();

      using (var connection = new OleDbConnection(connectionString))
      using (var command = new OleDbCommand(commandText, connection))
      {
      // OleDbCommand uses positional, rather than named, parameters.
      // The parameter name doesn't matter; only the position

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      W 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        WickedFooker wrote:

        strSQL = "select * from tblEmployee where canTeach=" + Teachable;

        Your first problem is SQL Injection[^]. Never use string concatenation to insert parameters into a query; use a parameterized query instead.

        WickedFooker wrote:

        dbCmd = new OleDbCommand(strSQL, dbConn); DataSet ds = new DataSet(); dbConn.Close();

        Creating an OleDbCommand object isn't going to execute the query, let alone store the results in another variable. You could use ExecuteReader, iterate through the results, and add them to a DataTable, but it's much simpler to use a DataAdapter to do the work for you.

        WickedFooker wrote:

        if (ddlTeacher.SelectedValue == "0") {    ddlTeacher.Items.Clear();    ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0")); }

        Since you've just inserted an item with a value of "0" as the first item, this will most likely be the selected item. Even if your data-binding was working, this block of code will most likely throw away the results from the database and leave you with an empty list. Try something like this:

        protected void ddlcourseType_SelectedIndexChanged(object sender, EventArgs e)
        {
        string path = Server.MapPath("eAcademy_DB.mdb");
        string connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
        string commandText = "SELECT * FROM tblEmployee WHERE canTeach = ?";

        var ds = new DataSet();

        using (var connection = new OleDbConnection(connectionString))
        using (var command = new OleDbCommand(commandText, connection))
        {
        // OleDbCommand uses positional, rather than named, parameters.
        // The parameter name doesn't matter; only the position

        W Offline
        W Offline
        WickedFooker
        wrote on last edited by
        #3

        I just want to thank you for the help! This did the trick. I was able to populate the 2nd table with perhaps the smallest of changes.

        string commandText = "SELECT * FROM tblEmployee WHERE canTeach = " + ddlcourseType.SelectedValue;

        I removed the select on the bottom and instead put it in the page load since it is more needed for looks when the page loads. Thanks again for your help. I am certain I will be back for more :) REVISED: I see your warning about String concatenation. I will change it back. I went back to the way you originally posted it!

        1 Reply Last reply
        0
        • W WickedFooker

          Ok this has sort of been asked before but also somewhat differently here. I have a regular drop down (not connected to a database) that just has the topics of subjects. This is a sample of what I have:

                             Please Select
                             English
                             Math
                             Social Studies
                             Science
                             History
          

          I want to make it so that when it changes the next part will search and select teachers that teach that specific subject. I am using an Access Database for connection. I have pieced together some of what others did in an attempt to get it to work but no.

          protected void ddlcourseType_SelectedIndexChanged(object sender, EventArgs e)
          {
          OleDbConnection dbConn = null;
          OleDbCommand dbCmd;
          OleDbDataReader dr;
          String strConnection;
          String strSQL;
          string path = Server.MapPath("eAcademy_DB.mdb");

              string Teachable = Convert.ToString(ddlcourseType.SelectedValue);
              strConnection = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
              dbConn = new OleDbConnection(strConnection);
              dbConn.Open();
              strSQL = "select \* from tblEmployee where canTeach=" + Teachable;
          
              dbCmd = new OleDbCommand(strSQL, dbConn);
              DataSet ds = new DataSet();
                      
              dbConn.Close();
              ddlTeacher.DataSource = ds;
              ddlTeacher.DataTextField = "emp\_ID";
              ddlTeacher.DataValueField = "emp\_ID";
              ddlTeacher.DataBind();
              ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0"));
              if (ddlTeacher.SelectedValue == "0")
              {
                  ddlTeacher.Items.Clear();
                  ddlTeacher.Items.Insert(0, new ListItem("--Select--", "0"));
              }
          }
          

          I think what I really need to do is make a for loop and have it iterate for the amount of teachers that are in the list, and then have it populate the list, but not sure how exactly to do thi

          C Offline
          C Offline
          cyber_addicted
          wrote on last edited by
          #4

          i think you are missin to fill your dataset your command object not executing use

          DataSet ds = new DataSet();
          OleDbDataAdapter adapter = new OleDbDataAdapter(dbCmd)

          adapter.Fill(ds);

          after

          dbCmd = new OleDbCommand(strSQL, dbConn);

          then it fill dataset and your debug will show your dataset contain value.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups