Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Hashtable write to listbox???

Posted on 2004-08-27
6
Medium Priority
?
3,596 Views
Last Modified: 2008-02-20
I'm deciding whether or not my sql is writing to my hashtable.

Q. Does this code look correct???

string SQL1 = "SELECT col_1, col_2 FROM myTable";
SqlConnection cn1 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da1 = new SqlDataAdapter(SQL1,cn1);
cn1.Open();
SqlDataReader SqlReader1 = da1.SelectCommand.ExecuteReader();

hash = new Hashtable();      
            
listBox1.Items.Clear();

while(SqlReader1.Read())
      {                  
            str_col_1 = SqlReader1.GetString(0);
            str_col_2 = SqlReader1.GetString(1);

            hash.Add(str_col_1.Trim(), str_col_2.Trim());
                  {      
                        string key = hash[0].ToString();
                        string val = hash [1].ToString();
                        listBox1.Items.Add(key.ToString() + "\t" + val.ToString());
                  }

0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11921303
not really I am assuming this is actually in later code and just copy / pasted here...

               {    
                    string key = hash[0].ToString();
                    string val = hash [1].ToString();
                    listBox1.Items.Add(key.ToString() + "\t" + val.ToString());
               }
try this instead.

foreach (DictionaryEntry de in myHashtable) {
    listBox1.Items.Add(de.Key.ToString() + "\t" + de,Value.ToString());
}
0
 
LVL 4

Expert Comment

by:MrGhost
ID: 11921345
Why don't you use DataSource to fill your ListBox why do you need hastable!
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11921466
Well, I definitly need the 'key - value' pair concept. I've read that a SortedList is very similar to a hashtable. The hashtable was suggested to me and that's why I began using it, although I can see it may be and old inefficient way of doing things, so I'm open to suggestions.

Q. How can you make the DataSource work with with my SQL command?  

Q. How can you make the DataSource writes it's stored information out to a listbox?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:MrGhost
ID: 11921562
to get you started:

                        OleDbConnection conn = new OleDbConnection("<connection string>");
                  OleDbDataAdapter da = new OleDbDataAdapter("SELECT col_1, col_2 FROM myTable", conn);
                  DataSet ds = new DataSet();
                  da.Fill(ds);
                  listBox1.DataSource = ds.Tables[0].DefaultView;
                  also check listBox1.DisplayMember and listbox1.ValueMember
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11921812
are you doing anything else with the hashtable ? like using it to search later ?
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11922556
This is the raw code. I left out the 'catch' and 'finally' to conserve space. Basically I'm quickly loading a Hashtable from an active (threaded) SQL DataTable for later key-value pair retrieval by the 'LoadListView' method. I like the idea of a DataSet better than a hashtable, but not sure how to get the results I need. I was reader (splitting) to the hashtable from a Text.txt file with a slightly different coding with great success. That's why I tried to convert to reading an SQL DataTable, thought it would be easy....

Viewing the final results in a the ListView is my ultimate goal.

The ListBox is to meerly 'peek' inside the hashtable to asure it actually contains data.


private void LoadHashtable()

string SQL1 = "SELECT col_1, col_2 FROM myTable";
SqlConnection cn1 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da1 = new SqlDataAdapter(SQL1,cn1);
cn1.Open();
SqlDataReader SqlReader1 = da1.SelectCommand.ExecuteReader();

hash = new Hashtable();    
listBox1.Items.Clear();

while(SqlReader1.Read())
     {              
          str_col_1 = SqlReader1.GetString(0);
          str_col_2 = SqlReader1.GetString(1);

          hash.Add(str_col_1.Trim(), str_col_2.Trim());
       }
          listBox1.Items.Add(key.ToString() + "\t" + val.ToString());


private void ShowListView1()

/// Various SQL commands that work are here, I'm conserving space.......

listView1.Items.Clear();
listView1.BeginUpdate();

{
try
{
while(SqlReader2.Read())
{
string str_col_1 = SqlReader2.GetString(0);
string str_col_2 = SqlReader2.GetString(1);
                        
if (hash.ContainsKey(str_col_1))
{
strMatch = hash[str_col_9].ToString();      
}
else
{
strMatch = "No Match Found";
}

ListViewItem item1 = new ListViewItem(str_col_1.ToString());
item1.SubItems.Add(strMatch.ToString());
listView1.Items.Add(item1);
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question