display SqlDataReader values bound to labels MSSQL C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dibwas
    New Member
    • Apr 2010
    • 3

    display SqlDataReader values bound to labels MSSQL C#

    I have very limited knowledge on asp .net using c#. I am trying to fetch data from my mssql database and display the value on my page. I have succeeded to display it using datagrid. But I want it to diplay attached to labels or text boxes.

    Below is the code that I have made but it is throwing error

    Code:
    <%@ Page Language="c#" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">
    
        void Page_Load()
        {
                string connectionStr =
    "server=63.45.234.23;uid=MSSqldbdbdb;pwd=password;database=MSSqldbdbdb";
                string queryStr = "SELECT * FROM dibs_test";
        
                SqlConnection connectObj = new
    SqlConnection(connectionStr);
                SqlCommand commandObj = new
    SqlCommand(queryStr,connectObj);
        
                SqlDataReader readerObj;
        
                connectObj.Open();
                readerObj=commandObj.ExecuteReader();
        
                // display datareader contents into html table
    		   // first open the table and set up the table headers
    		   html += "<table cellspacing=1 class='data' width=90%>";
    		   html += "<tr>";
    		   html += "<th>Customer ID</th>";
    		   html += "<th>Company Name</th>";
    		   html += "</tr>";
    		
    		   // loop thru the reader
    		   while ( readerObj.Read ( ) ) {
    			  html += "<tr>";
    			  html += "<td>" + readerObj.GetInt ( 0 ) + "</td>";
    			  html += "<td>" + readerObj.GetString ( 1 ) + "</td>";
    			  html += "</tr>";
    		   }
    		
    		   // close the table
    		   html += "</table>";
        }
    
    </script>
    <html>
    <head>
        <title>Record</title>
    </head>
    <body>
        <h2>Use DataReader to display table with variables not using datagrid
        </h2>
        <%=html%>
    </body>
    </html>
    
    </html>
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It would really help if you stated what the error message was saying ;)
    I thought you said you were using a "DataGrid" to display your data? But looking at your code it's clear that you are not using a "DataGrid" at all! You're dynamically creating an HTML table based on the data retrieved from the database.

    There is an easier (better?) way of doing this.
    Use the GridView control instead.

    So, after you have retrieved the data from the database, instead of dynamically creating the table, set the GridView's DataSource property to the table retrieved and use the GridView.DataBi nd() method to bind the data to the grid.

    In the GridView, you can specify how the data is displayed.
    You do this using TemplateFields and other types of "columns".
    You can use labels in the template to make sure that the data is displayed exactly the way you want it to.

    -Frinny

    Comment

    • dibwas
      New Member
      • Apr 2010
      • 3

      #3
      Hii,

      Thanks for replying.

      I have used datagrid view... and that works correct..

      I didn't knew I could design a datagrid.. so I was trying to generate HTML code instead.

      Below is the code with datagrid that worked.

      Code:
      <%@ Page Language="c#" %>
      <%@ import Namespace="System.Data" %>
      <%@ import Namespace="System.Data.SqlClient" %>
      <script runat="server">
      
          void Page_Load()
          {
                  string connectionStr =
      "server=63.45.234.23;uid=MSSqldbdbdb;pwd=password;database=MSSqldbdbdb";
                  string queryStr = "SELECT * FROM dibs_test";
          
                  SqlConnection connectObj = new
      SqlConnection(connectionStr);
                  SqlCommand commandObj = new
      SqlCommand(queryStr,connectObj);
          
                  SqlDataReader readerObj;
          
                  connectObj.Open();
                  readerObj=commandObj.ExecuteReader();
          
                  dataGridControl.DataSource = readerObj;
          
                  dataGridControl.DataBind();
          
          }
      
      </script>
      <script language="javascript">
      alert("dibs");
      </script>
      <html>
      <head>
          <title>Visualizzo i record</title>
      </head>
      <body>
          <h2>Uso DataReader per visualizzare il contenuto di una tabella 
          </h2>
          <asp:DataGrid id="dataGridControl" runat="server"></asp:DataGrid>
      </body>
      </html>
      Can you provide some simple steps to display image, links, designed text ... etc with data fetched from database and displayed into datagrid.


      Thanks

      Dibs

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Instead of relying on the DataGrid to determine what columns to display you manually add the columns. To do this you need to use the TemplateField class this lets you specify controls that are bound to your data source so that you can display the data the way you want to.

        It has been a very long time since I've used a DataGrid so I'm not even sure if the TemplateField works with this control.

        Why are you using the DataGrid when the GridView is the updated, new-and-improved version that has been around for many years now?

        -Frinny

        Comment

        Working...