D
David Hearn
I am using the code below to create a new dataset, add a new table to it
called "Inventory" and then add a new row to the table with some data in it.
I am using this to create a temporary dataset so that I can bind a datagrid
to it. All goes well until I try to update it. When it gets to the last line
and tries to update, I get the error Invalid object name 'Inventory'. Can
someone please tell me what I am doing wrong?
Thanks in advance!
Dim MyConnection As SqlConnection
Dim MySQLDataAdapter As SqlDataAdapter
Dim bAdd As Boolean
Dim aryParts As Array
Dim aryQuantities As Array
Dim i As Integer
Dim PrimaryLocationName, SecondaryLocationName As String
Dim PrimaryLocationCode, SecondaryLocationCode As String
aryParts = Parts.Split(",")
aryQuantities = Quantities.Split(",")
PrimaryLocationCode = Location1
SecondaryLocationCode = Location2
PrimaryLocationName = GetLocation(Location1).Trim
SecondaryLocationName = GetLocation(Location2).Trim
MyConnection = CartFunctions.GetConnection
MyConnection.Open()
'Create a new dataset
Dim MyDataSet As New DataSet
'Add a new datatable to the dataset to store our records in
Dim dtInventory As New DataTable("Inventory")
Dim dc As New DataColumn
dc = New DataColumn("PartNumber", System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn("OrderQuantity", System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn(PrimaryLocationName,
System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn(SecondaryLocationName,
System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn("AllLocations", System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
MyDataSet.Tables.Add(dtInventory)
Dim MyDataRow As DataRow
MySQLDataAdapter = New SqlDataAdapter("SELECT * FROM Inventory",
MyConnection)
Dim MyDataRowsCommandBuilder As SqlCommandBuilder = New
SqlCommandBuilder(MySQLDataAdapter)
MySQLDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
'Now loop through and get the data
For i = 0 To UBound(aryParts)
'Get inventory for Primary Location
Dim strSQL As String = "SELECT * FROM location_inventory WHERE location_code
= '" & PrimaryLocationCode & "' AND part_num = '" & aryParts(i) & "'"
Dim cmdReader As SqlCommand
Dim dtrList As SqlDataReader
cmdReader = New SqlCommand(strSQL, MyConnection)
dtrList = cmdReader.ExecuteReader
Do While dtrList.Read
MyDataRow = MyDataSet.Tables("Inventory").NewRow
MyDataRow("PartNumber") = aryParts(i)
MyDataRow("OrderQuantity") = aryQuantities(i)
MyDataRow(PrimaryLocationName) = dtrList("Quantity")
Loop
dtrList.Close()
MyDataSet.Tables("Inventory").Rows.Add(MyDataRow)
MySQLDataAdapter.Update(MyDataSet, "Inventory")
Next
called "Inventory" and then add a new row to the table with some data in it.
I am using this to create a temporary dataset so that I can bind a datagrid
to it. All goes well until I try to update it. When it gets to the last line
and tries to update, I get the error Invalid object name 'Inventory'. Can
someone please tell me what I am doing wrong?
Thanks in advance!
Dim MyConnection As SqlConnection
Dim MySQLDataAdapter As SqlDataAdapter
Dim bAdd As Boolean
Dim aryParts As Array
Dim aryQuantities As Array
Dim i As Integer
Dim PrimaryLocationName, SecondaryLocationName As String
Dim PrimaryLocationCode, SecondaryLocationCode As String
aryParts = Parts.Split(",")
aryQuantities = Quantities.Split(",")
PrimaryLocationCode = Location1
SecondaryLocationCode = Location2
PrimaryLocationName = GetLocation(Location1).Trim
SecondaryLocationName = GetLocation(Location2).Trim
MyConnection = CartFunctions.GetConnection
MyConnection.Open()
'Create a new dataset
Dim MyDataSet As New DataSet
'Add a new datatable to the dataset to store our records in
Dim dtInventory As New DataTable("Inventory")
Dim dc As New DataColumn
dc = New DataColumn("PartNumber", System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn("OrderQuantity", System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn(PrimaryLocationName,
System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn(SecondaryLocationName,
System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
dc = New DataColumn("AllLocations", System.Type.GetType("System.String"))
dtInventory.Columns.Add(dc)
MyDataSet.Tables.Add(dtInventory)
Dim MyDataRow As DataRow
MySQLDataAdapter = New SqlDataAdapter("SELECT * FROM Inventory",
MyConnection)
Dim MyDataRowsCommandBuilder As SqlCommandBuilder = New
SqlCommandBuilder(MySQLDataAdapter)
MySQLDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
'Now loop through and get the data
For i = 0 To UBound(aryParts)
'Get inventory for Primary Location
Dim strSQL As String = "SELECT * FROM location_inventory WHERE location_code
= '" & PrimaryLocationCode & "' AND part_num = '" & aryParts(i) & "'"
Dim cmdReader As SqlCommand
Dim dtrList As SqlDataReader
cmdReader = New SqlCommand(strSQL, MyConnection)
dtrList = cmdReader.ExecuteReader
Do While dtrList.Read
MyDataRow = MyDataSet.Tables("Inventory").NewRow
MyDataRow("PartNumber") = aryParts(i)
MyDataRow("OrderQuantity") = aryQuantities(i)
MyDataRow(PrimaryLocationName) = dtrList("Quantity")
Loop
dtrList.Close()
MyDataSet.Tables("Inventory").Rows.Add(MyDataRow)
MySQLDataAdapter.Update(MyDataSet, "Inventory")
Next