System::String ^name_string;
SqlCeConnection ^sqlConnection = gcnew SqlCeConnection("Data Source=Northwind.sdf");
sqlConnection->Open();

SqlCeCommand comSelect = gcnew SqlCeCommand("SELECT * FROM tablo WHERE id = 33 ", sqlConnection);
SqlCeDataReader ^reader = comSelect->ExecuteReader();  
name_string = reader["name"]->ToString();  

sqlConnection->Close();

What's the mistake on the code above ?
:?:

Dear posters,

It's tricky.. DataReader works fine in the following example :

System::String ^name_string;
SQLiteConnection ^connection = gcnew SQLiteConnection();			  
connection->ConnectionString = "Data Source = database";
connection->Open();
SQLiteCommand^ command = connection->CreateCommand();  
command->CommandText = " Select name from test where id_int = 33 " ;     
SQLiteDataReader ^dataReader1 = command->ExecuteReader();
name_string = dataReader1["name"]->ToString(); 
connection->Close();

HOWEVER it gives error in the following case from the 5th and 6th lines :

System::String ^name_string;
SqlCeConnection ^sqlConnection = gcnew SqlCeConnection("Data Source= database");
sqlConnection->Open();
SqlCeCommand comSelect = gcnew SqlCeCommand("Select name from test where id_int = 33 ", sqlConnection);
SqlCeDataReader ^reader = comSelect->ExecuteReader();  
name_string = reader["name"]->ToString();  
sqlConnection->Close();

Please help.. !!

Actually the answer is obvious :

I remember this just few minutes before.. While reading data from a SqlServer database unlike SQLite databases we should use the following structure :

while (reader->Read())
{
   name_string = reader["name"]->ToString();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.