Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
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
Updated 3-Apr-12 6:58am
v5

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.
 
Share this answer
 
Comments
ProEnggSoft 3-Apr-12 13:39pm    
Good answer. +5
JacoBosch 4-Apr-12 12:16pm    
Thanx Clifford. I changed my Query and I also changed my method.
dicVersion.containsValue("VariableValue); will give you the key for "VariableValue".
 
Share this answer
 
v2
Comments
JacoBosch 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 3-Apr-12 12:10pm    
Ah yes - this link provides a solution that should help you out.
Thanx for everybody's help. I changed my Short procedure that it must only return one value, and I used ExecuteScalar.

C#
public string SelectdicVersion()
        {
            
SqlDatabase sqldb = new SqlDatabase(connectionstring);
string reader = sqldb.ExecuteScalar("pSEL_Version", new object[] { }).ToString();
return reader;
         }
 
Share this answer
 
C#
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.
 
Share this answer
 
v4
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.
C#
dicVersion.Add(reader["VariableValue"].ToString(),
(reader["VariableDescription"].ToString()));

may be replaced with
C#
dicVersion.Add(reader["VariableDescription"].ToString(),
(reader["VariableValue"].ToString()));

To retrieve the VariableValue corresponding to a given VariableDescription
C#
//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"];
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900