|
-
Nov 19th, 2006, 11:58 PM
#1
Thread Starter
Hyperactive Member
[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:
Private Sub PopulateCustomer()
'//This procedure retriews all customers
Dim SelectProducts = New SqlClient.SqlCommand
Dim myReader As SqlDataReader
MyConnection = New SqlConnection(MyCONNECTIONSTRING)
SelectProducts = New SqlClient.SqlCommand("SELECT Name, DCLink FROM Client Order By Name", MyConnection)
MyConnection.Open()
myReader = SelectProducts.ExecuteReader()
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.Int16")
myDataColumn.ColumnName = "id"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
tblCustomer.Columns.Add(myDataColumn)
'==>>>
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "CustomerName"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "Customer Name"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
tblCustomer.Columns.Add(myDataColumn)
If myReader.HasRows Then
Do While myReader.Read()
myDataRow = tblCustomer.NewRow()
myDataRow("id") = myReader.GetValue(1)
myDataRow("CustomerName") = myReader.GetString(0)
tblCustomer.Rows.Add(myDataRow)
Loop
Else
MsgBox("Customer Database is empty", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, "Load Customer")
End If
MyConnection.Close()
MyConnection = Nothing
myReader = Nothing
Dim Lookup As New Hashtable
<'--This is the area the error is occurred
[COLOR=DarkOrange] For Each Row As DataRow In tblCustomer.Rows
Lookup.Add("id", "CustomerName")
cboCustomer.Items.Add("CustomerName")
Next[/COLOR]
'--This is the area, the error is occurred>
End Sub
-
Nov 20th, 2006, 01:44 AM
#2
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:
Dim dt As New DataTable
Dim da As New SqlDataAdapter("query here", myConnection)
da.Fill(dt)
cboCustomer.DisplayMember = "Name"
cboCustomer.ValueMember = "DCLink"
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.
-
Nov 20th, 2006, 05:42 AM
#3
Thread Starter
Hyperactive Member
Re: [02/03]Add values to a hash table
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|