Results 1 to 19 of 19

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

Threaded View

  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  

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