Link to home
Start Free TrialLog in
Avatar of Tony Gardner
Tony GardnerFlag for United States of America

asked on

Need Assistance with Adding a Record to a Bound Data Table using VB.NET

Hello Experts.

Yesterday, I took on the challenge of assembling a record for my Match table, and writing it to the table. I thought perhaps it best to use a String array to store the data, then add it as a single DataTable row. Here's the code that I have right now:
Private Sub NineBallRack_Click(sender As Object, e As EventArgs) Handles NineBallRack.Click
    ' Clicking the Nine Ball Rack icon officially starts the match!
    Dim MsgOpts As MsgBoxStyle = vbOKCancel + vbQuestion + vbDefaultButton2
    Dim MsgText As String = "Click OK to Start the Match, or Cancel to return."
    If MsgBox(MsgText, MsgOpts, "SNAP - Start Match") = vbOK Then
        Call BuildMatchRecord()
        If ReturnArray IsNot Nothing Then
            Dim dtMatch As DataTable = Me.SNAPDataSet.Match
            dtMatch.NewRow()
            dtMatch.Rows.Add(ReturnArray)
            ReturnArray = Nothing
            'Call BindingNavigatorSave_Click(Me.DataMatchSave, Nothing)
        End If
    End If
End Sub

Open in new window


Note that the call to BindingNavigatorSave was commented out since it was throwing an error, so I thought it best just to focus on getting this first part working, then tackle that part later.

Here is what Sub BuildMatchRecord looks like, with comments describing the various fields:
Private Sub BuildMatchRecord()
    ReturnArray = Nothing
    ' Match Table Uses the Following Schema:
    '     0: Match_Key (yyMMddHHmm)
    Dim MatchKey As String = DateTime.Now.ToString("yyMMddHHmm")
    '     1: Schedule_Key
    Dim CurrDate As DateTime = DateTime.Now.ToString("d")
    Dim SchedKey As Integer = GetCurrSchedKey(CurrDate)
    If SchedKey = 0 Then
        Dim MsgOpts As MsgBoxStyle = vbOKOnly + vbExclamation + vbDefaultButton2
        Dim MsgText As String = "No events are scheduled for today. Go to the Schedule tab to setup an event."
        If MsgBox(MsgText, MsgOpts, "SNAP - Scheduling") = vbOK Then
            Me.tabData.SelectTab(3)
        End If
    End If
    '     2: Match_No (1-5)
    Dim Match_No As Integer = MN
    '   3/4: Home/Visitor Player ID
    Dim Play_1_ID As Integer = CInt(Me.pdPlay1No.Text)
    Dim Play_2_ID As Integer = CInt(Me.pdPlay2No.Text)
    '   5/6: Home/Visitor Team ID
    Dim Team_1_ID As Integer = CInt(Me.pdTeam1No.Text)
    Dim Team_2_ID As Integer = CInt(Me.pdTeam2No.Text)
    '   7/8: Home/Visitor Player SL
    Dim Play_1_SL As Integer = CInt(Me.pdPlay1SL.Text)
    Dim Play_2_SL As Integer = CInt(Me.pdPlay2SL.Text)
    '  9/10: Player 1/2 DS (Defensive Shots are Stored at the Match Level)
    Dim Play_1_DS As Integer = CInt(Me.pdPlay1DS.Text)
    Dim Play_2_DS As Integer = CInt(Me.pdPlay2DS.Text)
    ' 11/12: Player 1/2 MP (Match Points)
    Dim Play_1_MP As Integer = CInt(Me.pdPlay1MP.Text)
    Dim Play_2_MP As Integer = CInt(Me.pdPlay2MP.Text)
    '    13: Total Innings
    Dim TotInning As Integer = CInt(Me.TotalInnings.Text)
    ' 14/15: Start/End Time (Actual)
    Dim Beg_Time As DateTime = DateTime.Now
    Dim MatchRec = New String() {MatchKey, SchedKey, Match_No, Play_1_ID, Play_2_ID, Team_1_ID, Team_2_ID, _
            Play_1_SL, Play_2_SL, Play_1_DS, Play_2_DS, Play_1_MP, Play_2_MP, TotInning, Beg_Time}
    ReturnArray = MatchRec
End Sub

Open in new window


I declared ReturnArray as a public shared property at the top of the class so I could use it in both of the above Subs:
Public Shared Property ReturnArray As Array

Open in new window


I was able to capture the contents of ReturnArray when it is doing the Rows.Add, and it looks good to me:
User generated image
Since I'm not writing it to the MDB file just yet, I don't expect it to be permanently stored, but I do have a Tab Page setup with a DataGridView for the Match table so I can see it's contents during run time, and this is what gets written:
User generated image
Would anyone be able to elaborate why all I get is "System.String[]" in the ID field instead of populating all the fields?

Thanks.
SOLUTION
Avatar of Member_2_25505
Member_2_25505

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tony Gardner

ASKER

Although both contributors had helpful suggestions, I feel that the one I chose was the best way to approach the problem.