简体   繁体   中英

Displaying a list on ASPX page

I have this C# function returning a list, the function is present in a .dll file. How to display the list back on the ASPX page. I am new to ASP.Net and tried binding it to a grid but it doesn't work.

public static List<string>[] Select()
{

  server = "localhost";
  database = "cdl";
  uid = "root";
  password = "password";
  string connectionString;
  connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

  connection = new MySqlConnection(connectionString);

  connection.Open();

  string query = "SELECT * FROM caleiddevice";

  //Create a list to store the result
  List<string>[] list = new List<string>[3];
  list[0] = new List<string>();
  list[1] = new List<string>();
  list[2] = new List<string>();

  //Open connection
  // if (this.OpenConnection() == true)
  // {
  //Create Command
  MySqlCommand cmd = new MySqlCommand(query, connection);
  //Create a data reader and Execute the command
  MySqlDataReader dataReader = cmd.ExecuteReader();
  // MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(query, connection);
  //Read the data and store them in the list
  while (dataReader.Read())
  {
    list[0].Add(dataReader["device_id"] + "");
    list[1].Add(dataReader["status"] + "");
    list[2].Add(dataReader["timestamp"] + "");
  }

  //close Data Reader
  dataReader.Close();

  //close Connection
  // this.CloseConnection();

  //return list to be displayed
  return list;
  //}
  //else
  //{
  //   return list;
  // }
}
If (!IsPostBack)
{
  gridView1.DataSource=select();
  gridView1.DataBind();
}

Put this in page load and it works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM