hi,
here m suffering from retrieivng code...i m saving spinner value in
database with ur dictionary collection...now i wnt to populate the spinner
with the coursename based on database corresponding value...
i wrote some code to populate the spinner (retreive code )but it is not
working...
for all the times my task is half done...plz help me to retrieve code:
in previous spinner ex i did the retrieve with following code using
arrayadapter...
while (sdr.Read())
{
string str = Convert.ToString(sdr["SCOURSE"]);
Spcourse.SetSelection(aas.GetPosition(str));// here aas
is ArrayAdapter<String> aas = new
ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerDropDownItem);
-----------------------------------------
i fill spinner using arrayadapter like this
Spcourse = FindViewById<Spinner>(Resource.Id.Spcourse);
aas = new
ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerDropDownItem);
Spcourse.Adapter = aas;
aas.Add(String.Empty);
aas.Add("MCA");
aas.Add("MBA");
aas.Add("M.Sc");
and in saving code i saved like this
string spcourse = Spcourse.SelectedItem.ToString();
sqlc.Parameters.Add(new SqliteParameter("@SCOURSE", spcourse));
-------------------------------------
its working fine for saving and retreiving because it contains only
value(coursename)...
but now coming into my current example i filled spinner with dictionary
contains key and value ....here saving the value corresponding to course is
saving...now when i enter student no and click on retreive button the
spinner should set with the course based on database value....i tried some
retreive code but its not working..
void retreivedict_Click(object sender, EventArgs e) {
string DatabaseName = "sdata.db3";
string documents =
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string db = Path.Combine(documents, DatabaseName);
var conn = new SqliteConnection("Data Source=" + db);
var strSql = "select SNO,SCOURSE,SADD from Stdetails where
SNO=@SNO";
var cmd = new SqliteCommand(strSql, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqliteParameter("@SNO",studentno.Text));
try
{
conn.Open();
SqliteDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
//string str = Convert.ToString(sdr["SCOURSE"]);
int spinintval = Convert.ToInt32(sdr["SCOURSE"]);
if (retcourse.ContainsKey(spinintval)) //here i
wrote like this for retreiving
{ studentcourse.SetSelection(adapter1.GetPosition(spinintval));
}//here is my retreive code which is not working
studentaddr.Text = Convert.ToString(sdr["SADD"]);
// Toast.MakeText(this, str, ToastLength.Long).Show();
}
}
here is my saving code for spinner with dictionary collection:
public class SaveSpinnerValueDictionary : Activity
{
Spinner studentcourse;
EditText studentno, studentaddr;
Button savedict, retreivedict;
Dictionary<string, int> course;
string spinval;
ArrayAdapter<string> adapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.SaveSpinnerValueDictionary);
// Create your application here
studentcourse =
FindViewById<Spinner>(Resource.Id.studentcourse);
studentno = FindViewById<EditText>(Resource.Id.studentno);
studentaddr = FindViewById<EditText>(Resource.Id.studentaddr);
savedict = FindViewById<Button>(Resource.Id.savedictionary);
retreivedict =
FindViewById<Button>(Resource.Id.retreivedictionary);
savedict.Click+=new EventHandler(savedict_Click);
retreivedict.Click+=new EventHandler(retreivedict_Click);
course = new Dictionary<string, int>();
course.Add("MCA", 101);
course.Add("MBA", 202);
course.Add("MSc", 303);
course.Add("Mtech", 404);
adapter = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleSpinnerDropDownItem);
foreach(string k in course.Keys)
adapter.Add(k);
studentcourse.Adapter = adapter;
}
void savedict_Click(object sender, EventArgs e) {
string spcourse = studentcourse.SelectedItem.ToString();
int outValue = 0;
if (course.TryGetValue(spcourse, out outValue)) {
spinval = outValue.ToString();
}
string DatabaseName = "sdata.db3";
string documents =
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string db = Path.Combine(documents, DatabaseName);
bool exists = File.Exists(db);
if (!exists)
{
SqliteConnection.CreateFile(db);
}
var conn = new SqliteConnection("Data Source=" + db);
SqliteCommand sqlitecmd = new SqliteCommand();
sqlitecmd.Connection = conn;
string cmd = "CREATE TABLE IF NOT EXISTS Stdetails(SNO
INT,SCOURSE VARCHAR(50),SADD VARCHAR(50))";
sqlitecmd.CommandType = CommandType.Text;
sqlitecmd.CommandText = cmd;
conn.Open();
sqlitecmd.ExecuteNonQuery();
conn.Close();
SqliteCommand sqlc = new SqliteCommand();
sqlc.Connection = conn;
conn.Open();
string strSql = "INSERT INTO Stdetails (SNO, SCOURSE, SADD)
VALUES (@SNO, @SCOURSE, @SADD)";
sqlc.CommandText = strSql;
sqlc.CommandType = CommandType.Text;
sqlc.Parameters.Add(new SqliteParameter("@SNO",studentno.Text));
sqlc.Parameters.Add(new SqliteParameter("@SCOURSE", spinval));
sqlc.Parameters.Add(new
SqliteParameter("@SADD",studentaddr.Text));
sqlc.ExecuteNonQuery();
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
conn.Dispose();
Toast.MakeText(this, "data inserted successfully...!",
ToastLength.Long).Show();
studentno.Text = "";
studentaddr.Text = "";
}
now i saved data like this in database:
sno scourse saddress
1 101 UK
2 303 INDIA
3 404 USA
when i enter 2 in sno edittext and click on retreive button the spinner
should populate with "MSc"...
plz tell indetail...actually my task is half completing all time ..plz help
me to complete this task...
--
View this message in context:
http://mono-for-android.1047100.n5.nabble.com/Enhancement-code-for-Spinner-tp5712950p5712996.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
[email protected]
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid