Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by PlausiblyDamp
Posted

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?

Posted

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?

Posted

[csharp]

ArrayList row in results.Rows

[/csharp]

 

Doesn't this sentence mean

 

For every results.Rows

ArrayList row = results.Rows

 

?? :confused:

  • Administrators
Posted

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();

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...