I am trying to bind XML data into a gridview, after clicking the ‘ButtonSaveToDataBase’ button, the method will begin to read data from my gridview and load it into an array of my serializable class type and after that serialize it and finally store it into a XML type field in my SQL. The problem is When I call BindData() at the end of the serialization, it reads the XML from my database and successfully binded it but my gridview displays an empty row below my datas like this:
Edit-Update VouCode Quantity Delete
Edit 1 3 Delete
Edit Delete
Could someone please advice where went wrong, is it the serialization part? Thanks.
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
'This method will add a new row of data into my gridview but not save it yet.
Dim newTable As New DataTable("NewTable")
newTable.Columns.Add("VouCode")
newTable.Columns.Add("QTY")
Dim dr2 As DataRow = newTable.NewRow
dr2("VouCode") = DropDownList1.SelectedIndex
dr2("QTY") = TextBox1.Text
newTable.Rows.Add(dr2)
ds.Tables.Add(newTable)
Me.GridView1.DataSource = ds.Tables(0)
Me.GridView1.DataBind()
ViewState("VoucherRewardsSet") = ds
con.Close()
End Sub
Protected Sub ButtonSaveToDataBase_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSaveToDatabase.Click
Dim dbCommand As DbCommand = Nothing
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Test").ConnectionString)
Dim cmd As New SqlCommand("Campaign_InsertNew", con)
cmd.CommandType = CommandType.StoredProcedure
Dim cv(GridView1.Rows.Count) As CampaignVoucher 'create an array of gv row size
Dim vc As String = String.Empty
Dim qt As Integer
For i As Integer = 0 To GridView1.Rows.Count - 1 'loop through gv and load data into array
vc = GridView1.Rows(i).Cells(1).Text
qt = GridView1.Rows(i).Cells(2).Text
cv(i) = New CampaignVoucher(vc, qt)
Next
' -----------------Serialization ------------------
Dim serializer As New XmlSerializer(cv.[GetType]())
Dim memoryStream As New MemoryStream()
Dim writer As New XmlTextWriter(memoryStream, Encoding.UTF8)
serializer.Serialize(writer, cv)
'get the stream from the writer
memoryStream = TryCast(writer.BaseStream, MemoryStream)
'apply encoding to the stream
Dim enc As New UTF8Encoding
Dim xml As String = enc.GetString(memoryStream.ToArray()).Trim()
' -------------------------------------------
cmd.Parameters.Add("@voucherXML", SqlDbType.Text).Value = xml
cmd.Connection = con
con.Open()
cmd.ExecuteScalar()
con.Close()
GridView1.EditIndex = -1
BindData()
TextBox1.Text = ""
End Sub
Private Sub BindData()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Test").ConnectionString)
Dim cmdSelect As New SqlCommand("Select VoucherXML from RewardVouchers", con)
Dim ds As New DataSet("VoucherRewardsSet")
con.Open()
Using reader = cmdSelect.ExecuteReader()
cmdSelect.Connection = con
reader.Read()
If (reader.HasRows) Then
Dim xml As String = reader.GetString(0)
'Dim ds As New DataSet()
ds.ReadXml(New StringReader(xml))
Dim dtableForGVBinding As DataTable = ds.Tables(0)
Me.GridView1.DataMember = "CampaignVoucher"
Me.GridView1.DataSource = dtableForGVBinding
Me.GridView1.DataBind()
End If
End Using
con.Close()
End Sub
XML copied from my SQL field:
<ArrayOfCampaignVoucher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CampaignVoucher VouCode="1" Qty="34" />
<CampaignVoucher xsi:nil="true" />
</ArrayOfCampaignVoucher>
You are creating an extra element in your array.
Should be