Results 1 to 8 of 8

Thread: [RESOLVED] How do I get my output inside the table and not at the beginning of the document?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Resolved [RESOLVED] How do I get my output inside the table and not at the beginning of the document?

    Hi everyone,

    Long time ASP 3.0 user, ultra n00b to ASP.NET. I've finally found an example of using an SqlDataReader on 4GuysFromRolla which is working a treat, I feel like I'm at home again (as opposed to using data binding which is new to me).

    The reason I am using a DataReader is because I need to perform some on-the-fly calculations which seems far too difficult in a DataGrid (and too heavy, not what it's designed for etc).

    At any rate, I am executing a very simple query, which I hope to build a table from. The obvious issue I am having is, the sub Page_Load is called when the page is first loaded (regardless of where this sub is located within the ASPX file).

    I have tried a couple of options to get the data in the correct position in the HTML, but these seem cumbersome. I.e. Storing the output in a string and doing a Response.Write of this string in the correct position or creating this as a seperate function which executes at the correct position. Neither work.

    I obviously don't have any understand on how ASP.NET pages are constructed.

    Here be the code, I hope someone can point out how ridiculously simple this is when you know what to do!

    VB.Net Code:
    1. <%@ Import Namespace="System.Data" %>
    2. <%@ Import Namespace="System.Data.SqlClient" %>
    3.  
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    5.  
    6. <html xmlns="http://www.w3.org/1999/xhtml">
    7. <head runat="server">
    8.     <title></title>
    9. </head>
    10. <body>
    11. <table>
    12.     <tr>
    13.         <td>Quantitiy</td>
    14.         <td>Description</td>
    15.         <td>Item Price</td>
    16.         <td>Item Total</td>
    17.     </tr>
    18. <script language="VB" runat="server">
    19.    
    20.     Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    21.        
    22.         Dim strTableOutput As String = Nothing
    23.        
    24.         Dim blnError As Boolean = False
    25.        
    26.         Dim strCRM_GUID As String
    27.         strCRM_GUID = Request.QueryString("id")
    28.         If strCRM_GUID = Nothing Then
    29.             blnError = True
    30.         End If
    31.        
    32.         Dim strRef_Arrow As String
    33.         strRef_Arrow = Request.QueryString("Ref_Arrow")
    34.         If strRef_Arrow = Nothing Then
    35.             blnError = True
    36.         End If
    37.        
    38.         If Not blnError Then
    39.             'Create a connection string
    40.             Dim connString As String
    41.             connString = "Data Source=CCA-SQL;Initial Catalog=Arrow;Integrated Security=True;"
    42.    
    43.             'Open a connection
    44.             Dim objConnection As SqlConnection
    45.             objConnection = New SqlConnection(connString)
    46.             objConnection.Open()
    47.    
    48.             'Specify the SQL string
    49.             Dim strSQL As String = "SELECT [QUANTITY], [DESCRIPTION], [SELLING_PRICE], [NET_VALUE], [TAX_VALUE] FROM [ARROW_TO_CRM_DEBTOR_CHECKDETAIL] WHERE (([CRM_GUID] = '" & strCRM_GUID & "') AND ([REF_ARROW] = '" & strRef_Arrow & "'))"
    50.    
    51.             'Create a command object
    52.             Dim objCommand As SqlCommand
    53.             objCommand = New SqlCommand(strSQL, objConnection)
    54.  
    55.             'Get a datareader
    56.             Dim objDataReader As SqlDataReader
    57.             objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
    58.  
    59.             If Not objDataReader.HasRows Then
    60.                 'Output No Rows
    61.                 Response.Write("<TR><TD COLSPAN=""5"">There are no records available</TD></TR>")
    62.             Else
    63.                 'Output
    64.                 While objDataReader.Read()
    65.                     Response.Write("<TR><TD>" & objDataReader("QUANTITY") & "</TD></TR>")
    66.                 End While
    67.             End If
    68.            
    69.             'Close the datareader/db connection
    70.             objDataReader.Close()
    71.         Else
    72.             'Output No Rows
    73.             Response.Write("<TR><TD COLSPAN=""5"">There are no records available</TD></TR>")
    74.         End If
    75.          
    76.     End Sub
    77. </script>
    78. </table>
    79. </body>
    80. </html>

    This is the output I get:
    Code:
    <TR><TD>10.0000</TD></TR><TR><TD>4.0000</TD></TR><TR><TD>7.0000</TD></TR><TR><TD>20.0000</TD></TR><TR><TD>2.0000</TD></TR><TR><TD>9.0000</TD></TR><TR><TD>7.0000</TD></TR><TR><TD>1.0000</TD></TR>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>
    
    </title></head>
    <body>
    <table>
    
        <tr>
            <td>Quantitiy</td>
            <td>Description</td>
            <td>Item Price</td>
            <td>Item Total</td>
        </tr>
    
    </table>
    </body>
    </html>
    Also, I am using Visual Web Developer 2008 Express, not Visual Studio 2008.

    Cheers,
    Scoota

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: How do I get my output inside the table and not at the beginning of the document?

    Nevermind, I found that the repeater component does exactly what I want. Grr, hate finding things the hard way! It's generally the way though, when I'm involved!

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th

    Hey,

    Yip, sounds like you have gone down the correct route.

    As a rule of thumb, unless you are doing some fairly lower level control creation code, if you find yourself doing Response.Write's, look for an alternative, as there normally always is a better approach.

    Gary

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th

    Since you're new, move your code to the codebehind file so that you're starting out with best practices and a cleaner way of doing things.

    With the repeater, I presume you've define an ItemTemplate and are performing your calculations in there or in the ItemDataBound event.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th

    @gep13. Only the one Response.Write for the total, so I'm pretty happy with that. I can't believe how dramatically different ASP.NET is to ASP 3.0, although I can't say I'm surprised. Slowly getting my head around this postback theory...

    @mendhak. My code was in the codebehind file once I'd reverted to the Repeater, instead of that example I'd done the old copy + paste on.

    And yes, I am using the ItemDataBound event.

    Thanks!


  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th

    scootabug,

    Why do you still need the Response.Write? Can you not add a Label where needed, and then simply set the .Text property from the server side code?

    Gary

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th

    Or Literal.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th

    Yeah, that is what I meant

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width