开发者

getting info from mysql database in asp.net

开发者 https://www.devze.com 2023-03-20 15:10 出处:网络
i am using these using statements- using System; using System.Data; using System.Data.Odbc; using System.Configuration;

i am using these using statements-

  using System;
  using System.Data;
  using System.Data.Odbc;
  using System.Configuration;
  using System.Collections;
  u开发者_如何学运维sing 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.


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();
    }
}


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;


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));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号