Results 1 to 15 of 15

Thread: SqlDataReader Add Blank Row

  1. #1

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

    SqlDataReader Add Blank Row

    Hi all,

    I am populating a ComboBox using an SPROC using a SqlDataReader and it works perfectly; however, I want to be able to manually add a blank row to the SqlDataReader, so that the default option in the ComboBox is empty.

    I can do this using a DataSet with no issues, but I don't want to update a database (MSSQL), I just want to pull information from the database to provide options to the end user.

    This is what I have and how I am going about it. Any suggestions as to how this can be done? I have searched for many hours on adding a blank row to a SqlDataReader and have not found anything so far.

    VB Code:
    1. ALTER PROCEDURE GetFeeFrequencies
    2. AS
    3. BEGIN
    4.     SELECT ServiceFeeFrequency AS SFF
    5.     FROM FeeFrequencies
    6.     ORDER BY SFF ASC
    7. END

    VB Code:
    1. Private Sub CCProcessors_Load(ByVal sender As System.Object, _
    2.                                   ByVal e As System.EventArgs) Handles MyBase.Load
    3.  
    4.         LoadFeeFreqDropDown(ServiceFeeFrequencySR)
    5.     End Sub
    6.  
    7.     Public Sub LoadFeeFreqDropDown(ByVal CBName As Elegant.Ui.ComboBox)
    8.         Using SetDatabaseConnection As New SqlConnection(ConnectToDatabase)
    9.             Using GetFeeFreq As SqlCommand = New SqlCommand
    10.                 Try
    11.                     SetDatabaseConnection.Open()
    12.  
    13.                     With GetFeeFreq
    14.                         .Connection = SetDatabaseConnection
    15.                         .CommandType = CommandType.StoredProcedure
    16.                         .CommandText = "GetFeeFrequencies"
    17.                         .ExecuteNonQuery()
    18.                     End With
    19.  
    20.                     Dim FeeFreqDr As SqlDataReader
    21.                     FeeFreqDr = GetFeeFreq.ExecuteReader
    22.  
    23.                     'TODO: Add a blank entry/row to the SqlDataReader and make it default.
    24.  
    25.                     With CBName
    26.                         While FeeFreqDr.Read
    27.                             If FeeFreqDr.HasRows Then
    28.                                 .AutoCompleteMode = AutoCompleteMode.SuggestAppend
    29.                                 .AutoCompleteSource = AutoCompleteSource.ListItems
    30.                                 .Items.Add(FeeFreqDr("SFF"))
    31.                                 .DisplayMember = CStr(FeeFreqDr("SFF"))
    32.                                 .ValueMember = CStr(FeeFreqDr("SFF"))
    33.                                 '.Text = vbNullString
    34.                             End If
    35.                         End While
    36.                     End With
    37.  
    38.                     FeeFreqDr.Close()
    39.                 Catch CastEx As InvalidCastException
    40.                     MessageBox.Show(CastEx.ToString, _
    41.                                     "Invalid Cast Exception", _
    42.                                     MessageBoxButtons.OK, _
    43.                                     MessageBoxIcon.Error, _
    44.                                     MessageBoxDefaultButton.Button1)
    45.                 Catch ArgEx As ArgumentException
    46.                     MessageBox.Show(ArgEx.ToString, _
    47.                                     "Argument Exception", _
    48.                                     MessageBoxButtons.OK, _
    49.                                     MessageBoxIcon.Error, _
    50.                                     MessageBoxDefaultButton.Button1)
    51.                 Catch NullRefEx As NullReferenceException
    52.                     MessageBox.Show(NullRefEx.ToString, _
    53.                                     "Null Reference Exception", _
    54.                                     MessageBoxButtons.OK, _
    55.                                     MessageBoxIcon.Error, _
    56.                                     MessageBoxDefaultButton.Button1)
    57.                 Catch SQLEx As SqlException
    58.                     MessageBox.Show(SQLEx.ToString, _
    59.                                     "SQL Exception", _
    60.                                     MessageBoxButtons.OK, _
    61.                                     MessageBoxIcon.Error, _
    62.                                     MessageBoxDefaultButton.Button1)
    63.                 Catch FormatEx As FormatException
    64.                     MessageBox.Show(FormatEx.ToString, _
    65.                                     "Format Exception", _
    66.                                     MessageBoxButtons.OK, _
    67.                                     MessageBoxIcon.Error, _
    68.                                     MessageBoxDefaultButton.Button1)
    69.                 Catch OverflowEx As OverflowException
    70.                     MessageBox.Show(OverflowEx.ToString, _
    71.                                     "Overflow Exception", _
    72.                                     MessageBoxButtons.OK, _
    73.                                     MessageBoxIcon.Error, _
    74.                                     MessageBoxDefaultButton.Button1)
    75.                 Catch IdxOutOfRangeEx As IndexOutOfRangeException
    76.                     MessageBox.Show(IdxOutOfRangeEx.ToString, _
    77.                                     "Index Out Of Range Exception", _
    78.                                     MessageBoxButtons.OK, _
    79.                                     MessageBoxIcon.Error, _
    80.                                     MessageBoxDefaultButton.Button1)
    81.                 Finally
    82.                     SetDatabaseConnection.Close()
    83.                 End Try
    84.             End Using
    85.         End Using
    86.     End Sub

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,518

    Re: SqlDataReader Add Blank Row

    I can do this using a DataSet with no issues, but I don't want to update a database (MSSQL)
    You wouldn't use a dataset, you would use a datatable (it could be part of a dataset or a stand alone). There's no issue with updating the database, you just don't call the update method.

  3. #3

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

    Re: SqlDataReader Add Blank Row

    Quote Originally Posted by wes4dbt View Post
    You wouldn't use a dataset, you would use a datatable (it could be part of a dataset or a stand alone). There's no issue with updating the database, you just don't call the update method.
    Oh, okay wes4dbt. I'll look into the DataTable. It's been a while since I used one.

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: SqlDataReader Add Blank Row

    Hi,

    Maybe I am missing something or maybe this is just the default behaviour of the Elegant.UI.ComboBox but a standard ComboBox does not have anything selected when you use its Add or AddRange Methods and even if it did then to Deselect Something after Items have been added you would just set the SelectedIndex Property to -1. Is this not possible with the Elegant.UI.ComboBox control?

    On another note, that bit of code you posted is a little inefficient since it checks a DataReader for Rows after it Starts to read it and you set the ComboBox properties multiple times while reading the reader which you should not do. Finally you only need to set the DisplayMember and ValueMember Properties when you set the DataSource Property of the ComboBox but again this may not be the case with this Custom Control. Have a try with this and depending on how many items you are loading you should see a significant increase in Speed when loading the Control:-

    vb.net Code:
    1. If FeeFreqDr.HasRows Then
    2.   With CBName
    3.     .AutoCompleteMode = AutoCompleteMode.SuggestAppend
    4.     .AutoCompleteSource = AutoCompleteSource.ListItems
    5.     While FeeFreqDr.Read
    6.       .Items.Add(FeeFreqDr("FullName"))
    7.     End While
    8.     .SelectedIndex = -1
    9.   End With
    10. End If

    Hope that helps.

    Cheers,

    Ian

  5. #5

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

    Re: SqlDataReader Add Blank Row

    Yes, by default, the option is blank; however, if the end user selects an option from the list of available options, they are not able to specify a blank option should they want to. This is the reason I wanted to add a blank line to the ComboBox. Only way I can do that as far as I can tell is to add the blank line/record/row to the SqlDataReader. I just haven't been able to figure that out. I can do it on an SqlDataSet. Someone also suggested to use a DataTable and do it that way. Either way, the options for my ComboBoxes are pulled from a database, so that all the ComboBoxes that use the same options are not hard coded and can be added or removed and then all ComboBoxes using the database records would be affected. The Elegant ComboBox is based off the WinForms ComboBox, but the coding is a little different. It's a great product and it makes my WinForms apps look like Microsoft products with ease.
    Name:  1.jpg
