Hi,

hi,its glad to tell i saved spinner value(courseNumbers) in database with ur
assistance code...but when i click on retreive button now i want to populate
that spinner with courses based on the value(courseNumber) in database based
on sno...
ex data in database:
sno    course    address
1000   303        uk
1001    404      india
1002    505      usa
here my requiremnt is when i give value in sno edittext and click on
retreive button the data should populate in spinner and edittext....for
example i enter 1000 in sno edittext and click on retreive button the
spinner should populate with Biology and  address edittext sholud populate
with uk....the spinner should set with coursename based on courseNumber in
database...

This is where the use of a dictionary object comes in handy. My original code constructed two Lists (an int and string) which contained the course names and course values. This was fine for my demonstration purpose, but now we're doing searches. Of course, you can do them on List objects and we'll use both so you can decide which works best.

A dictionary has the advantage of being a key/value system, I have a key (say "German") and this has an associated value (505). To set up the dictionary...

Dictionary <keyType, valueType> name = new Dictionary<keyType, valueType>();

where keyType is the type of object the key is (for example, string, int, double, class, List<T> etc) and valueType is the type of object the value is (string, double etc).

As with a List, you add to the dictionary using the .Add method, so if I have a Dictionary<string, int> foo, it would be

foo.Add("German", 505);

There are plenty of ways to get values out of the dictionary object, but possibly the most useful for you will be to use TryGetValue. This looks up a given key in the dictionary and returns a value.

To get a value from foo...

int outValue = 0;
if (foo.TryGetValue("German", out outValue))
Console.WriteLine("value found = {0}, outValue);
else
Console.WriteLine("key not found");

There is a very slight performance hit using a Dictionary; it is slightly slower than searching a list, but consider this. If I want to search a list for a string, I can do this

if (foo.Contains("German"))

but then I have to find where in the list "German", so the code becomes

int position = 0;
if (foo.Contains("German"))
{
position = foo.IndexOf("German");
Console.WriteLine("position of German = {0}", position;
}
else
Console.WriteLine("German not found");

In effect, there are two lookups. I've not done any sort of performance timing on using this "double lookup", but I'd expect it now to be slower than a dictionary.

                 conn.Open();
                 SqliteDataReader sdr = cmd.ExecuteReader();
                 while (sdr.Read())
                 {
                     string str = Convert.ToString(sdr["SCOURSE"]);

                          /* here have to write the code for population of
spinner ....here i have taken courseNumber in string variable str...based on
this value spinner should set with course...  */

Here then you would want to be using Dictionary<int, string>

the parameter to search though would need to be an int, so

int strToFind = Convert.ToInt32(sdr["SCOURSE"]);
then

if (myDictionary.ContainsKey(strToFind))
etaddspinner.Text = Convert.ToString(sdr["SADD"]);

However, as you're just drawing from the database rather than anything else, you can ignore the dictionary and use the list as before. Again though, you will need to be using an int to look up the course number, so the code would be

int strToFind = Convert.ToInt32(sdr["SCOURSE"]);
if (myCourseList.Contains(strToFind))
etaddspinner.Text = Convert.ToString(sdr["SADD"]);

At this point you may be wondering how to make this even more flexible. That's simple, follow this logic

1. Set up your container(s) (either List<> or Dictionary)
2. Read in from the database the available courses and course numbers into the containers
3. That's it

Once you've read in from the database to propagate the containers, you don't need to do it anymore. Database access slows things down big time!

Paul
--
"Space," it says, "is big. Really big. You just won't believe how vastly, hugely, mindbogglingly big it is. I mean, you may think it's a long way down the road to the chemist's, but that's just peanuts to space, listen..."
Hitch Hikers Guide to the Galaxy, a truly remarkable book!

_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to