The online racing simulator
Working with .sdf files in C#
Hi everyone and happy new year to all to who I didn't say it.

So: I am making a cruise server from long time ago(Actually modding the one from the forum). I learned C# thanks to that and nothing else. But recently I had the need to transfer all the info from .txt files to a .sdf database for the local files and I am also having a remote database but this isn't so important right now.

What I am having trouble in is that I can't get the code to work for the SQL SELECT command. There is some code:


const string localDatabasePath = @"FOLDER";
const string localDatabaseName = "FILE.sdf";
const string localDatabasePass = "WhyDoYouCare";
const string localDatabaseUsersTable = "userstable";
static public int doesntMatter(string Username)
{
try
{
SqlCeConnection localDatabaseConn = new SqlCeConnection("Data Source = " + localDatabasePath + "\\" + localDatabaseName + "; Password=" + localDatabasePass);
localDatabaseConn.Open();
SqlCeCommand localDatabaseCmd = localDatabaseConn.CreateCommand();
localDatabaseCmd.CommandText = "SELECT something FROM " + localDatabaseUsersTable + " WHERE username='" + Username + "'";
localDatabaseCmd.ExecuteNonQuery();
SqlCeDataReader localDatabaseRdr = localDatabaseCmd.ExecuteReader();
while (localDatabaseRdr.Read())
{
localDatabaseConn.Close();
[B]return int.Parse(localDatabaseRdr.GetInt64(0).ToString());[/B] line 197
}
catch (Exception E)
{
MessageBox.Show(Username + " - info - Message: " + E.Message);
MessageBox.Show("StackTrace: " + E.StackTrace);
return -2;
}

What I get from this is the 2 messageboxes. There are the messages:
MessageBox1: "SomeUsername - info - Message: No data exists for the row/column."

MessageBox2: "StackTrace: at System.Data.SqlServerCe.SqlCeDataReader.IsDBNull(Int32.Ordinal)
at System.Data.SqlServerCe.SqlCeDataReader.GetInt64(Int32.Ordinal)
at blablabla.blablabla.doesntMatter(String Username) in Path\To\The\Class\File.cs:line 197


I will be thankfull for any replies. Hope I will get this sorted out asap.

Edit: I'm just stupid... I was trying to close the connection before I get the result.

FGED GREDG RDFGDR GSFDG