[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Update Dataset row by name

I have a simple dataset like this:
        Dim stochtable As New Data.DataTable
        stochtable.Clear()
        stochtable.Columns.Add("Symbol")
        stochtable.Columns.Add("Price")
        Me.StochDataSet.Tables.Add(stochtable)

How can I update a row in the datatable by using the symbol name instead of the row number?
0
johnywhite
Asked:
johnywhite
  • 3
  • 2
1 Solution
 
RonaldBiemansCommented:
?????, Do rows have names, I think not. Or maybe I don't understand the question.
0
 
RonaldBiemansCommented:
aha, I think I do understand :-)

there are several options.

1. if the column Sysmbol is the primary key you can just do

        Dim dr As DataRow = stochtable.Rows.Find("yoursymbol")
        dr.Item("Price") = 10

2. if not you can use a dataview like

        Dim dv As DataView = stochtable.DefaultView
        dv.Sort = "Symbol"
        Dim dr As DataRow = stochtable.Rows(dv.Find("yoursymbol"))
        dr.Item("Price") = 10
0
 
johnywhiteAuthor Commented:
When I try that I get an error "Table does not have a primary key".  I added a primary key to the database and I still get the error.  I am trying to search it by symbol.
0
 
RonaldBiemansCommented:
If you made the column symbol the primary key in the database you must also add it to the datatable in your program
by letting the dataadapter add the key

like

        da.MissingSchemaAction = MissingSchemaAction.AddWithKey

or by setting it explicitly in code

Stochtable.PrimaryKey = New DataColumn() {stochtable("Symbol")}


0
 
johnywhiteAuthor Commented:
Thanks that worked
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now