Solved

Help with saving data to Datatable and displaying data in listbox

Posted on 2012-12-26
6
393 Views
Last Modified: 2012-12-27
Hi,

I am using the code below to include data in a Datatable to display in a listbox, problem is
eventhough the dta is found in two records (part B), my DataTable contains 5 rows instead
of two rows, apparently I am getting a row for each natch but I need a row for each record. Based on data in Part B, my Datatable should contain:

Row1 102,104,105
Row2 102,104

Instead I am getting
Row1 102,
Row2 104
Row3 105
Row4 102
Row5 104


The code below gives me the following error:

"Complex DataBinding accepts as a data source either an IList or an IListSource."

For example if C1Ctry.SelectedValue.ToString = 1
For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA[Ctry_ID='" & C1Ctry.SelectedValue.ToString & "']")
            Link_ID = xnLink.SelectSingleNode("LinkAID").InnerText

‘Retreive data

  For Each xnNSN As Xml.XmlNode In xdNSN.SelectNodes("/Root/NSNTable[contains('," & NSNID & ",', concat(',', NSN_ID, ','))]")
                NSNID2 = xnNSN.SelectSingleNode("NSN_ID").InnerText
                Name = xnNSN.SelectSingleNode("NSN").InnerText
                dtNSN.Rows.Add({NSNID2, Name})
                utubeNSN = "," & Name
            Next

          ‘Display data
C1NSN.Items.Clear()
C1NSN.DataSource = dtNSN.Rows(0) ' Error
 C1NSN.DisplayMember = "NSN"
 C1NSN.ValueMember = "NSN_ID"
Next


Part B
<LinkA>
    <LinkAID>1</LinkAID>
    <crty_ID>1</ctry_ID>
       <NSN_ID>1,2,5</NSN_ID>
      </LinkA>
<LinkA>
    <LinkAID>2</LinkAID>
    <ctry_ID>1</ctry_ID>
     <NSN_ID>1,2</NSN_ID>
     </LinkA>


How do I modify the code to obtain the data in the right number of rows and How do I move to the next row to display the right record?

Thanks,

Victor
0
Comment
Question by:vcharles
  • 5
6 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
Comment Utility
Some notes form code above:
- The node "NSN" does not exists in your xml. Thus the binding will fail.
- The error occurs because you are trying to bind to the row not the data table. (I assume dtNSN)
    C1NSN.DataSource = dtNSN

Questions:
1. What are the correct C1NSN.DisplayMember  and  C1NSN.ValueMember for the C1NSN?
2. Do you really need a datatable? if you just need to read xml we can use linq to to xml as shown below:
http://stackoverflow.com/questions/2805477/parse-xml-and-populate-in-list-box
(Translation available on request)
0
 

Author Comment

by:vcharles
Comment Utility
Hi,
C1NSN.DisplayMember  is = NSN and C1NSN.ValueMember NSN_ID. I would like to use a datatable to store multiple rows (one row per record) to be able to use Next and Prvious buttons to move through the rows. Probloem is when I use C1NSN.DataSource = dtNSN I get
all rows, but I only one to display the first row in the listbox, the other problem is, the code is returning 5 rows, when it should only return two rows as explained in my initial post.

How do I only see one row for each record? as shown in part B I only have two records, but the  datatable contains 5 rows.

Thanks,

V.
0
 

Author Comment

by:vcharles
Comment Utility
Hi,

Below is the code that worked with the web version. How do I replace session in the Windows version?

 Public Function SearchCtry()

  Dim xdNSN As New Xml.XmlDocument
  xdNSN.Load(Server.MapPath("~/App_DataA/NSN.xml"))
  Dim fuze_Nsn_id As String
  Dim NSNID As String
  Dim NSNID2 As String
 
  dtMaster.Columns.Add("NSN_ID", GetType(String))
  dtMaster.Columns.Add("NSN", GetType(String))


  dtKeyMaster = New DataTable
  dtKeyMaster.Columns.Add("rowId", GetType(String))
  dtKeyMaster.Columns.Add("keyLinkAid", GetType(String))
  dtKeyMaster.Columns.Add("keyNsn", GetType(String))

  Dim rowIndex As Integer = 1

    For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA[Ctry_ID='" & C1ctry.SelectedItem.Value & "']")

NSNID = xnLink.SelectSingleNode("NSN_ID").InnerText
fuze_Nsn_id = NSNID
LinkAID = Link_ID

            'For Listbox
            For Each xnNSN As Xml.XmlNode In xdNSN.SelectNodes("/Root/NSNTable[contains('," & NSNID & ",', concat(',', NSN_ID, ','))]") 'NSN.xml
                NSNID2 = xnNSN.SelectSingleNode("NSN_ID").InnerText
                If dtMaster.Select("NSN_ID in (" & NSNID2 & ")").Length = 0 Then
                    Name = xnNSN.SelectSingleNode("NSN").InnerText
                    dtMaster.Rows.Add({fuze_Nsn_id, LinkAID, NSNID2, Name})
                End If
            Next

   dtMaster.AcceptChanges()
   dtKeyMaster.Rows.Add({rowIndex, LinkAID, fuze_Nsn_id, NSSIda})
   rowIndex += 1

 Next


  Session("dtMaster") = dtMaster
  Session("dtKeyMaster") = dtKeyMaster
  ViewDataB(dtKeyMaster.Rows(0)(1), dtKeyMaster.Rows(0)(2))

End Function


Private Sub ViewDataB(ByVal keyLinkAid As String, ByVal keyNsn As String)

  Dim _table As DataView = dtMaster.DefaultView
  _table.RowFilter = "NSN_ID in (" & keyNsn & ")"
  C1NSN.DataSource = _table
 C1NSN.DataTextField = "NSN"
 C1NSN.DataValueField = "NSN_ID"
C1NSN.DataBind()
End Sub
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:vcharles
Comment Utility
Below is the code i used in the web  version to move to the next row in the table, how do i replace Session in the Windows version?

        dtMaster = Session("dtMaster")
        dtKeyMaster = Session("dtKeyMaster")
        Dim rowId As Integer = Session("currentIndex") + 1
        ViewDataB(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))

I am also using the code below for datafield to move to the next row for datafields not using dtmaster but MyRowIndex can not replace Session.

 MyRowIndex = If(MyRowIndex < dtDonor.Rows.Count - 1, MyRowIndex + 1, MyRowIndex)
        C1CountryOrigin.Text = dtDonor.Rows(MyRowIndex).Item("Country")
0
 

Author Comment

by:vcharles
Comment Utility
Hi,

I was able to move to the next row using the following code:

  ViewDataB(dtKeyMaster.Rows(MyRowIndex)(1), dtKeyMaster.Rows(MyRowIndex)(2))

Any ideas on how to move to first and last row?


Thanks,

V.
0
 

Author Comment

by:vcharles
Comment Utility
Issue is resolved. Thanks.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

14 Experts available now in Live!

Get 1:1 Help Now