|
-
Jan 4th, 2010, 08:53 PM
#1
Thread Starter
Addicted Member
[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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|