[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:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!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 runat="server">
<title></title>
</head>
<body>
<table>
<tr>
<td>Quantitiy</td>
<td>Description</td>
<td>Item Price</td>
<td>Item Total</td>
</tr>
<script language="VB" runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim strTableOutput As String = Nothing
Dim blnError As Boolean = False
Dim strCRM_GUID As String
strCRM_GUID = Request.QueryString("id")
If strCRM_GUID = Nothing Then
blnError = True
End If
Dim strRef_Arrow As String
strRef_Arrow = Request.QueryString("Ref_Arrow")
If strRef_Arrow = Nothing Then
blnError = True
End If
If Not blnError Then
'Create a connection string
Dim connString As String
connString = "Data Source=CCA-SQL;Initial Catalog=Arrow;Integrated Security=True;"
'Open a connection
Dim objConnection As SqlConnection
objConnection = New SqlConnection(connString)
objConnection.Open()
'Specify the SQL string
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 & "'))"
'Create a command object
Dim objCommand As SqlCommand
objCommand = New SqlCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader As SqlDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
If Not objDataReader.HasRows Then
'Output No Rows
Response.Write("<TR><TD COLSPAN=""5"">There are no records available</TD></TR>")
Else
'Output
While objDataReader.Read()
Response.Write("<TR><TD>" & objDataReader("QUANTITY") & "</TD></TR>")
End While
End If
'Close the datareader/db connection
objDataReader.Close()
Else
'Output No Rows
Response.Write("<TR><TD COLSPAN=""5"">There are no records available</TD></TR>")
End If
End Sub
</script>
</table>
</body>
</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
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!
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
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.
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!
:thumb:
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
Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th
Re: [RESOLVED] How do I get my output inside the table and not at the beginning of th
Yeah, that is what I meant :blush: