473,738 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EASY, RIGHT? : Retrieving SQL Data as an XML Document

Hello,

How do I retrieve SQL Data into an XML Document? I have the following
code, which will retrieve SQL data and write it to the screen via the
Response Object, but I'd like to be able to read it as XML Data, and
use XSL and XSLT to display the data.

Any suggestions? Code Snippets MORE than appreciated... :)

=============== =============== =============== =============== =====
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As

System.EventArg s) Handles
Button1.Click
Dim objConn As New OleDbConnection ( _
"Provider=MSDAO RA.1;User
ID=abc;DataSour ce=sdqa.ourcomp any.com;Passwor d=pwabc")

Dim objCmd As OleDbCommand
Dim objRdr As OleDbDataReader
Dim doc As XmlDocument
Dim trans As XslTransform

objConn.Open()
objCmd = New OleDbCommand("S elect * from z_test", objConn)
OleDbConnection 1.Open()
objRdr = objCmd.ExecuteR eader

While objRdr.Read
Response.Write( objRdr.Item("fi rstname") & _
"," & objRdr.Item("la stname") & _
"," & objRdr.Item("ci ty") & _
"," & objRdr.Item("st ate") & _
"<br />")
End While
objRdr.Close()

'doc = New XmlDocument
'doc.Load(Serve r.MapPath("chap ter8.xml"))

'trans = New XslTransform
'trans.Load(Ser ver.MapPath("ch apter8.xsl"))

'Xml1.Document = doc
'Xml1.Transform = trans

End Sub

Aug 7 '06 #1
5 1437


sa***********@y ahoo.ca wrote:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As

System.EventArg s) Handles
Button1.Click
Dim objConn As New OleDbConnection ( _
"Provider=MSDAO RA.1;User
ID=abc;DataSour ce=sdqa.ourcomp any.com;Passwor d=pwabc")
If you use SQL server then you can generate XML with a FOR XML query and
use the ExcuteXmlReader method
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemData SqlClientSqlCom mandClassExecut eXmlReaderTopic .asp>

If you want or need to use OleDb then I think one way is as follows
which gets the result of the query in a DataSet and then loads the
DataSet in an XmlDataDocument which can be used for XSLT transformations :

Dim dataset As DataSet
Dim conn As New OleDbConnection (connection)
Dim adapter As New OleDbDataAdapte r()
adapter.SelectC ommand = new OleDbCommand("S elect * from z_test", conn)
adapter.Fill(da taset)
Dim dataDocument as XmlDataDocument = new XmlDataDocument (dataset)

Dim trans As XslTransform
'trans = New XslTransform
'trans.Load(Ser ver.MapPath("ch apter8.xsl"))
Then here you can pass dataDocument as the first parameter to the
Transform method of the XslTransform trans object
trans.Transform (dataDocument, other arguments)
where other arguments obviously depends on which overload of the
Transform method you want to use.

My code snippets above are typed here in that post so don't expect that
to compile as typed, but you should get the idea on which objects to use.

Give some feedback on whether the outlined approach works for you.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 7 '06 #2
Hi Martin,

Thanks for the help. I've tried your snippet of code and it compiles
fine... Here's what I've tried:

=============== =============== =============== =============== =============== =========
Dim dataset As DataSet
Dim conn As New OleDbConnection ( _
"Provider=MSDAO RA.1;User ID=user1;Data
Source=sdqa.com pany.com;Passwo rd=password1")
Dim adapter As New OleDbDataAdapte r
adapter.SelectC ommand = New OleDbCommand("S elect firstname from
z_test", conn)
adapter.Fill(da taset)
Dim dataDocument As XmlDataDocument = New
XmlDataDocument (dataset)
=============== =============== =============== =============== =============== =========

Unfortunately, now I'm getting a "run time" error on the line
"adapter.Fill(d ataset)". The error states:

Exception Details: System.Argument NullException: Value cannot be null.
Parameter name: dataSet

Hmmm... When I create an OleDBConnection and an OleDBDataAdapte r
Object on the Web Form, and link them together, I'm able to bring up
the data when I "right-click" on the "QueryBuild er" and do "Run" on the
bottom table. This tells me that the program is able to connect to the
table and retrieve the information, at least via the GUI. For some
reason it will not work via this code.

Any ideas on what the problem could be, or how I could narrow this
problem down further?

Thanks a Bunch...

Aug 7 '06 #3


sa***********@y ahoo.ca wrote:

Dim dataset As DataSet
Does it work if you do
Dim dataset As DataSet = new DataSet()
here before passing dataset to the Fill method?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 7 '06 #4
Hi Martin,

Soooo Close. That worked, now it's gone to the statement:

trans.Transform (dataDocument, other arguments)

I've tried:

trans.Transform (dataDocument, Server.MapPath( "chapter8.xsl") )

etc., etc.

It doesn't like the dataDocument. It gives about 18 separate method
calls, and this first parameter needs to be one of :

XPathNavigator
IXPathNavigable

Any ideas?

Thanks...

Aug 7 '06 #5


sa***********@y ahoo.ca wrote:

That worked, now it's gone to the statement:

trans.Transform (dataDocument, other arguments)

I've tried:

trans.Transform (dataDocument, Server.MapPath( "chapter8.xsl") )

etc., etc.

It doesn't like the dataDocument. It gives about 18 separate method
calls, and this first parameter needs to be one of :

XPathNavigator
IXPathNavigable
I had hoped you had used the Transform method before and know what other
parameters you need. As for dataDocument, it is an XmlDataDocument which
implements IXPathNavigable so any Transform overload that takes an
IXPathNavigable as the first arguments will consume the dataDocument as
the first argument.
Assuming you have (with comments removed)

'trans = New XslTransform
'trans.Load(Ser ver.MapPath("ch apter8.xsl"))

as in your first post you have to decide what kind of transformation
result you want, if that is ASP.NET and you want to send the result of
the transformation to the user agent as the HTTP response then doing e.g.

trans.Transform (dataDocument, Nothing, Response.Output Stream, Nothing)

is one possible way (With ASP you should also send Response.Conten tType
as needed if your stylesheet does not create HTML). And of course your
stylesheet chapter8.xsl that you load with XslTransform is crucial to
get any meaningful results, the DataSet/XmlDataDocument convert the
relational query result to XML but obviously if you want to write a
stylesheet to process that XML then you need to know how the XML looks.

So for a test not doing any transform but rather simply
Response.Conten tType = "applicatio n/xml"
dataDocument.Sa ve(Response.Out putStream)
to simply look in IE at the XML returned could help to enable you to
write a stylesheet.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2008
by: 4Ankit | last post by:
hello all, i am having some difficulty retrieving information from my form. I want to add content in my table but the content i want to add to the table is what the user inputs in my form. Basically i want to know how i retrieve information from the form so i can disaply it in the table. i have put the code i am using below, the feature i cannot get to work is the change content function. I want the email address entered by the...
0
3391
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
34
2573
by: vpriya6 | last post by:
Hi guys, I am new to Ajax, xml and javascript. I want to know how can I retrieve data from xml and display in the html page? please help me out. suppose my xml file is customer.xml the code is below: <CUSTOMER>
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.