我正在尝试将XML数据绑定到gridview中,在单击“ButtonSaveToDataBase”按钮后,该方法将开始从我的gridview中读取数据并将其加载到我的可序列化类类型的数组中,然后将其序列化,最后将它存储在我的SQL中的XML类型字段中。问题是当我在序列化结束时调用BindData()时,它从我的数据库中读取XML并成功绑定它,但我的gridview在我的数据下面显示一个空行,如下所示:
Edit-Update VouCode Quantity Delete
Edit 1 3 Delete
Edit Delete
有人可以建议哪里出错了,是序列化部分吗?感谢。
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
从我的SQL字段复制的XML:
<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>
答案 0 :(得分:2)
您正在数组中创建一个额外的元素。
Dim cv(GridView1.Rows.Count) As CampaignVoucher 'create an array of gv row size
应该是
Dim cv(GridView1.Rows.Count - 1) As CampaignVoucher 'create an array of gv row size