Friday, March 16, 2012

Newbie needs some hand holding - dont know where to put the code.

Hi all. I have never created anything in ASP.NET but over the past week I've been trawling the web for some answer and I'm struggling.

What i need is an aspx page that I can call from flash parsing the a ID to my sql statement and it returns me the data to display in Flash.

So far what I've managed to achieve is the following.

1. i create a new web site. I've managed to get it all up and running in IIS and it connects to the MsSQL Database. I'm using 2005 and VB script. I can get my values to display in a Grid view but what i need it to do is execute a function on page load. Now someone told me put the code in code behind but I have no idea what that means.

the code i got from a tutorial is as follows:

Private Function GetXML(ByVal SQLCom As SQLCommand, ByVal TableName As String) As XMLDocument

Dim Temp As New XmlDocument()

With SQLCom

.Connection.Open()

Dim i As Integer

Dim SQLData As SqlDataReader = .ExecuteReader

With SQLData

Temp.AppendChild(Temp.CreateElement(TableName))

While .Read

Temp.FirstChild.AppendChild(Temp.CreateElement("item"))

For i = 0 To .FieldCount - 1

Temp.FirstChild.LastChild.Attributes.Append(Temp.CreateAttribute(.GetName(i))).Value = .GetValue(i)

Next

End While

End With

.Connection.Close()

End With

Return Temp

End Function

Response.ContentType = "text/xml"

GetData(SQLProductsGet, "products").Save(Response.OutputStream)

My first problem, i don't know how or where to put this code. I'm supposed to be able to call the getXML function in flash.

Secondly, does this code return an xml file?

if anyone can help or suggest an alternative solution I'd be most grateful.

Many thanks.

blu.

Hello my friend,

Yes the output of this page will be an xml file, courtesy of the "Response.ContentType = "text/xml" line.

To put this code into your aspx page you have 2 choices. If your aspx page has a code-behind file you can put it in there. In asp.net, you can code aspx pages primarily in either VB.NET or c#. If you are coding them in VB.NET, your pages may have vb code behind files. Default.aspx will have another file called Default.aspx.vb, Hello.aspx will have a file called Hello.aspx.vb. If coding in C#, they will have cs instead of vb. The code you have is in VB.NET.

But you do not need to put the code in a code behind. You can instead put it into the actual aspx page that contains the html via the script tag. I give an example page below: -

<%@. Page Language="VB" AutoEventWireup="true" CodeFile="TestPage.aspx.vb" Inherits="TestPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

' paste code in here

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

Kind regards

Scotty


hi scotty thank for your response.

Still no joy though :-(

i created a new Web form page - then put this code in... (there isn't a aspx.vb file that was created - to i need to manually create one)

<%@.PageLanguage="VB"AutoEventWireup="true"CodeFile="careers.aspx.vb"Inherits="careers" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scriptrunat="server">

PrivateFunction GetXML(ByVal SQLComAs SQLCommand,ByVal TableNameAsString)As XMLDocument

Dim TempAsNew XmlDocument()

With SQLCom

.Connection.Open()

Dim iAsInteger

Dim SQLDataAs SqlDataReader = .ExecuteReaderWith SQLData

Temp.AppendChild(Temp.CreateElement(TableName))

While .Read

Temp.FirstChild.AppendChild(Temp.CreateElement("item"))For i = 0To .FieldCount - 1

Temp.FirstChild.LastChild.Attributes.Append(Temp.CreateAttribute(.GetName(i))).Value = .GetValue(i)

Next

EndWhile

EndWith

.Connection.Close()

EndWith

Return Temp

EndFunction

Response.ContentType ="text/xml"

GetData(SQLProductsGet,"careers").Save(Response.OutputStream)

</script>

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headid="Head1"runat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

</div>

</form>

</body>

</html>

i get a whole bunch of errors that I'd appreciate some help with

Validation(): Element 'html' occurs too few times

'Context is not a member of careers (Careers is my page name

Class connectors_careers.aspx must implement REadOnlyPropery is Reusable() ...

... must implement Sub ProcessRequest for interface System.WebIHttpHandler

GetWrappedFileDependecies is not a member of ASPConnectors_careers.aspx

sub processRequest cannot be declared 'Overrides' because it does not overide a sub in a base class.

process request is not a member of careers

there's a whole bunch of others bt i'm guessing they're related.

also some Declaration expected errors.

do i need to import something (as i would in java)?

thanks scotty


hi scotty thank for your response.

Still no joy though :-(

i created a new Web form page - then put this code in... (there isn't a aspx.vb file that was created - to i need to manually create one)

<%@.PageLanguage="VB"AutoEventWireup="true"CodeFile="careers.aspx.vb"Inherits="careers" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scriptrunat="server">

PrivateFunction GetXML(ByVal SQLComAs SQLCommand,ByVal TableNameAsString)As XMLDocument

Dim TempAsNew XmlDocument()

With SQLCom

.Connection.Open()

Dim iAsInteger

Dim SQLDataAs SqlDataReader = .ExecuteReaderWith SQLData

Temp.AppendChild(Temp.CreateElement(TableName))

While .Read

Temp.FirstChild.AppendChild(Temp.CreateElement("item"))For i = 0To .FieldCount - 1

Temp.FirstChild.LastChild.Attributes.Append(Temp.CreateAttribute(.GetName(i))).Value = .GetValue(i)

Next

EndWhile

EndWith

.Connection.Close()

EndWith

Return Temp

EndFunction

Response.ContentType ="text/xml"

GetData(SQLProductsGet,"careers").Save(Response.OutputStream)

</script> <htmlxmlns="http://www.w3.org/1999/xhtml">

<headid="Head1"runat="server">

<title>Untitled Page</title> </head>

<body>

<formid="form1"runat="server">

<div>

</div>

</form> </body>

</html>

i get a whole bunch of errors that I'd appreciate some help with

Validation(): Element 'html' occurs too few times

'Context is not a member of careers (Careers is my page name

Class connectors_careers.aspx must implement REadOnlyPropery is Reusable() ...

... must implement Sub ProcessRequest for interface System.WebIHttpHandler

GetWrappedFileDependecies is not a member of ASPConnectors_careers.aspx

sub processRequest cannot be declared 'Overrides' because it does not overide a sub in a base class.

process request is not a member of careers

there's a whole bunch of others bt i'm guessing they're related.

also some Declaration expected errors.

do i need to import something (as i would in java)?

thanks scotty


sorry pressed enter too many times there. just want to say i noticed that its now created an aspx.vb file - also this tutorial is asp.net 1.0 how would it be different for asp.net 2.0?

0 comments:

Post a Comment