Ionix Posted October 19, 2006 Posted October 19, 2006 (edited) I am currently using SQLite.NET Wrapper from http://www.phpguru.org/static/SQLite.NET.html I am currently facing a problem on retrieving the data. My codes are as below... SQLiteResultSet tempResult; ArrayList tempString; tempResult = sqlite.Execute("SELECT * FROM MARKET;"); tempString = tempResult.Rows; for(int i=0; i<tempString.Count; i++) { Debug.Print((String)tempString[i]); } I am trying to typecast the tempString which is an ArrayList to String. But receive this error "Unable to cast object of type 'System.Collections.ArrayList' to type 'System.String'." I am new to C#, please kindly teach me how to convert this? Thanks. Edited October 21, 2006 by PlausiblyDamp Quote
Ionix Posted October 20, 2006 Author Posted October 20, 2006 Is this forum thread, as "quiet" as this all the time? Quote
Nate Bross Posted October 20, 2006 Posted October 20, 2006 Try using the .ToString() method. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Ionix Posted October 21, 2006 Author Posted October 21, 2006 tried. ToString() will display "'System.Collections.ArrayList". I want the value, not the class. Quote
Nate Bross Posted October 21, 2006 Posted October 21, 2006 Just out of curisoty, have you tried tempString.Item[i].ToString() Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Ionix Posted October 21, 2006 Author Posted October 21, 2006 There is no Items[] for ArrayList.!!! Are you sure about the possibility? Quote
Administrators PlausiblyDamp Posted October 21, 2006 Administrators Posted October 21, 2006 tmpString[i].ToString() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Ionix Posted October 21, 2006 Author Posted October 21, 2006 I tried tempString.ToString() //It will print "System.Collections.ArrayList" (String)tempString //It will give error "System.InvalidCastException" So what should I do? tempString is an ArrayList and how do I display it's content? Shouldn't it be tempString? Quote
Administrators PlausiblyDamp Posted October 21, 2006 Administrators Posted October 21, 2006 tempString doesn't contain strings - only just looked at your original post, it looks like it contains an array of DataRow objects. Is that right? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Ionix Posted October 21, 2006 Author Posted October 21, 2006 using SQLite.NET; [..] private SQLiteClient db; [..] /// /// Load the database on form load. If the database does not /// exist the client library will create it for us. /// private void formMain_Load(object sender, System.EventArgs e) { db = new SQLiteClient(�test.db�); } /// /// Will execute the SQL command entered on the new database /// and display the result set. Create the items described, /// 1 text input for the statement (textSQL), 1 button (buttonExecSQL) /// and 1 multiline textbox for the output (textOutput) /// private void buttonExecSQL_Click(object sender, System.EventArgs e) { SQLiteResultSet results; try { results = db.Execute(textSQL.Text); textOutput.Text = �Executed. � + results.Rows.Count; foreach (ArrayList row in results.Rows) { textOutput.Text += row[0] + �\r\n�; } } catch (SQLiteException s) { textOutput.Text += �ERROR: � + s.ErrorCode + � � + s.Message + � in � + s.Source + �\r\n�; } } This code was copied from http://www.nik.com.au/archives/2005/05/03/using-sqlite-in-net/ And what that we are talking here is this portion foreach (ArrayList row in results.Rows) { textOutput.Text += row[0] + �\r\n�; } If ArrayList here does not contain any String then how does it textOutput.Text? I have to assume that tempString, actually contains some string inside... I mean what else can that be? Quote
Ionix Posted October 21, 2006 Author Posted October 21, 2006 [csharp] ArrayList row in results.Rows [/csharp] Doesn't this sentence mean For every results.Rows ArrayList row = results.Rows ?? :confused: Quote
Administrators PlausiblyDamp Posted October 21, 2006 Administrators Posted October 21, 2006 try something like tmpString[i][0].ToString() and see if that does anything. In their sample the variable row is an array. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Ionix Posted October 21, 2006 Author Posted October 21, 2006 Hi Tried your suggest. Result is... Error 1 Cannot apply indexing with [] to an expression of type 'object' Quote
Ionix Posted October 21, 2006 Author Posted October 21, 2006 Surprisingly base on the example from http://www.nik.com.au/archives/2005/05/03/using-sqlite-in-net/ [csharp] foreach (ArrayList row in results.Rows) { textOutput.Text += row[0] + �\r\n�; } [/csharp] actually works. but... a for loop in my code cannot work. Anyone can explain this??? it's confusing :confused: Quote
Administrators PlausiblyDamp Posted October 21, 2006 Administrators Posted October 21, 2006 tmpString contains an array but it is returned as an object, you will need to cast the result of tmpString to an Arraylist and then access those elements. Try something like ((ArrayList) tmpString[i])[0].ToString() //or more explicit ArrayList tmp = (ArrayList) tmpString[i]; string s = tmp[0].ToString(); Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Ionix Posted October 21, 2006 Author Posted October 21, 2006 Yes you are right finally it works. So there is a double return of ArrayList... no wander I am unable to convert it to string. Thanks. but the example by Nik, doesn't it only takes in 1 ArrayList row for each loop? how is it possible for that his example to work that way? Quote
FZelle Posted October 27, 2006 Posted October 27, 2006 Just a question, why do you use a Provider that actually ignores ADO.NET and insists on it's own methods? If you would use http://sourceforge.net/projects/adodotnetsqlite/ for a FW 1.1 prg and http://sourceforge.net/projects/sqlite-dotnet2/ for a FW 2.0 program you could use the std. mechanisms in ADO.NET. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.