title: Returning a random row from sql database and displaying in a html table using ASP.NET
body: <p>Im new to using ASP.NET and would like to know how I can select a random row from a sql database and then display the fields in a html table on a separate page. It is intended that the user can press on button which will retrieve a random movie from the database and then display the movie details in a html table on a new page. I am not sure how to go about this and have been trying to use labels to display the data. Here is a sample of the code so far:</p>
  
  <pre><code>    private SqlConnection conn;
      protected void Page_Load(object sender, EventArgs e)
      {
              ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings ["MovieAppConnectionString1"];
              conn = new SqlConnection(connString.ConnectionString);
      }
  
      protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
      {
          try
          {
              conn.Open();
              string queryString = "SELECT TOP 1 * FROM Movie ORDER BY NEWID()";
              SqlCommand cmd = new SqlCommand(queryString, conn);
              {
                  SqlDataReader reader = cmd.ExecuteReader();
                  StringBuilder MyStringBuilder = new StringBuilder();
                  while (reader.Read())
                  {
                      Image2.Text = reader[2].ToString();
                      Label1.Text = reader[1].ToString();
                      Desc.Text = reader[3].ToString();
                      Direc.Text = reader[5].ToString();
                      Strs.Text = reader[7].ToString();
                      Rtime.Text = reader[4].ToString();
                      ImdbRt.Text = reader[8].ToString();
                  }
              }
         }
          finally
          {
              conn.Close();
          }
          Server.Transfer("MovieSelected.aspx");
  </code></pre>