Results 1 to 3 of 3

Thread: [RESOLVED] [02/03]Add values to a hash table

  1. #1

    Thread Starter
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Resolved [RESOLVED] [02/03]Add values to a hash table

    Hi All,
    I'm trying to get data (2 columns) from SQL Server 2000 database (it sounds ok) to a datatable and adding these data to a hashtable for lookup and add one column to a combo box. when an item is selected from the combo box it get to the appropriate referenced value from the hash table. But when I'm going to run it, an error is occurred at the marked area. I'm sure I've done a greate mistake. Please help me to get rid of this.

    VB Code:
    1. Private Sub PopulateCustomer()
    2.         '//This procedure retriews all customers
    3.         Dim SelectProducts = New SqlClient.SqlCommand
    4.         Dim myReader As SqlDataReader
    5.         MyConnection = New SqlConnection(MyCONNECTIONSTRING)
    6.         SelectProducts = New SqlClient.SqlCommand("SELECT Name, DCLink FROM Client Order By Name", MyConnection)
    7.         MyConnection.Open()
    8.         myReader = SelectProducts.ExecuteReader()
    9.         myDataColumn = New DataColumn
    10.         myDataColumn.DataType = System.Type.GetType("System.Int16")
    11.         myDataColumn.ColumnName = "id"
    12.         myDataColumn.ReadOnly = True
    13.         myDataColumn.Unique = True
    14.         tblCustomer.Columns.Add(myDataColumn)
    15.         '==>>>
    16.         myDataColumn = New DataColumn
    17.         myDataColumn.DataType = System.Type.GetType("System.String")
    18.         myDataColumn.ColumnName = "CustomerName"
    19.         myDataColumn.AutoIncrement = False
    20.         myDataColumn.Caption = "Customer Name"
    21.         myDataColumn.ReadOnly = False
    22.         myDataColumn.Unique = False
    23.         tblCustomer.Columns.Add(myDataColumn)
    24.  
    25.         If myReader.HasRows Then
    26.             Do While myReader.Read()
    27.                 myDataRow = tblCustomer.NewRow()
    28.                 myDataRow("id") = myReader.GetValue(1)
    29.                 myDataRow("CustomerName") = myReader.GetString(0)
    30.                 tblCustomer.Rows.Add(myDataRow)
    31.             Loop
    32.         Else
    33.             MsgBox("Customer Database is empty", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, "Load Customer")
    34.         End If
    35.         MyConnection.Close()
    36.         MyConnection = Nothing
    37.         myReader = Nothing
    38.         Dim Lookup As New Hashtable
    39. <'--This is the area the error is occurred
    40.        [COLOR=DarkOrange] For Each Row As DataRow In tblCustomer.Rows  
    41.             Lookup.Add("id", "CustomerName")
    42.             cboCustomer.Items.Add("CustomerName")
    43.         Next[/COLOR]
    44.   '--This is the area, the error is occurred>
    45.     End Sub

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

    Re: [02/03]Add values to a hash table

    You don't need the Hashtable at all. Just use a DataAdapter to populate your DataTable directly. That way you don't have to worry about building the schema, reading the query results one record at a time or adding the records to the table. That whole block of code reduces to a few lines:
    VB Code:
    1. Dim dt As New DataTable
    2. Dim da As New SqlDataAdapter("query here", myConnection)
    3.  
    4. da.Fill(dt)
    5. cboCustomer.DisplayMember = "Name"
    6. cboCustomer.ValueMember = "DCLink"
    7. cboCustomer.DataSource = dt
    That's it. When the user selects a Name value from the list you get the corresponding DCLink value from the ComboBox's SelectedValue property.

    Also, can I recommend some consistency in your naming conventions? You're using Hungarian notation in some places, e.g. tbl and cbo, but not others, plus you're starting some variable names with lower case letters and others with upper case. Consistency is very important when writing code or it simply gets confusing. I strongly suggest that you chooses Hungarian notation or not and camel or Pascal casing and stick to it. The .NET convention is to NOT use Hungarian notation at all and to use camel casing (first letter lower case) for variables and Pascal casing (first letter upper case) for just about everything else. Unless you have a valid reason for doing otherwise I suggest that you choose that and stick to it.
    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

  3. #3

    Thread Starter
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Re: [02/03]Add values to a hash table

    Quote Originally Posted by jmcilhinney
    You don't need the Hashtable at all. Just use a DataAdapter to populate your DataTable directly.
    You are correct but what I want is to try with a Hashtable example. Any way, Thank you very much for coming my help.

    Also, can I recommend some consistency in your naming conventions? You're using Hungarian notation in some places, e.g. tbl and cbo, but not others, plus you're starting some variable names with lower case letters and others with upper case. Consistency is very important when writing code or it simply gets confusing. I strongly suggest that you chooses Hungarian notation or not and camel or Pascal casing and stick to it. The .NET convention is to NOT use Hungarian notation at all and to use camel casing (first letter lower case) for variables and Pascal casing (first letter upper case) for just about everything else. Unless you have a valid reason for doing otherwise I suggest that you choose that and stick to it.
    Thank you for your suggession and I appreciate it. I'm sorry to put a code with various notations within a single procedure. That was happen just because I was experimenting by putting codes here and there. I'm sorry , and thank you again

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