Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:45:28+00:00 2026-05-26T11:45:28+00:00

I am trying to bind XML data into a gridview, after clicking the ‘ButtonSaveToDataBase’

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:45:29+00:00Added an answer on May 26, 2026 at 11:45 am

    You are creating an extra element in your array.

    Dim cv(GridView1.Rows.Count) As CampaignVoucher 'create an array of gv row size
    

    Should be

    Dim cv(GridView1.Rows.Count - 1) As CampaignVoucher 'create an array of gv row size
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to bind to some XML data from my WPF application. I've set
I am trying to bind the data from linq xml in isolated storage. The
Hi I am trying to bind a SQL XML field to my gridview. The
I am trying to bind an event to a method of a particular instance
I'm trying to bind a list of integers into an SQLTemplate IN clause like
I'm trying to load an .xml datafile into oracle for the last couple of
I'm trying to bind a XML to POJOs using Simpleframework but it doesn't work
I am trying to map a java model into some XML. To do this
I'm trying to bind data from my SQLiteDatabase to a ListView . I'm currently
I am trying to generate Java classes to bind xml to objects using castor.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.