Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Good day
 
I created a dictionary. The dictionary values is coming from a stored procedure.
 

The following values are stored inside my dictionary:
 
VariableDescription           VariableValue
 
Version                       1.0.0.0 
 
The dictionary key is VariableValue and the value is VariableDescription.
 
I want to return the VariableValue, "1.0.0.0" and not the whole dictionary. Here is my code that I have written:
 
public Dictionary<string, string> SelectdicVersion()
{       
Dictionary<string, string> dicVersion = new Dictionary<string, string>();
    {
     SqlDatabase sqldb = new SqlDatabase(connectionstring);
     IDataReader reader = sqldb.ExecuteReader("pSEL_Version", new object[] { });
           while (reader.Read())
          {
dicVersion.Add(reader["VariableValue"].ToString(), 
(reader["VariableDescription"].ToString()));
sqldb = null;
            }
 
       }
 return dicVersion;
 }
 
At the moment it is returning the whole dictionary. What must I change so that "1.0.0.0" can be returned?
Posted 3-Apr-12 5:46am
JacoBosch1.4K
Edited 3-Apr-12 7:58am
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

It appears that you are using a stored proceedure. You really do not need the dictionary when reading the information if all you are looking for is the Version (reader["VariableValue"].ToString()). You just have to check the key for each record read and abort when you find the version. When you find "Version", return the reader["VariableDescription"].ToString()). Ideally you should create the stored procedure to allow selection of only a single key, not the entire dictionary.
  Permalink  
Comments
ProEnggSoft at 3-Apr-12 13:39pm
   
Good answer. +5
JacoBosch at 4-Apr-12 12:16pm
   
Thanx Clifford. I changed my Query and I also changed my method.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

dicVersion.containsValue("VariableValue); will give you the key for "VariableValue".
  Permalink  
v2
Comments
JacoBosch at 3-Apr-12 11:11am
   
I tried that, but I get a bool value, instead of the value that is inside the dictionary.
Abhinav S at 3-Apr-12 12:10pm
   
Ah yes - this link provides a solution that should help you out.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Thanx for everybody's help. I changed my Short procedure that it must only return one value, and I used ExecuteScalar.
 
public string SelectdicVersion()
        {
            
SqlDatabase sqldb = new SqlDatabase(connectionstring);
string reader = sqldb.ExecuteScalar("pSEL_Version", new object[] { }).ToString();
return reader;
         }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

foreach (KeyValuePair<string, string> kvp in dicVersion)
{
   string key = kvp.Key;
   string value = kvp.Value;
}
 
A Dictionary is basically a collection of KeyValuePair on which you can iterate.
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

From the question it appears that there is a VariableValue for each VariableDescription in the database, and a particular VariableValue is required to be selected from these values for a given VariableDescription.
If so, I think a Dictionary with VariableDescription as key and VariableValue as Value can be populated.
dicVersion.Add(reader["VariableValue"].ToString(),
(reader["VariableDescription"].ToString()));
may be replaced with
dicVersion.Add(reader["VariableDescription"].ToString(),
(reader["VariableValue"].ToString()));
To retrieve the VariableValue corresponding to a given VariableDescription
//First create a dictionary, say in Load event of a Form
Dictionary<string,> VariableDictionary =  SelectdicVersion();
 
//Get the VariableValue for VariableDescription "myDescription"
string myValue = VariableDictionary["myDescription"];
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 4 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100