Solved

XSD + ADO.NET + XML -> Binding to Grid

Posted on 2009-05-07
8
422 Views
Last Modified: 2013-11-26
Hi,

I want to bind an XML file to a grid and be able to Add/Update/Delete.

All is working fine with the following code

--------------------------------------------------------------------------

Private dsLicence As New DataSet()

Private Sub frmMain_Load(..........)
dsLicence.ReadXmlSchema(My.Settings.XSD_PATH & "\LicencesShema.xsd")
dsLicence.ReadXml(My.Settings.XML_PATH & "\Licences.xml")

gridLicences.SetDataBinding(dsLicence, Nothing)
End Sub


Private Sub gridLicences_AfterCellUpdate(.....)
dsLicence.WriteXml(My.Settings.XML_PATH & "\Licences.xml")
End Sub

--------------------------------------------------------------------------

My problem is if the XML file is empty or does not exist i got an error. I was thinking that the use of the XSD file, even if the xml file was not existing, that i've got the base for the binding.

Is there any solution, cause for now i need to create the XML file and put some garbage data in it (Sample Data) and to block the user if he try to delete the last line of to grid.

Thanks for your help.

Here is my XML & XSD File
<?xml version="1.0"?>

<xs:schema xmlnss="http://www.w3.org/2001/XMLSchema"

xmlns:msdata="urn:schemas-microsoft-comml-msdata">
 

<xs:element name="Client">

<xs:complexType>

<xs:sequence>

<xs:element name="ID" type="xs:int" msdata:AutoIncrement="true" msdata:ReadOnly="true"/>

<xs:element name="COMPANYNAME" type="xs:string"/>

<xs:element name="NBUSER" type="xs:int" nillable="true"/> 

<xs:element name="NBUSERNOMME" type="xs:int" nillable="true"/>

<xs:element name="DATABASE" type="xs:string"/>

<xs:element name="EXPDATE" type="xs:dateTime" nillable="true"/>

</xs:sequence>

</xs:complexType>

</xs:element>
 

</xs:schema>
 
 
 

<?xml version="1.0" standalone="yes"?>

<NewDataSet>

<Client>

<ID>1</ID>

<COMPANYNAME>Sample Company</COMPANYNAME>

<NBUSER>1</NBUSER>

<NBUSERNOMME>1</NBUSERNOMME>

<DATABASE>Sample Database</DATABASE>

<EXPDATE>2009-04-27T00:00:00-04:00</EXPDATE>

</Client>

</NewDataSet>

Open in new window

0
Comment
Question by:d1rtyw0rm
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 24333744
What about simply checking the contents of dsLicenses before you call setDataBinding?

If dsLicence.Tables.Count <> 0 Then
   gridLicences.SetDataBinding(dsLicence, Nothing)
End If
0
 
LVL 2

Author Comment

by:d1rtyw0rm
ID: 24394785
Because it crash before ... on this line

dsLicence.ReadXml(My.Settings.XML_PATH & "\Licences.xml")
0
 
LVL 27

Expert Comment

by:Dabas
ID: 24400530
What if you wrap that line inside a try catch?


Private dsLicence As New DataSet()
 

Private Sub frmMain_Load(..........)

Try

    dsLicence.ReadXmlSchema(My.Settings.XSD_PATH & "\LicencesShema.xsd")

    dsLicence.ReadXml(My.Settings.XML_PATH & "\Licences.xml")

    gridLicences.SetDataBinding(dsLicence, Nothing)

Catch (ex as Exception)

    MessageBox.Show("Something seems to be wrong with the xml file")

End Try
 

End Sub

Open in new window

0
 
LVL 2

Author Comment

by:d1rtyw0rm
ID: 24405105
First of all thanks a lot for your help but ...

That's exactly what i do for now, but what i want is to be able to RUN IT without an XML file, and the grid column to be based on my xsd file.

The problem that i have is that i'm in the obligation to create an XML file with at least one record, and i can't delete the last item of a grid because the XML will be empty and unavailible for the next use.

Hope that you understand what is my problem.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 27

Expert Comment

by:Dabas
ID: 24405133
Sorry I do not.
0
 
LVL 2

Author Comment

by:d1rtyw0rm
ID: 24418535
All i want, is to be able to bind a grid to an empty XML file.

Based on an XSD file to setup the column title.
0
 
LVL 27

Accepted Solution

by:
Dabas earned 250 total points
ID: 24418664
What about this idea:

You create a non-empty xml, that confroms to your xsd file, just that it has empty nodes.

Something like in the snippet

You use the code I posted before, only that if the catch statement is reached you bind to this file?

Dabas
<?xml version="1.0" standalone="yes"?>

<NewDataSet>

<Client>

<COMPANYNAME>COMPANYNAME>

<DATABASE></DATABASE>

</Client>

</NewDataSet>

Open in new window

0
 
LVL 2

Author Closing Comment

by:d1rtyw0rm
ID: 31579149
Thanks a lot for your patience, it's really appreciated !
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Problem description :  Some external hard disks / USB flash drives do not show actual space as mentioned in the factory settings. This is a common problem when you use an 8 GB USB drive to make it bootable to install a firmware/ driver on a serv…
I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now