Results 1 to 19 of 19

Thread: [RESOLVED] Getting information from DataSet to populate defaults in controls.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] Getting information from DataSet to populate defaults in controls.

    Hi all,

    I have a form that has many controls. My next task is to populate the phone number (MaskedTextBox) and phone number type (ComboBox) when the cursor has lost focus from a ComboBox that has auto complete functionality. I have tried to do it and when the cursor leaves focus, the other controls are not populated. Nothing happens. I do not get any errors either. Screen shot attached for the form and I am also using a stored procedure to do this. There are two tables in question here. A client table that the name and phone number and then the second table that has the phone number type (cell, work, home, etc). There is an FK relationship between the two.

    This is my stored procedure. It is called sp_GetClientInfoByName.
    vb Code:
    1. CREATE PROCEDURE sp_GetClientInfoByName
    2. @ClientName VARCHAR(50)
    3. AS
    4. BEGIN
    5. SELECT ClientInformation.ClientName,
    6.        ClientInformation.ClientPhoneNumber,
    7.        ClientInformation.PhoneNumberTypeID_PKFK,
    8.        PhoneNumber_Type.PhoneNumberTypeID_PK,
    9.        PhoneNumber_Type.PhoneNumberTypeName
    10. FROM ClientInformation
    11. INNER JOIN PhoneNumber_Type
    12. ON ClientInformation.PhoneNumberTypeID_PKFK = PhoneNumber_Type.PhoneNumberTypeID_PK
    13. WHERE ClientInformation.ClientName = @ClientName
    14. END

    The code I am trying to use is as follows (partially completed).
    vb Code:
    1. Private Sub cboClientName_LostFocus(ByVal sender As Object, _
    2.                                         ByVal e As System.EventArgs) Handles cboClientName.LostFocus
    3.  
    4.         'Check the backend database for existing clients based on name.
    5.         'If multiple phone numbers exist for the client, show a form to be able to select a phone number.
    6.         'If single phone number exists, just populate the form.
    7.         Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
    8.             Dim TableName As String = "ClientInformation"
    9.             Dim ColumnName As String = "ClientName"
    10.  
    11.             Try
    12.                 'Create a new SQLCommand.
    13.                 Dim GetClientInfo As SqlCommand = New SqlCommand
    14.  
    15.                 'Open the database connection.
    16.                 SetDatabaseConnection.Open()
    17.  
    18.                 'Set some values to the SQLCommand object.
    19.                 With GetClientInfo
    20.                     .Connection = SetDatabaseConnection
    21.                     .CommandType = CommandType.StoredProcedure
    22.                     .CommandText = "sp_GetClientInfoByName"
    23.                     .Parameters.AddWithValue("@ClientName", cboClientName.Text.Trim)
    24.                 End With
    25.  
    26.                 'Create a new DataSet.
    27.                 Dim ClientInfoDS As New DataSet(GetClientInfo.CommandText)
    28.                 'Create a new DataAdapter.
    29.                 Dim ClientInfoDA As SqlDataAdapter = New SqlDataAdapter(GetClientInfo)
    30.  
    31.                 'Set the Current Culture of the DataSet.
    32.                 ClientInfoDS.Locale = CurrentCulture
    33.  
    34.                 'Clear the DataSet of any previously obtained database information.
    35.                 ClientInfoDS.Clear()
    36.  
    37.                 'Refill the DataAdapter with new information obtained from the database.
    38.                 ClientInfoDA.Fill(ClientInfoDS, TableName)
    39.  
    40.                 'First check to see if any records.  If no records, add a default item.
    41.                 Dim ClientInfoDT As DataTable = New DataTable
    42.  
    43.                 'Populate the DataTable.
    44.                 ClientInfoDT = ClientInfoDS.Tables(TableName)
    45.  
    46.                 'Set the culture of the DataTable.
    47.                 ClientInfoDT.Locale = CurrentCulture
    48.  
    49.                 If ClientInfoDT.Rows.Count > 0 Then 'Has records.
    50.                     'Now that there are records returned, see if there is only one or multiple.
    51.                     'If there is one record, just populate the form.
    52.                     'If there are multiple, show a form to choose the phone number and phone number type.
    53.                     Select Case ClientInfoDT.Rows.Count
    54.                         Case 1 'One record only.
    55.                             'Just populate the form.
    56.                             Dim TheClientInfoDR As DataRow = ClientInfoDS.Tables(TableName).Rows.Find(cboClientName.Text.Trim)
    57.                             mtbClientPhoneNumber.Text = TheClientInfoDR(1).ToString 'Phone Number.
    58.                             cboPhoneNumberType.Text = TheClientInfoDR(2).ToString 'Phone Number Type.
    59.                         Case Else 'More than one record returned.
    60.                             'Show a form to choose the phone number and phone number type.
    61.  
    62.                     End Select
    63.                 Else 'Has no records.
    64.                     'Focus Client Phone Number.
    65.                     mtbClientPhoneNumber.Focus()
    66.                 End If
    67.  
    68.                 'Close the database connection.
    69.                 SetDatabaseConnection.Close()
    70.             Catch ex As SqlException
    71.                 MessageBox.Show("There was an error retrieving data from table name " & TableName & " for the column " & ColumnName & "." & vbNewLine & vbNewLine & _
    72.                                 "The reason why you got this error in the first place is:" & vbNewLine & _
    73.                                 "No data items in the database " & TableName & " to pull." & vbNewLine & vbNewLine & _
    74.                                 "The exception error is:" & vbNewLine & _
    75.                                 ex.Message, _
    76.                                 "Database Information Retrieval Issues.", _
    77.                                 MessageBoxButtons.OK, _
    78.                                 MessageBoxIcon.Error, _
    79.                                 MessageBoxDefaultButton.Button1, _
    80.                                 MessageBoxOptions.DefaultDesktopOnly, _
    81.                                 False)
    82.             Catch ex As InvalidOperationException
    83.                 MessageBox.Show(ex.Message, _
    84.                                 "Invalid Operation Exception.", _
    85.                                 MessageBoxButtons.OK, _
    86.                                 MessageBoxIcon.Error, _
    87.                                 MessageBoxDefaultButton.Button1, _
    88.                                 MessageBoxOptions.DefaultDesktopOnly, _
    89.                                 False)
    90.             Catch ex As NullReferenceException
    91.                 MessageBox.Show(ex.Message, _
    92.                                 "Null Reference Exception.", _
    93.                                 MessageBoxButtons.OK, _
    94.                                 MessageBoxIcon.Error, _
    95.                                 MessageBoxDefaultButton.Button1, _
    96.                                 MessageBoxOptions.DefaultDesktopOnly, _
    97.                                 False)
    98.             End Try
    99.         End Using
    100.     End Sub

    What am I doing wrong?
    Attached Images Attached Images  

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I have been doing some research and I have found that because there are two tables involved here, I have to create a DataRelation between the two tables. I will try this and be back if I have any problems.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I have never worked with a data relation before, so this is my first time. I am getting a Null Reference Exception. The error is generated when I tried to add the DataRelation. This is my code. Please assist if you can, thanks.

    Null Reference Exception.
    Object reference not set to an instance of an object.
    Error happens on this line:
    vb Code:
    1. 'Setup a DataRelation between ClientInformation and PhoneNumber_Type tables.
    2.                 ClientInfoDS.Relations.Add("Phone Numbers", _
    3.                                            ClientInfoDS.Tables(TableName1).Columns(TableName1_PKFKColumn), _
    4.                                            ClientInfoDS.Tables(TableName2).Columns(TableName2_PKColumn))

    Code
    vb Code:
    1. Private Sub cboClientName_LostFocus(ByVal sender As Object, _
    2.                                         ByVal e As System.EventArgs) Handles cboClientName.LostFocus
    3.  
    4.         'Check the backend database for existing clients based on name.
    5.         'If multiple phone numbers exist for the client, show a form to be able to select a phone number.
    6.         'If single phone number exists, just populate the form.
    7.         Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
    8.             Dim TableName1 As String = "ClientInformation"
    9.             Dim TableName1_ColumnName As String = "ClientName"
    10.             Dim TableName1_PKFKColumn As String = "PhoneNumberTypeID_PKFK"
    11.             Dim TableName2 As String = "PhoneNumber_Type"
    12.             Dim TableName2_ColumnName As String = "PhoneNumberTypeName"
    13.             Dim TableName2_PKColumn As String = "PhoneNumberTypeID_PK"
    14.  
    15.             Try
    16.                 'Open the database connection.
    17.                 SetDatabaseConnection.Open()
    18.  
    19.                 'Create a new SQLCommand.
    20.                 Dim GetClientInfo As SqlCommand = New SqlCommand
    21.  
    22.                 'Set the attributes for the SQLCommand.
    23.                 With GetClientInfo
    24.                     .Connection = SetDatabaseConnection
    25.                     .CommandType = CommandType.StoredProcedure
    26.                     .CommandText = "sp_GetClientInfoByName"
    27.                     .Parameters.AddWithValue("@ClientName", cboClientName.Text.Trim)
    28.                 End With
    29.  
    30.                 'Create a new DataSet.
    31.                 Dim ClientInfoDS As DataSet = New DataSet(GetClientInfo.CommandText)
    32.  
    33.                 'Create a new DataAdapter.
    34.                 Dim ClientInfoDA As SqlDataAdapter = New SqlDataAdapter(GetClientInfo)
    35.  
    36.                 'Set the CurrentCulture of the DataSet.
    37.                 ClientInfoDS.Locale = CurrentCulture
    38.  
    39.                 'Clear the DataSet of any previously obtained information.
    40.                 ClientInfoDS.Clear()
    41.  
    42.                 'Refill the DataAdapter with new information obtained from the database.
    43.                 ClientInfoDA.Fill(ClientInfoDS, TableName1)
    44.  
    45.                 'Create two DataRows because there are two tables to work with.
    46.                 Dim ParentDR, ChildDR As DataRow
    47.  
    48.                 'Setup a DataRelation between ClientInformation and PhoneNumber_Type tables.
    49.                 ClientInfoDS.Relations.Add("Phone Numbers", _
    50.                                            ClientInfoDS.Tables(TableName1).Columns(TableName1_PKFKColumn), _
    51.                                            ClientInfoDS.Tables(TableName2).Columns(TableName2_PKColumn))
    52.  
    53.                 'Display the content from the tables.
    54.                 For Each ParentDR In ClientInfoDS.Tables(TableName1).Rows
    55.                     For Each ChildDR In ParentDR.GetChildRows(TableName2)
    56.                         'Check to see if anything is within the DataSet.
    57.                         If ClientInfoDS.Tables(TableName1).Rows.Count > 0 Then
    58.                             'Information has been returned.
    59.                             'Now if there is only one phone number returned, populate the other controls.
    60.                             'If there are multiple phone numbers returned, show a form for the end-user to
    61.                             'choose the phone number and phone number type they want.
    62.                             Select Case ClientInfoDS.Tables(TableName1).Rows.Count
    63.                                 Case 1 'One record returned.
    64.                                     'Populate the other controls.
    65.                                     mtbClientPhoneNumber.Text = ParentDR(TableName1_ColumnName).ToString
    66.                                     cboPhoneNumberType.Text = ChildDR(TableName2_ColumnName).ToString
    67.                                 Case Else 'Multiple records returned.
    68.                                     'Show a form for the end user to choose which phone number/phone number type to select.
    69.                                     'TODO
    70.                             End Select
    71.                         Else 'Nothing returned.
    72.                             'Focus the client phone number field.
    73.                             mtbClientPhoneNumber.Focus()
    74.                         End If
    75.                     Next ChildDR
    76.                 Next ParentDR
    77.  
    78.                 'Close the database connection.
    79.                 SetDatabaseConnection.Close()
    80.             Catch ex As SqlException
    81.                 MessageBox.Show("There was an error retrieving data from the databse." & vbNewLine & vbNewLine & _
    82.                                 "The reason why you got this error in the first place is:" & vbNewLine & _
    83.                                 "No data items in the database to pull." & vbNewLine & vbNewLine & _
    84.                                 "The exception error is:" & vbNewLine & _
    85.                                 ex.Message, _
    86.                                 "Database Information Retrieval Issues.", _
    87.                                 MessageBoxButtons.OK, _
    88.                                 MessageBoxIcon.Error, _
    89.                                 MessageBoxDefaultButton.Button1, _
    90.                                 MessageBoxOptions.DefaultDesktopOnly, _
    91.                                 False)
    92.             Catch ex As InvalidOperationException
    93.                 MessageBox.Show(ex.Message, _
    94.                                 "Invalid Operation Exception.", _
    95.                                 MessageBoxButtons.OK, _
    96.                                 MessageBoxIcon.Error, _
    97.                                 MessageBoxDefaultButton.Button1, _
    98.                                 MessageBoxOptions.DefaultDesktopOnly, _
    99.                                 False)
    100.             Catch ex As NullReferenceException
    101.                 MessageBox.Show(ex.Message, _
    102.                                 "Null Reference Exception.", _
    103.                                 MessageBoxButtons.OK, _
    104.                                 MessageBoxIcon.Error, _
    105.                                 MessageBoxDefaultButton.Button1, _
    106.                                 MessageBoxOptions.DefaultDesktopOnly, _
    107.                                 False)
    108.             End Try
    109.         End Using
    110.     End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Getting information from DataSet to populate defaults in controls.

    You've got your parent and child around the wrong way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    Quote Originally Posted by jmcilhinney View Post
    You've got your parent and child around the wrong way.
    I tried that and I got the same error as before.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I just had a closer look at your code. A DataRelation exists between two DataTables. You're only populating one DataTable though. That would explain why you're getting a NullReferenceException. You're trying to get two DataTables from the DataSet but at least one of them doesn't exist. You do this:
    Code:
    ClientInfoDA.Fill(ClientInfoDS, TableName1)
    so that one's in there but there's no other call to Fill.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    I am also seeing that I am only filling the DataAdapter with the first table. Don't I have to do it with both tables?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Getting information from DataSet to populate defaults in controls.

    You might want to follow the CodeBank link in my signature and check out my thread on Master/Detail Data-binding. Your situation is not exactly the same but it's similar enough.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I tried it again and it still isn't working for me.

    Argument Exception
    These columns don't currently have unique values
    The error happens on the Relations.Add line.
    vb Code:
    1. 'Create a new relationship between the two DataTables in the DataSet.
    2.                 ClientInfoDS.Relations.Add("Phone Numbers", _
    3.                                            ClientInfoNamePhoneDT.Columns(TableName1_PKFKColumn), _
    4.                                            ClientPhoneNumberTypeDT.Columns(TableName2_PKColumn))

    My current code is:
    vb Code:
    1. Private Sub cboClientName_LostFocus(ByVal sender As Object, _
    2.                                         ByVal e As System.EventArgs) Handles cboClientName.LostFocus
    3.  
    4.         'Check the backend database for existing clients based on name.
    5.         'If multiple phone numbers exist for the client, show a form to be able to select a phone number.
    6.         'If single phone number exists, just populate the form.
    7.         Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
    8.             Dim TableName1 As String = "ClientInformation"
    9.             Dim TableName1_PKFKColumn As String = "PhoneNumberTypeID_PKFK"
    10.             Dim TableName1_ColumnName As String = "ClientName"
    11.             Dim TableName1_ColumnName2 As String = "ClientPhoneNumber"
    12.             Dim TableName2 As String = "PhoneNumber_Type"
    13.             Dim TableName2_PKColumn As String = "PhoneNumberTypeID_PK"
    14.             Dim TableName2_ColumnName As String = "PhoneNumberTypeName"
    15.  
    16.             Try
    17.                 'Open the database connection.
    18.                 SetDatabaseConnection.Open()
    19.  
    20.                 'Create a new DataSet to hold two DataTables.
    21.                 Dim ClientInfoDS As DataSet = New DataSet()
    22.  
    23.                 'Set the locale of the DataSet.
    24.                 ClientInfoDS.Locale = CurrentCulture
    25.  
    26.                 'Create two DataTables for each database table.
    27.                 Dim ClientInfoNamePhoneDT As DataTable = New DataTable("ClientInfoNamePhone")
    28.                 Dim ClientPhoneNumberTypeDT As DataTable = New DataTable("ClientPhoneNumberType")
    29.  
    30.                 'Set the locale of the DataTables.
    31.                 ClientInfoNamePhoneDT.Locale = CurrentCulture
    32.                 ClientPhoneNumberTypeDT.Locale = CurrentCulture
    33.  
    34.                 'Add some columns to the DataTables.
    35.                 With ClientInfoNamePhoneDT.Columns
    36.                     .Add(TableName1_ColumnName, GetType(String))
    37.                     .Add(TableName1_ColumnName2, GetType(String))
    38.                     .Add(TableName1_PKFKColumn, GetType(Integer))
    39.                 End With
    40.  
    41.                 With ClientPhoneNumberTypeDT.Columns
    42.                     .Add(TableName2_PKColumn, GetType(Integer))
    43.                     .Add(TableName2_ColumnName, GetType(String))
    44.                 End With
    45.  
    46.                 'Create two SQLCommands to get the information from the database.
    47.                 Dim ClientInfoNamePhoneCommand As SqlCommand = New SqlCommand
    48.                 Dim ClientPhoneNumberTypeCommand As SqlCommand = New SqlCommand
    49.  
    50.                 'Clear the dataset of previously obtained information.
    51.                 ClientInfoDS.Clear()
    52.  
    53.                 'Define the information from the SQLCommands.
    54.                 With ClientInfoNamePhoneCommand
    55.                     .Connection = SetDatabaseConnection
    56.                     .CommandType = CommandType.Text
    57.                     .CommandText = "SELECT " & TableName1_PKFKColumn & "," _
    58.                                    & TableName1_ColumnName & "," _
    59.                                    & TableName1_ColumnName2 & _
    60.                                    " FROM " & TableName1
    61.                     .ExecuteNonQuery()
    62.                 End With
    63.  
    64.                 With ClientPhoneNumberTypeCommand
    65.                     .Connection = SetDatabaseConnection
    66.                     .CommandType = CommandType.Text
    67.                     .CommandText = "SELECT " & TableName2_PKColumn & "," _
    68.                                    & TableName2_ColumnName & _
    69.                                    " FROM " & TableName2
    70.                     .ExecuteNonQuery()
    71.                 End With
    72.  
    73.                 'Create two DataAdapters so that we can fill the DataTables with information.
    74.                 Dim ClientInfoNamePhoneDA As SqlDataAdapter = New SqlDataAdapter(ClientInfoNamePhoneCommand)
    75.                 Dim ClientPhoneNumberTypeDA As SqlDataAdapter = New SqlDataAdapter(ClientPhoneNumberTypeCommand)
    76.  
    77.                 'Fill the DataAdapters.
    78.                 ClientInfoNamePhoneDA.Fill(ClientInfoNamePhoneDT)
    79.                 ClientPhoneNumberTypeDA.Fill(ClientPhoneNumberTypeDT)
    80.  
    81.                 'Now add the DataTables to the DataSet.
    82.                 With ClientInfoDS
    83.                     .Tables.Add(ClientInfoNamePhoneDT)
    84.                     .Tables.Add(ClientPhoneNumberTypeDT)
    85.                 End With
    86.  
    87.                 'Create a new relationship between the two DataTables in the DataSet.
    88.                 ClientInfoDS.Relations.Add("Phone Numbers", _
    89.                                            ClientInfoNamePhoneDT.Columns(TableName1_PKFKColumn), _
    90.                                            ClientPhoneNumberTypeDT.Columns(TableName2_PKColumn))
    91.  
    92.                 'Now create two DataRows for the records.
    93.                 Dim ParentDR, ChildDR As DataRow
    94.  
    95.                 'Loop through the records.
    96.                 For Each ParentDR In ClientInfoDS.Tables(TableName1).Rows
    97.                     For Each ChildDR In ParentDR.GetChildRows(TableName2)
    98.                         'Check to see if there are actually records available.
    99.                         If ClientInfoDS.Tables(TableName1).Rows.Count > 0 Then
    100.                             'Records available.
    101.                             ParentDR = ClientInfoDS.Tables(TableName1).Rows.Find(cboClientName.Text.Trim)
    102.  
    103.                             Select Case ClientInfoDS.Tables(TableName1).Rows.Count
    104.                                 Case 1 'One record only.
    105.                                     'Now if there is only one phone number returned, populate the other controls.
    106.                                     'Populate the phone number field.
    107.                                     mtbClientPhoneNumber.Text = ParentDR(1).ToString
    108.                                     cboPhoneNumberType.Text = ChildDR(2).ToString
    109.                                 Case Else 'Multiple records returned.
    110.                                     'Show a form for the end-user to choose the phone number and phone number type they want.
    111.  
    112.                             End Select
    113.                         Else
    114.                             'Nothing returned.
    115.                             'Focus the phone number field.
    116.                             mtbClientPhoneNumber.Focus()
    117.                         End If
    118.                     Next ChildDR
    119.                 Next ParentDR
    120.  
    121.                 'Close the database connection.
    122.                 SetDatabaseConnection.Close()
    123.             Catch ex As SqlException
    124.                 MessageBox.Show("There was an error retrieving data from the databse." & vbNewLine & vbNewLine & _
    125.                                 "The reason why you got this error in the first place is:" & vbNewLine & _
    126.                                 "No data items in the database to pull." & vbNewLine & vbNewLine & _
    127.                                 "The exception error is:" & vbNewLine & _
    128.                                 ex.Message, _
    129.                                 "Database Information Retrieval Issues.", _
    130.                                 MessageBoxButtons.OK, _
    131.                                 MessageBoxIcon.Error, _
    132.                                 MessageBoxDefaultButton.Button1, _
    133.                                 MessageBoxOptions.DefaultDesktopOnly, _
    134.                                 False)
    135.             Catch ex As InvalidOperationException
    136.                 MessageBox.Show(ex.Message, _
    137.                                 "Invalid Operation Exception.", _
    138.                                 MessageBoxButtons.OK, _
    139.                                 MessageBoxIcon.Error, _
    140.                                 MessageBoxDefaultButton.Button1, _
    141.                                 MessageBoxOptions.DefaultDesktopOnly, _
    142.                                 False)
    143.             Catch ex As NullReferenceException
    144.                 MessageBox.Show(ex.Message, _
    145.                                 "Null Reference Exception.", _
    146.                                 MessageBoxButtons.OK, _
    147.                                 MessageBoxIcon.Error, _
    148.                                 MessageBoxDefaultButton.Button1, _
    149.                                 MessageBoxOptions.DefaultDesktopOnly, _
    150.                                 False)
    151.             Catch ex As ArgumentException
    152.                 MessageBox.Show(ex.Message, _
    153.                                 "Argument Exception.", _
    154.                                 MessageBoxButtons.OK, _
    155.                                 MessageBoxIcon.Error, _
    156.                                 MessageBoxDefaultButton.Button1, _
    157.                                 MessageBoxOptions.DefaultDesktopOnly, _
    158.                                 False)
    159.             End Try
    160.         End Using
    161.     End Sub

    Does this mean that the column names can't be the same names as in the database?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Getting information from DataSet to populate defaults in controls.

    Refer to post #4.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    Quote Originally Posted by jmcilhinney View Post
    Refer to post #4.
    I switched them and then got:
    Argument Exception
    'column' argument cannot be null.
    Parameter name: column
    Happens on the same line each time. I the put a break point on that offending line and it shows the right values. This is starting to frustrate me

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I have been trying to work through this and now I do not get any errors and nothing happens when the cursor loses focus in the control.

    My code is as follows:
    vb Code:
    1. Private Sub cboClientName_LostFocus(ByVal sender As Object, _
    2.                                         ByVal e As System.EventArgs) Handles cboClientName.LostFocus
    3.  
    4.         'Check the backend database for existing clients based on name.
    5.         'If multiple phone numbers exist for the client, show a form to be able to select a phone number.
    6.         'If single phone number exists, just populate the form.
    7.         Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
    8.             Dim TableName1 As String = "ClientInformation"
    9.             Dim TableName1_PKFKColumn As String = "PhoneNumberTypeID_PKFK"
    10.             Dim TableName1_ColumnName As String = "ClientName"
    11.             Dim TableName1_ColumnName2 As String = "ClientPhoneNumber"
    12.             Dim TableName2 As String = "PhoneNumber_Type"
    13.             Dim TableName2_PKColumn As String = "PhoneNumberTypeID_PK"
    14.             Dim TableName2_ColumnName As String = "PhoneNumberTypeName"
    15.  
    16.             Try
    17.                 'Open the database connection.
    18.                 SetDatabaseConnection.Open()
    19.  
    20.                 'Create a new DataSet to hold two DataTables.
    21.                 Dim ClientInfoDS As DataSet = New DataSet()
    22.  
    23.                 'Set the locale of the DataSet.
    24.                 ClientInfoDS.Locale = CurrentCulture
    25.  
    26.                 'Create two DataTables for each database table.
    27.                 Dim ClientInfoNamePhoneDT As DataTable = New DataTable(TableName1)
    28.                 Dim ClientPhoneNumberTypeDT As DataTable = New DataTable(TableName2)
    29.  
    30.                 'Set the locale of the DataTables.
    31.                 ClientInfoNamePhoneDT.Locale = CurrentCulture
    32.                 ClientPhoneNumberTypeDT.Locale = CurrentCulture
    33.  
    34.                 'Add some columns to the DataTables.
    35.                 With ClientInfoNamePhoneDT.Columns
    36.                     .Add(TableName1_ColumnName, GetType(String))
    37.                     .Add(TableName1_ColumnName2, GetType(String))
    38.                     .Add(TableName1_PKFKColumn, GetType(Integer))
    39.                 End With
    40.  
    41.                 With ClientPhoneNumberTypeDT.Columns
    42.                     .Add(TableName2_PKColumn, GetType(Integer))
    43.                     .Add(TableName2_ColumnName, GetType(String))
    44.                 End With
    45.  
    46.                 'Create two SQLCommands to get the information from the database.
    47.                 Dim ClientInfoNamePhoneCommand As SqlCommand = New SqlCommand
    48.                 Dim ClientPhoneNumberTypeCommand As SqlCommand = New SqlCommand
    49.  
    50.                 'Clear the dataset of previously obtained information.
    51.                 ClientInfoDS.Clear()
    52.  
    53.                 'Define the information from the SQLCommands.
    54.                 With ClientInfoNamePhoneCommand
    55.                     .Connection = SetDatabaseConnection
    56.                     .CommandType = CommandType.Text
    57.                     .CommandText = "SELECT " & TableName1_PKFKColumn & "," _
    58.                                    & TableName1_ColumnName & "," _
    59.                                    & TableName1_ColumnName2 & _
    60.                                    " FROM " & TableName1
    61.                     .ExecuteNonQuery()
    62.                 End With
    63.  
    64.                 With ClientPhoneNumberTypeCommand
    65.                     .Connection = SetDatabaseConnection
    66.                     .CommandType = CommandType.Text
    67.                     .CommandText = "SELECT " & TableName2_PKColumn & "," _
    68.                                    & TableName2_ColumnName & _
    69.                                    " FROM " & TableName2
    70.                     .ExecuteNonQuery()
    71.                 End With
    72.  
    73.                 'Create two DataAdapters so that we can fill the DataTables with information.
    74.                 Dim ClientInfoNamePhoneDA As SqlDataAdapter = New SqlDataAdapter(ClientInfoNamePhoneCommand)
    75.                 Dim ClientPhoneNumberTypeDA As SqlDataAdapter = New SqlDataAdapter(ClientPhoneNumberTypeCommand)
    76.  
    77.                 'Fill the DataAdapters.
    78.                 ClientInfoNamePhoneDA.Fill(ClientInfoNamePhoneDT)
    79.                 ClientPhoneNumberTypeDA.Fill(ClientPhoneNumberTypeDT)
    80.  
    81.                 'Now add the DataTables to the DataSet.
    82.                 With ClientInfoDS
    83.                     .Tables.Add(ClientInfoNamePhoneDT)
    84.                     .Tables.Add(ClientPhoneNumberTypeDT)
    85.                 End With
    86.  
    87.                 'Create a new relationship between the two DataTables in the DataSet.
    88.                 Dim ClientInfoDRe As DataRelation
    89.                 Dim ParentTable, ChildTable As DataColumn
    90.  
    91.                 With ClientInfoDS
    92.                     ParentTable = .Tables(TableName2).Columns(TableName2_PKColumn) 'PK (PhoneNumber_Type)
    93.                     ChildTable = .Tables(TableName1).Columns(TableName1_PKFKColumn) 'PKFK (ClientInformation)
    94.                     ClientInfoDRe = New DataRelation("Phone Numbers", ParentTable, ChildTable)
    95.                     .Relations.Add(ClientInfoDRe)
    96.                 End With
    97.  
    98.                 'Now create two DataRows for the records.
    99.                 Dim ParentDR, ChildDR As DataRow
    100.  
    101.                 'Loop through the records.
    102.                 For Each ParentDR In ClientInfoDS.Tables(TableName2).Rows
    103.                     For Each ChildDR In ParentDR.GetChildRows(TableName1)
    104.                         'Check to see if there are actually records available.
    105.                         If ClientInfoDS.Tables(TableName2).Rows.Count > 0 Then
    106.                             'Records available.
    107.                             ChildDR = ClientInfoDS.Tables(TableName1).Rows.Find(cboClientName.Text.Trim)
    108.  
    109.                             Select Case ClientInfoDS.Tables(TableName1).Rows.Count
    110.                                 Case 1 'One record only.
    111.                                     'Now if there is only one phone number returned, populate the other controls.
    112.                                     'Populate the phone number field.
    113.                                     mtbClientPhoneNumber.Text = ChildDR(TableName1_ColumnName2).ToString
    114.                                     cboPhoneNumberType.Text = ParentDR(TableName2_ColumnName).ToString
    115.                                 Case Else 'Multiple records returned.
    116.                                     'Show a form for the end-user to choose the phone number and phone number type they want.
    117.  
    118.                             End Select
    119.                         Else
    120.                             'Nothing returned.
    121.                             'Focus the phone number field.
    122.                             mtbClientPhoneNumber.Focus()
    123.                         End If
    124.                     Next ChildDR
    125.                 Next ParentDR
    126.  
    127.                 'Close the database connection.
    128.                 SetDatabaseConnection.Close()
    129.             Catch ex As SqlException
    130.                 MessageBox.Show("There was an error retrieving data from the databse." & vbNewLine & vbNewLine & _
    131.                                 "The reason why you got this error in the first place is:" & vbNewLine & _
    132.                                 "No data items in the database to pull." & vbNewLine & vbNewLine & _
    133.                                 "The exception error is:" & vbNewLine & _
    134.                                 ex.Message, _
    135.                                 "Database Information Retrieval Issues.", _
    136.                                 MessageBoxButtons.OK, _
    137.                                 MessageBoxIcon.Error, _
    138.                                 MessageBoxDefaultButton.Button1, _
    139.                                 MessageBoxOptions.DefaultDesktopOnly, _
    140.                                 False)
    141.             Catch ex As InvalidOperationException
    142.                 MessageBox.Show(ex.Message, _
    143.                                 "Invalid Operation Exception.", _
    144.                                 MessageBoxButtons.OK, _
    145.                                 MessageBoxIcon.Error, _
    146.                                 MessageBoxDefaultButton.Button1, _
    147.                                 MessageBoxOptions.DefaultDesktopOnly, _
    148.                                 False)
    149.             Catch ex As NullReferenceException
    150.                 MessageBox.Show(ex.Message, _
    151.                                 "Null Reference Exception.", _
    152.                                 MessageBoxButtons.OK, _
    153.                                 MessageBoxIcon.Error, _
    154.                                 MessageBoxDefaultButton.Button1, _
    155.                                 MessageBoxOptions.DefaultDesktopOnly, _
    156.                                 False)
    157.             Catch ex As ArgumentException
    158.                 MessageBox.Show(ex.Message, _
    159.                                 "Argument Exception.", _
    160.                                 MessageBoxButtons.OK, _
    161.                                 MessageBoxIcon.Error, _
    162.                                 MessageBoxDefaultButton.Button1, _
    163.                                 MessageBoxOptions.DefaultDesktopOnly, _
    164.                                 False)
    165.             End Try
    166.         End Using
    167.     End Sub

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I am going insane and will need a straight jacket before long. I am making progress though. The new error message I am getting is:

    MissingPrimaryKeyException
    Table doesn't have a primary key.
    The thing is, BOTH tables have a primary key and I have added them to the datatables. The line the error is thrown on is:
    vb Code:
    1. ChildDR = ClientInfoDS.Tables(ClientInformationTable).Rows.Find(cboClientName.Text.Trim)

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    My code:
    vb Code:
    1. Private Sub cboClientName_LostFocus(ByVal sender As Object, _
    2.                                         ByVal e As System.EventArgs) Handles cboClientName.LostFocus
    3.  
    4.         'Check the backend database for existing clients based on name.
    5.         'If multiple phone numbers exist for the client, show a form to be able to select a phone number.
    6.         'If single phone number exists, just populate the form.
    7.         Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
    8.             Dim ClientInformationTable As String = "ClientInformation"
    9.             Dim ClientInformationTable_PKColumn As String = "ClientInfoID_PK"
    10.             Dim ClientInformationTable_PKFKColumn As String = "PhoneNumberTypeID_PKFK"
    11.             Dim ClientInformationTable_ClientNameColumn As String = "ClientName"
    12.             Dim ClientInformationTable_ClientPhoneNumberColumn As String = "ClientPhoneNumber"
    13.             Dim PhoneNumberTypeTable As String = "PhoneNumber_Type"
    14.             Dim PhoneNumberTypeTable_PKColumn As String = "PhoneNumberTypeID_PK"
    15.             Dim PhoneNumberTypeTable_PhoneNumberTypeNameColumn As String = "PhoneNumberTypeName"
    16.  
    17.             Try
    18.                 'Open the database connection.
    19.                 SetDatabaseConnection.Open()
    20.  
    21.                 'Create a new DataSet to hold two DataTables.
    22.                 Dim ClientInfoDS As DataSet = New DataSet()
    23.  
    24.                 'Set the locale of the DataSet.
    25.                 ClientInfoDS.Locale = CurrentCulture
    26.  
    27.                 'Create two DataTables for each database table.
    28.                 Dim ClientInfoNamePhoneDT As DataTable = New DataTable(ClientInformationTable)
    29.                 Dim ClientPhoneNumberTypeDT As DataTable = New DataTable(PhoneNumberTypeTable)
    30.  
    31.                 'Set the locale of the DataTables.
    32.                 ClientInfoNamePhoneDT.Locale = CurrentCulture
    33.                 ClientPhoneNumberTypeDT.Locale = CurrentCulture
    34.  
    35.                 'Add some columns to the DataTables.
    36.                 With ClientInfoNamePhoneDT.Columns
    37.                     .Add(ClientInformationTable_PKColumn, GetType(Integer))
    38.                     .Add(ClientInformationTable_ClientNameColumn, GetType(String))
    39.                     .Add(ClientInformationTable_ClientPhoneNumberColumn, GetType(String))
    40.                     .Add(ClientInformationTable_PKFKColumn, GetType(Integer))
    41.                 End With
    42.  
    43.                 With ClientPhoneNumberTypeDT.Columns
    44.                     .Add(PhoneNumberTypeTable_PKColumn, GetType(Integer))
    45.                     .Add(PhoneNumberTypeTable_PhoneNumberTypeNameColumn, GetType(String))
    46.                 End With
    47.  
    48.                 'Create two SQLCommands to get the information from the database.
    49.                 Dim ClientInfoNamePhoneCommand As SqlCommand = New SqlCommand
    50.                 Dim ClientPhoneNumberTypeCommand As SqlCommand = New SqlCommand
    51.  
    52.                 'Clear the dataset of previously obtained information.
    53.                 ClientInfoDS.Clear()
    54.  
    55.                 'Define the information from the SQLCommands.
    56.                 With ClientInfoNamePhoneCommand
    57.                     .Connection = SetDatabaseConnection
    58.                     .CommandType = CommandType.Text
    59.                     .CommandText = "SELECT " & ClientInformationTable_PKColumn & "," _
    60.                                    & ClientInformationTable_PKFKColumn & "," _
    61.                                    & ClientInformationTable_ClientNameColumn & "," _
    62.                                    & ClientInformationTable_ClientPhoneNumberColumn & _
    63.                                    " FROM " & ClientInformationTable
    64.                     .ExecuteNonQuery()
    65.                 End With
    66.  
    67.                 With ClientPhoneNumberTypeCommand
    68.                     .Connection = SetDatabaseConnection
    69.                     .CommandType = CommandType.Text
    70.                     .CommandText = "SELECT " & PhoneNumberTypeTable_PKColumn & "," _
    71.                                    & PhoneNumberTypeTable_PhoneNumberTypeNameColumn & _
    72.                                    " FROM " & PhoneNumberTypeTable
    73.                     .ExecuteNonQuery()
    74.                 End With
    75.  
    76.                 'Create two DataAdapters so that we can fill the DataTables with information.
    77.                 Dim ClientInfoNamePhoneDA As SqlDataAdapter = New SqlDataAdapter(ClientInfoNamePhoneCommand)
    78.                 Dim ClientPhoneNumberTypeDA As SqlDataAdapter = New SqlDataAdapter(ClientPhoneNumberTypeCommand)
    79.  
    80.                 'Fill the DataAdapters.
    81.                 ClientInfoNamePhoneDA.Fill(ClientInfoNamePhoneDT)
    82.                 ClientPhoneNumberTypeDA.Fill(ClientPhoneNumberTypeDT)
    83.  
    84.                 'Now add the DataTables to the DataSet.
    85.                 With ClientInfoDS
    86.                     .Tables.Add(ClientInfoNamePhoneDT)
    87.                     .Tables.Add(ClientPhoneNumberTypeDT)
    88.                 End With
    89.  
    90.                 'Create a new relationship between the two DataTables in the DataSet.
    91.                 Dim ClientInfoDRe As DataRelation
    92.                 Dim ParentTable, ChildTable As DataColumn
    93.  
    94.                 With ClientInfoDS
    95.                     ParentTable = .Tables(PhoneNumberTypeTable).Columns(PhoneNumberTypeTable_PKColumn) 'PK (PhoneNumber_Type)
    96.                     ChildTable = .Tables(ClientInformationTable).Columns(ClientInformationTable_PKFKColumn) 'PKFK (ClientInformation)
    97.                     ClientInfoDRe = New DataRelation("Phone Numbers", ParentTable, ChildTable)
    98.                     .Relations.Add(ClientInfoDRe)
    99.                 End With
    100.  
    101.                 'Now create two DataRows for the records.
    102.                 Dim ParentDR, ChildDR As DataRow
    103.  
    104.                 'Loop through the records.
    105.                 For Each ParentDR In ClientInfoDS.Tables(PhoneNumberTypeTable).Rows
    106.                     For Each ChildDR In ParentDR.GetChildRows(ClientInfoDRe)
    107.                         'Check to see if there are actually records available.
    108.                         If ClientInfoDS.Tables(PhoneNumberTypeTable).Rows.Count > 0 Then
    109.                             'Records available.
    110.                             ChildDR = ClientInfoDS.Tables(ClientInformationTable).Rows.Find(cboClientName.Text.Trim)
    111.  
    112.                             Select Case ClientInfoDS.Tables(ClientInformationTable).Rows.Count
    113.                                 Case 1 'One record only.
    114.                                     'Now if there is only one phone number returned, populate the other controls.
    115.                                     'Populate the phone number field.
    116.                                     mtbClientPhoneNumber.Text = ChildDR(ClientInformationTable_ClientPhoneNumberColumn).ToString
    117.                                     cboPhoneNumberType.Text = ParentDR(PhoneNumberTypeTable_PhoneNumberTypeNameColumn).ToString
    118.                                 Case Else 'Multiple records returned.
    119.                                     'Show a form for the end-user to choose the phone number and phone number type they want.
    120.  
    121.                             End Select
    122.                         Else
    123.                             'Nothing returned.
    124.                             'Focus the phone number field.
    125.                             mtbClientPhoneNumber.Focus()
    126.                         End If
    127.                     Next ChildDR
    128.                 Next ParentDR
    129.  
    130.                 'Close the database connection.
    131.                 SetDatabaseConnection.Close()
    132.             Catch ex As SqlException
    133.                 MessageBox.Show("There was an error retrieving data from the databse." & vbNewLine & vbNewLine & _
    134.                                 "The reason why you got this error in the first place is:" & vbNewLine & _
    135.                                 "No data items in the database to pull." & vbNewLine & vbNewLine & _
    136.                                 "The exception error is:" & vbNewLine & _
    137.                                 ex.Message, _
    138.                                 "Database Information Retrieval Issues.", _
    139.                                 MessageBoxButtons.OK, _
    140.                                 MessageBoxIcon.Error, _
    141.                                 MessageBoxDefaultButton.Button1, _
    142.                                 MessageBoxOptions.DefaultDesktopOnly, _
    143.                                 False)
    144.             Catch ex As InvalidOperationException
    145.                 MessageBox.Show(ex.Message, _
    146.                                 "Invalid Operation Exception.", _
    147.                                 MessageBoxButtons.OK, _
    148.                                 MessageBoxIcon.Error, _
    149.                                 MessageBoxDefaultButton.Button1, _
    150.                                 MessageBoxOptions.DefaultDesktopOnly, _
    151.                                 False)
    152.             Catch ex As NullReferenceException
    153.                 MessageBox.Show(ex.Message, _
    154.                                 "Null Reference Exception.", _
    155.                                 MessageBoxButtons.OK, _
    156.                                 MessageBoxIcon.Error, _
    157.                                 MessageBoxDefaultButton.Button1, _
    158.                                 MessageBoxOptions.DefaultDesktopOnly, _
    159.                                 False)
    160.             Catch ex As ArgumentException
    161.                 MessageBox.Show(ex.Message, _
    162.                                 "Argument Exception.", _
    163.                                 MessageBoxButtons.OK, _
    164.                                 MessageBoxIcon.Error, _
    165.                                 MessageBoxDefaultButton.Button1, _
    166.                                 MessageBoxOptions.DefaultDesktopOnly, _
    167.                                 False)
    168.             End Try
    169.         End Using
    170.     End Sub

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I got rid of the error by changing my code but as soon as the cursor has left the control (lost focus), still nothing happens and no errors!!!!

    I changed my code to reflect the following:
    vb Code:
    1. 'Add some columns to the DataTables.
    2.                 With ClientInfoNamePhoneDT
    3.                     .Columns.Add(New DataColumn(ClientInformationTable_PKColumn, GetType(Integer)))
    4.                     .Columns.Add(New DataColumn(ClientInformationTable_ClientNameColumn, GetType(String)))
    5.                     .Columns.Add(New DataColumn(ClientInformationTable_ClientPhoneNumberColumn, GetType(String)))
    6.                     .Columns.Add(New DataColumn(ClientInformationTable_PKFKColumn, GetType(Integer)))
    7.  
    8.                     'Declare a primary key column for the table.
    9.                     Dim PrimaryKey(1) As DataColumn
    10.                     PrimaryKey(1) = .Columns(ClientInformationTable_PKColumn)
    11.                     .PrimaryKey = PrimaryKey
    12.                 End With
    13.  
    14.                 With ClientPhoneNumberTypeDT
    15.                     .Columns.Add(New DataColumn(PhoneNumberTypeTable_PKColumn, GetType(Integer)))
    16.                     .Columns.Add(New DataColumn(PhoneNumberTypeTable_PhoneNumberTypeNameColumn, GetType(String)))
    17.  
    18.                     'Declare a primary key column for the table.
    19.                     Dim PrimaryKey(1) As DataColumn
    20.                     PrimaryKey(1) = .Columns(PhoneNumberTypeTable_PKColumn)
    21.                     .PrimaryKey = PrimaryKey
    22.                 End With

    It shouldn't be this difficult to do something simple! Definitely shouldn't have to write 186 lines of useless code to do a simple task!

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Getting information from DataSet to populate defaults in controls.

    There's a lot going wrong here. For one thing, I don't understand why you would be building the whole DataSet on the LostFocus event of a ComboBox. You really shouldn't be handling LostFocus at all, but surely you want the DataSet populated before that regardless.

    Please provide a clear, step by step explanation of what you're trying to accomplish from a functionality point of view, not code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    Quote Originally Posted by jmcilhinney View Post
    There's a lot going wrong here. For one thing, I don't understand why you would be building the whole DataSet on the LostFocus event of a ComboBox. You really shouldn't be handling LostFocus at all, but surely you want the DataSet populated before that regardless.

    Please provide a clear, step by step explanation of what you're trying to accomplish from a functionality point of view, not code.
    There definitely has to be an easier way but not sure of that way. The form in question has a combobox that is editable (can type in it) and it also is autocomplete suggest/append. The client names are populated in this combobox from the ClientInfo database table. As the end user types, the names are suggested. there might even be times that the end user keeps typing and the client name not be in the database.

    If the name selected by the end user is in the database and the cursor loses focus from this combobox, i am looking to check to see how many phone numbers this client name has. If there is only one phone number, the phone number and phone number type is auto populated in the other controls (maskedtextbox) and a non-editable combobox. If there are more than one phone number for this particular client, then a form comes up with a list of their phone numbers for the end user to choose from. Once chosen, the selection is defaulted. What I am doing with those datasets, datatables, relations and stuff is pretty interesting, even though very frustrating, but I think it's doing too much for a simple task.

    The phone number type is in another table with a FK relationship.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I am making some progress. I am able to populate the MaskedTextBox for the phone number based on the Client Name. However, when trying to get the Phone Number Type from another table, I get an error. Looks like I still have to go the one DataSet, two DataTables and a DataRelation route. Ugh! I did have two tables previously but had to restructure to include a third because of name duplication.

    I am told an instance is not set for this line:
    vb Code:
    1. cboPhoneNumberType.Text = CStr(.Tables("ClientInformation_PhoneNumberType").Rows(0).Item("PhoneNumberTypeName"))

    Code:
    vb Code:
    1. Private Sub cboClientName_LostFocus(ByVal sender As Object, _
    2.                                         ByVal e As System.EventArgs) Handles cboClientName.LostFocus
    3.  
    4.         'Check the backend database to see if the client name exists.
    5.         'If the client name exists, check to see how many phone numbers the client has.
    6.         'If there is only one phone number, populate the MaskedTextBox and ComboBox.
    7.         'If there are multiple phone numbers, show a form for the end user to choose the phone number
    8.         'they want.
    9.         Using SetDatabaseConnection As SqlConnection = New SqlConnection(GetDatabaseConnectionString)
    10.             Try
    11.                 'Open the connection to the database.
    12.                 SetDatabaseConnection.Open()
    13.  
    14.                 'Create a dataset.
    15.                 Dim ClientInfoDS As DataSet = New DataSet
    16.  
    17.                 'Create a new SQLCommand.
    18.                 Dim MySQLCommand As SqlCommand = New SqlCommand
    19.  
    20.                 With MySQLCommand
    21.                     .Connection = SetDatabaseConnection
    22.                     .CommandType = CommandType.Text
    23.                     .CommandText = "SELECT ClientInformation.ClientInfoID_PK, " & _
    24.                                    "ClientInformation.ClientName, " & _
    25.                                    "ClientInformation_PhoneNumbers.ClientPhoneNumber, " & _
    26.                                    "ClientInformation_PhoneNumbers.ClientInfoID_PKFK, " & _
    27.                                    "ClientInformation_PhoneNumbers.PhoneNumberTypeID_PKFK, " & _
    28.                                    "ClientInformation_PhoneNumberType.PhoneNumberTypeID_PK, " & _
    29.                                    "ClientInformation_PhoneNumberType.PhoneNumberTypeName " & _
    30.                                    "FROM ClientInformation " & _
    31.                                    "INNER JOIN ClientInformation_PhoneNumbers " & _
    32.                                    "ON ClientInformation.ClientInfoID_PK = ClientInformation_PhoneNumbers.ClientInfoID_PKFK " & _
    33.                                    "INNER JOIN ClientInformation_PhoneNumberType " & _
    34.                                    "ON ClientInformation_PhoneNumberType.PhoneNumberTypeID_PK = ClientInformation_PhoneNumbers.PhoneNumberTypeID_PKFK " & _
    35.                                    "WHERE ClientInformation.ClientName =" & "'" & cboClientName.Text.Trim & "'"
    36.                     .ExecuteNonQuery()
    37.                 End With
    38.  
    39.                 'Create a DataAdapter.
    40.                 Dim ClientInfoDA As SqlDataAdapter = New SqlDataAdapter(MySQLCommand.CommandText, _
    41.                                                                         SetDatabaseConnection)
    42.  
    43.  
    44.                 'Clear the DataSet prior to doing anything.
    45.                 ClientInfoDS.Clear()
    46.  
    47.                 'Fill the DataSet with the information found.
    48.                 ClientInfoDA.Fill(ClientInfoDS, "ClientInformation_PhoneNumbers")
    49.  
    50.                 'Check to see if any records were populated within the DataSet.
    51.                 With ClientInfoDS
    52.                     If .Tables("ClientInformation_PhoneNumbers").Rows.Count > 0 Then
    53.                         'Records were returned.
    54.                         'Lets see how many records.  If a single record or multiple.
    55.                         Select Case .Tables("ClientInformation_PhoneNumbers").Rows.Count
    56.                             Case 1 'Single record.
    57.                                 'Populate the phone number into the MaskedTextBox.
    58.                                 mtbClientPhoneNumber.Text = CStr(.Tables("ClientInformation_PhoneNumbers").Rows(0).Item("ClientPhoneNumber"))
    59.  
    60.                                 'Populate a default selection for the phone number type.
    61.                                 cboPhoneNumberType.Text = CStr(.Tables("ClientInformation_PhoneNumberType").Rows(0).Item("PhoneNumberTypeName"))
    62.                             Case Else 'Multiple records.
    63.                                 'Show a form for the end user to be able to select the phone number they want
    64.                                 'to use.
    65.                                 'TODO
    66.                         End Select
    67.                     Else
    68.                         'Records were not returned.
    69.                         'Focus the phone number field.
    70.                         mtbClientPhoneNumber.Focus()
    71.                     End If
    72.                 End With
    73.  
    74.                 'Close the database connection.
    75.                 SetDatabaseConnection.Close()
    76.             Catch ex As SqlException
    77.                 MessageBox.Show("There was an error getting data from the database. The error is:" & vbNewLine & _
    78.                                 ex.Message, _
    79.                                 "Error Retrieving Data.", _
    80.                                 MessageBoxButtons.OK, _
    81.                                 MessageBoxIcon.Information, _
    82.                                 MessageBoxDefaultButton.Button1, _
    83.                                 MessageBoxOptions.DefaultDesktopOnly, _
    84.                                 False)
    85.             End Try
    86.         End Using
    87.     End Sub

    Would my assumption be correct?
    Attached Images Attached Images  

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Getting information from DataSet to populate defaults in controls.

    OK, I went slow with my thinking and I finally did it! I have successfully achieved my goal with the use of a DataSet, 2 DataTables and a DataRelation. First time ever working with this. What a trip!

    The code is not fully completed but I am going to include it here in the event anyone in the future needs a reference point.
    vb Code:
    1. Private Sub cboClientName_LostFocus(ByVal sender As Object, _
    2.                                         ByVal e As System.EventArgs) Handles cboClientName.LostFocus
    3.  
    4.         'Check the backend database to see if the client name exists.
    5.         'If the client name exists, check to see how many phone numbers the client has.
    6.         'If there is only one phone number, populate the MaskedTextBox and ComboBox.
    7.         'If there are multiple phone numbers, show a form for the end user to choose the phone number
    8.         'they want.
    9.         Using SetDatabaseConnection As SqlConnection = New SqlConnection(GetDatabaseConnectionString)
    10.             Try
    11.                 'Open the connection to the database.
    12.                 SetDatabaseConnection.Open()
    13.  
    14.                 'Create a dataset.
    15.                 Dim ClientInfoDS As DataSet = New DataSet
    16.  
    17.                 'Create two DataTables.
    18.                 Dim ClientPhoneDT As DataTable = New DataTable("ClientInformation_PhoneNumbers")
    19.                 Dim ClientPhoneTypeDT As DataTable = New DataTable("ClientInformation_PhoneNumberType")
    20.  
    21.                 'Create the columns for the DataTables.
    22.                 With ClientPhoneDT
    23.                     .Columns.Add(New DataColumn("PhoneNumberID_PK", GetType(Integer)))
    24.                     .Columns.Add(New DataColumn("ClientPhoneNumber", GetType(String)))
    25.                     .Columns.Add(New DataColumn("PhoneNumberTypeID_PKFK", GetType(Integer)))
    26.                     .Columns.Add(New DataColumn("ClientInfoID_PKFK", GetType(Integer)))
    27.  
    28.                     'Set the primary key column.
    29.                     Dim PrimaryKey(1) As DataColumn
    30.                     PrimaryKey(1) = .Columns("PhoneNumberID_PK")
    31.                     .PrimaryKey = PrimaryKey
    32.                 End With
    33.  
    34.                 With ClientPhoneTypeDT
    35.                     .Columns.Add(New DataColumn("PhoneNumberTypeID_PK", GetType(Integer)))
    36.                     .Columns.Add(New DataColumn("PhoneNumberTypeName", GetType(String)))
    37.  
    38.                     'Set the primary key column.
    39.                     Dim PrimaryKey(1) As DataColumn
    40.                     PrimaryKey(1) = .Columns("PhoneNumberTypeID_PK")
    41.                     .PrimaryKey = PrimaryKey
    42.                 End With
    43.  
    44.                 'Add the DataTables to the DataSet.
    45.                 With ClientInfoDS
    46.                     .Tables.Add(ClientPhoneDT)
    47.                     .Tables.Add(ClientPhoneTypeDT)
    48.                 End With
    49.  
    50.                 'Create a DataRelation for the two tables.
    51.                 Dim ClientInfoDRe As DataRelation
    52.  
    53.                 'Create a new SQLCommand.
    54.                 Dim MySQLCommand As SqlCommand = New SqlCommand
    55.  
    56.                 'Set the attributes for the SQLCommand.
    57.                 With MySQLCommand
    58.                     .Connection = SetDatabaseConnection
    59.                     .CommandType = CommandType.Text
    60.                     .CommandText = "SELECT ClientInformation.ClientInfoID_PK, " & _
    61.                                    "ClientInformation.ClientName, " & _
    62.                                    "ClientInformation_PhoneNumbers.ClientPhoneNumber, " & _
    63.                                    "ClientInformation_PhoneNumbers.ClientInfoID_PKFK, " & _
    64.                                    "ClientInformation_PhoneNumbers.PhoneNumberTypeID_PKFK, " & _
    65.                                    "ClientInformation_PhoneNumberType.PhoneNumberTypeID_PK, " & _
    66.                                    "ClientInformation_PhoneNumberType.PhoneNumberTypeName " & _
    67.                                    "FROM ClientInformation " & _
    68.                                    "INNER JOIN ClientInformation_PhoneNumbers " & _
    69.                                    "ON ClientInformation.ClientInfoID_PK = ClientInformation_PhoneNumbers.ClientInfoID_PKFK " & _
    70.                                    "INNER JOIN ClientInformation_PhoneNumberType " & _
    71.                                    "ON ClientInformation_PhoneNumberType.PhoneNumberTypeID_PK = ClientInformation_PhoneNumbers.PhoneNumberTypeID_PKFK " & _
    72.                                    "WHERE ClientInformation.ClientName =" & "'" & cboClientName.Text.Trim & "'"
    73.                     .ExecuteNonQuery()
    74.                 End With
    75.  
    76.                 'Create a DataAdapter.
    77.                 Dim ClientInfoDA As SqlDataAdapter = New SqlDataAdapter(MySQLCommand.CommandText, _
    78.                                                                         SetDatabaseConnection)
    79.  
    80.  
    81.                 'Clear the DataSet prior to doing anything.
    82.                 ClientInfoDS.Clear()
    83.  
    84.                 'Fill the DataTables with the information found.
    85.                 With ClientInfoDA
    86.                     .Fill(ClientPhoneDT)
    87.                     .Fill(ClientPhoneTypeDT)
    88.                 End With
    89.  
    90.                 'Create two DataColumns for the DataRelation relationship.
    91.                 Dim ParentTableDC, ChildTableDC As DataColumn
    92.  
    93.                 With ClientInfoDS
    94.                     'Create the DataRelation between the PK/FK columns.
    95.                     ParentTableDC = .Tables("ClientInformation_PhoneNumberType").Columns("PhoneNumberTypeID_PK")
    96.                     ChildTableDC = .Tables("ClientInformation_PhoneNumbers").Columns("PhoneNumberTypeID_PKFK")
    97.  
    98.                     'Create a new DataRelation.
    99.                     ClientInfoDRe = New DataRelation("Phone Numbers", ParentTableDC, ChildTableDC)
    100.  
    101.                     'Add the DataRelation to the DataSet housing the two DataTables.
    102.                     .Relations.Add(ClientInfoDRe)
    103.                 End With
    104.  
    105.                 'Create two DataRows.  One for the parent and another for the child.
    106.                 Dim ParentTableDR, ChildTableDR As DataRow
    107.  
    108.                 'Now the fun begins, let's go ahead and get the information from the DataTables.
    109.                 'If there is one phone number, auto populate the MaskedTextBox with the phone number and ComboBox with the phone number type.
    110.                 'If there are multiple phone numbers, show a form to allow the end user to select the desired phone number.
    111.                 'If there are no records returned, focus the MaskedTextBox.
    112.                 For Each ParentTableDR In ClientInfoDS.Tables("ClientInformation_PhoneNumberType").Rows 'PK.
    113.                     For Each ChildTableDR In ParentTableDR.GetChildRows(ClientInfoDRe) 'FK.
    114.                         'Now see how many records were returned.
    115.                         Select Case ClientInfoDS.Tables("ClientInformation_PhoneNumberType").Rows.Count
    116.                             Case 0 'No records.
    117.                                 'No records returned, go ahead and focus the MaskedTextBox.
    118.                                 mtbClientPhoneNumber.Focus()
    119.  
    120.                                 'Go no further because nothing returned.
    121.                                 Exit For
    122.                             Case 1 'One record.
    123.                                 'Yippee!  A record was returned.
    124.                                 'Auto populate the phone number in the MaskedTextBox (Phone Number).
    125.                                 'Auto populate a default value in the ComboBox (Phone Number Type).
    126.                                 mtbClientPhoneNumber.Text = ChildTableDR("ClientPhoneNumber").ToString
    127.                                 cboPhoneNumberType.Text = ParentTableDR("PhoneNumberTypeName").ToString
    128.  
    129.                                 'Focus the Partner Name ComboBox.
    130.                                 cboPartnerName.Focus()
    131.                             Case Else 'Multiple records.
    132.                                 'Show a form to the end user, so that s/he can select from a list of available phone numbers.
    133.                                 'TODO
    134.                         End Select
    135.                     Next ChildTableDR
    136.                 Next ParentTableDR
    137.  
    138.                 'Close the database connection.
    139.                 SetDatabaseConnection.Close()
    140.             Catch ex As SqlException
    141.                 MessageBox.Show("There was an error getting data from the database. The error is:" & vbNewLine & _
    142.                                 ex.Message, _
    143.                                 "Error Retrieving Data.", _
    144.                                 MessageBoxButtons.OK, _
    145.                                 MessageBoxIcon.Information, _
    146.                                 MessageBoxDefaultButton.Button1, _
    147.                                 MessageBoxOptions.DefaultDesktopOnly, _
    148.                                 False)
    149.             End Try
    150.         End Using
    151.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width