简体   繁体   中英

Displaying results from DB query - Do I need to bind?

I am displying a list of properties that I am getting from a database. I am used to working in classic ASP and simply create a connection to the DB and then loop through the results and display them like this.

My question is, should I do the same in .NET or should I bind the data to an element? I prefer to loop through the results as I want to display the results in a div with styles.

here is my code:

       SqlConnection conn = new SqlConnection(
        "Data Source=xxxx;Initial Catalog=Rets_Full_local;Integrated Security=True");
        SqlDataReader rdr = null;

       try
       {
           conn.Open();

           // 3. Pass the connection to a command object
           SqlCommand cmd = new SqlCommand(
               "select * from RETS_Listings_full" +
               " WHERE 1 =1 " + sqlWhere
               , conn);

           rdr = cmd.ExecuteReader();

           String resultsDiv = "";

           while (rdr.Read())
           {
               String rdr_MLSNUM = (rdr["MLSNUM"]).ToString();
               String rdr_SYSID = (rdr["SYSID"]).ToString();
               String rdr_LISTPRICE = (rdr["LISTPRICE"]).ToString();
               String rdr_PropType = (rdr["PROPTYPE"]).ToString();
               String rdr_Beds = (rdr["beds"].ToString());
               String rdr_BathsFull = (rdr["BATHSFULL"].ToString());
               String rdr_BathsHalf = (rdr["BATHSHALF"].ToString());
               String rdr_AREA = (rdr["AREA"]).ToString();
               String rdr_STREETNUM = (rdr["STREETNUM"]).ToString();
               String rdr_STREETNAME = (rdr["STREETNAME"]).ToString();
               String rdr_City = (rdr["CITY"]).ToString();
               String rdr_View = (rdr["VIEWDESC"]).ToString();

               resultsDiv += "<div class='row-fluid'>";
               resultsDiv += "<div class='span4'>";
                resultsDiv += " <img src='http://www.hiltonheadrealestates.com/listings/Photo" + rdr_SYSID + "-1.jpeg' width='250px'>";
            resultsDiv += "</div>";
            resultsDiv += " <div class='span8' >";
                resultsDiv += "List Price:  $" + rdr_LISTPRICE +"<br />";
                resultsDiv += "Property Type: " + rdr_PropType +"<br />";
                resultsDiv += "Address : " + rdr_STREETNUM + " " + rdr_STREETNAME + "<br />";
                resultsDiv += "Location : " + rdr_City + "<br />";
                resultsDiv += "Area : " + rdr_AREA + "<br />";
                resultsDiv += "Bedrooms: " + rdr_Beds  + "<br />";
                resultsDiv += "Full Baths: " +rdr_BathsFull+    "<br />";
                resultsDiv += "Half Baths: " +rdr_BathsHalf+"<br />";
                resultsDiv += "View: " + rdr_View + "<br />";
            resultsDiv += "   </div>";
            resultsDiv += "   </div> <hr>";



           }

           // print results to div
           if (rdr == null || !rdr.HasRows)
           {
               div_Results.Text = "No Results - Please click <a href='/'>here</a> to search again";
           }
           else
           {
               div_Results.Text = resultsDiv;
           }
           // End print results to div

       }
       finally
       {
           // close the reader
           if (rdr != null)
           {
               rdr.Close();
           }

           // 5. Close the connection
           if (conn != null)
           {
               conn.Close();
           }
       }
   }

You should use a bindable control. Keeping most of the business logic in the codebehind (the .aspx.cs file) and the presentation logic in the aspx (the .aspx file)

Repeater is the most simple control for displaying collections and keeping complete control on the markup.

Welcome to ASP.NET

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