Hi I am trying to bind a SQL XML field to my gridview.
The gridview should be like this:
edit-update | **Voucher Code** | **Quantity** | delete
edit vouc001 5 del
edit vouc002 57 del
The XML file in my SQL field looks like this:
<ArrayOfCampaignVoucher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CampaignVoucher>
<VouCode>Vouc002</VouCode>
<Qty>57</Qty>
</CampaignVoucher>
<CampaignVoucher xsi:nil="true" />
</ArrayOfCampaignVoucher>
I have came across examples of binding gridview from local XML file like this:
DataSet dataSet = new DataSet();
dataSet.ReadXml("~/App_Data/input.xml");
this.GridView1.DataMember = "fruit";
this.GridView1.DataSource = dataSet;
this.GridView1.DataBind();
How can I do that when my XML is inside a SQL server field? Thanks.
You need to connect to your SQL Server database, read rows containing your XML and extract it. Tutorials on how to connect to SQL Server and read data can be found for example here (you can easily find more of these googling or searching here on SO).
Once you have your XML as string read from database, you can easily set up stream to read from it and pass that to DataSet.ReadXml method – for example using StringReader class.
To give you idea of how program flow might look: