0

i am using these using statements-

  using System;
  using System.Data;
  using System.Data.Odbc;
  using System.Configuration;
  using System.Collections;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using System.Data.SqlClient;

and this is me attempting to get the term info into a dropdownlist

   protected void getAppTerm()
         {
            string status, y;
           y = "";
            string CommandText = "select term from Terms";
            OdbcConnection myConnection = dbconnect();
            OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);

        try
        {
            myConnection.Open();
            OdbcDataReader reader = myCommand.ExecuteReader();
            while (reader.Read())
            {
                status = reader.GetString(0);
                foreach (ListItem item in ddlApplicationTerm.Items)
                {
                    y = item.Text;
                }
                if (!(status == y))
                {
                    ddlApplicationTerm.Items.Add(status);
                }
            }

        }
        catch (OdbcException ex)
        {

        }
        finally
        {
            myConnection.Close();
        }
    }

The info is not going into the drop down list. i have all of the drivers installed too.

2
  • Any particular reason you're not using the MySQL Connector for .Net? Commented Jul 14, 2011 at 19:31
  • Your catch block is empty. Did you check if it threw any exception? Commented Jul 14, 2011 at 19:33

3 Answers 3

2
protected void getAppTerm()
{
    string CommandText = "select term from Terms";
    OdbcConnection myConnection = dbconnect();
    OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);

    try
    {
        myConnection.Open();
        OdbcDataReader reader = myCommand.ExecuteReader();
        while (reader.Read())
        {
            // Currently, you're overwriting the variable "y" on every iteration
            // and then just comparing the last item.text to status.

            var status = reader.GetString(0);
            if (!ddlApplicationTerm.Items.Contains(status)
                ddlApplicationTerm.Items.Add(status);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
    finally
    {
        myConnection.Close();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for complete example and doing something in the catch block
0

Try using the SqlConnection items if your data is in an SQL server.

SqlConnection myConn = new SqlConnection();
SQLCommand myCommand = new SQLCommand("Select term from Terms", myConn);
SqlDataReader myDR = myCommand.ExecuteReader;

1 Comment

OP's question title and tags say MySQL.
0

If you're trying to add items that aren't in the list, you need to adjust the code in your foreach loop:

foreach (ListItem item in ddlApplicationTerm.Items)
{
    if (!Status == item.Text)
    {
        ddlApplicationTerm.Items.Add(new ListItem(status));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.