Views: 254
Size:  28.6 KB
    Name:  2.jpg
Views: 244
Size:  28.2 KB

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: SqlDataReader Add Blank Row

    You can't add a blank line to a datareader, that would pretty much defeat the purpose of a datareader. However, you don't need to. The way you were populating the control is by looping through the reader and adding each item in turn. You never needed the valuemember or displaymember, because you weren't actually binding the control to the reader. So, just add a blank line to the control prior to populating it from the datareader.
    My usual boring signature: Nothing

  7. #7

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

    Re: SqlDataReader Add Blank Row

    I made a mistake in my programming and fixed the issue. The ComboBox is SuggestAppend, but was not editable, so changed it from False to True and can just delete the item selected and I am all good for now.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,518

    Re: SqlDataReader Add Blank Row

    If you really want to use a reader then don't bind the combobox. Load the items using a loop. Like IanRydar shown but just add the blank line before you enter the loop.
    Code:
    If FeeFreqDr.HasRows Then
      With CBName
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems
        .Items.Add(" ")
        While FeeFreqDr.Read
          .Items.Add(FeeFreqDr("FullName"))
        End While
        .SelectedIndex = -1
      End With
    End If

  9. #9

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

    Re: SqlDataReader Add Blank Row

    Quote Originally Posted by Shaggy Hiker View Post
    You can't add a blank line to a datareader, that would pretty much defeat the purpose of a datareader. However, you don't need to. The way you were populating the control is by looping through the reader and adding each item in turn. You never needed the valuemember or displaymember, because you weren't actually binding the control to the reader. So, just add a blank line to the control prior to populating it from the datareader.
    I did try adding a blank line, but the blank line was showing the same amount as the others, so if I had five options, there were five blank lines, too. I didn't know you can't add blank lines to DataReaders. Learned something new there. Thanks for the info. I will have to read up more about ValueMember and DisplayMember to learn exactly what they do.

  10. #10

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

    Re: SqlDataReader Add Blank Row

    Quote Originally Posted by wes4dbt View Post
    If you really want to use a reader then don't bind the combobox. Load the items using a loop. Like IanRydar shown but just add the blank line before you enter the loop.
    Code:
    If FeeFreqDr.HasRows Then
      With CBName
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems
        .Items.Add(" ")
        While FeeFreqDr.Read
          .Items.Add(FeeFreqDr("FullName"))
        End While
        .SelectedIndex = -1
      End With
    End If
    Thanks for the code snippet.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: SqlDataReader Add Blank Row

    You were adding the blank line inside the loop, then. Do it as wes4dbt showed: Add one blank line, THEN fill the control with the loop.

    However, I'd suggest that you not add a line that is truly blank. Blank lines in comboboxes are EASILY overlooked by the user. If the purpose of the blank line is to indicate "no selection", then put something like "<No Selection>" in the control. An actual blank line just doesn't make it clear to the user what impact selecting it will have.
    My usual boring signature: Nothing

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SqlDataReader Add Blank Row

    Quote Originally Posted by BrailleSchool View Post
    I did try adding a blank line, but the blank line was showing the same amount as the others, so if I had five options, there were five blank lines, too. I didn't know you can't add blank lines to DataReaders. Learned something new there. Thanks for the info. I will have to read up more about ValueMember and DisplayMember to learn exactly what they do.
    Probably because you were adding the blank inside hte loop... had you had it on the outside, it would have only added it once.

    ValueMember and DisplayMember are useful when binding the combobox to a list of somethings... could be a List(Of), or a DataTable, or even an array I think. Basically anything that implements IEnumerable - I think that's right. It wouldn't help in this case since you're not actually binding to anything - which is why then adding the blank manually at the start is all you should need.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

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

    Re: SqlDataReader Add Blank Row

    Quote Originally Posted by Shaggy Hiker View Post
    You were adding the blank line inside the loop, then. Do it as wes4dbt showed: Add one blank line, THEN fill the control with the loop.

    However, I'd suggest that you not add a line that is truly blank. Blank lines in comboboxes are EASILY overlooked by the user. If the purpose of the blank line is to indicate "no selection", then put something like "<No Selection>" in the control. An actual blank line just doesn't make it clear to the user what impact selecting it will have.
    That's what I usually do, but I also check for blanks before committing to a database.

  14. #14

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

    Re: SqlDataReader Add Blank Row

    Quote Originally Posted by techgnome View Post
    Probably because you were adding the blank inside hte loop... had you had it on the outside, it would have only added it once.

    ValueMember and DisplayMember are useful when binding the combobox to a list of somethings... could be a List(Of), or a DataTable, or even an array I think. Basically anything that implements IEnumerable - I think that's right. It wouldn't help in this case since you're not actually binding to anything - which is why then adding the blank manually at the start is all you should need.

    -tg
    I removed the DisplayMember and ValueMember from the code and nothing changed. It displays as intended.
    I refer to MSDN and VBF all the time for different things. Always try to research myself before posting as it may have been answered before. Thanks for the clarification.

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SqlDataReader Add Blank Row

    Quote Originally Posted by BrailleSchool View Post
    I removed the DisplayMember and ValueMember from the code and nothing changed. It displays as intended.
    I refer to MSDN and VBF all the time for different things. Always try to research myself before posting as it may have been answered before. Thanks for the clarification.
    Of course it did... you weren't using binding, so the two properties have no effect.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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