retrieve value from database and assign it in to string variable C#

Nick picture Nick · Nov 21, 2012 · Viewed 51.5k times · Source

I want to retrieve value from table. Please guys help me. The thing is I want to get that value and check it with a string value.

this code is not working:

SqlCeCommand cmd1 = new SqlCeCommand("SELECT username FROM tmpusername WHERE _id=1", connection);

SqlCeDataReader usernameRdr = null;

usernameRdr = cmd1.ExecuteReader();

while (usernameRdr.Read()){
    string username11 = usernameRdr["username11"].ToString(); 
}

Answer

Habib picture Habib · Nov 21, 2012

use username for accessing column.

string username11 = usernameRdr["username"].ToString(); 

Your select command is specifying Select username and later you are accessing it using username11. That is why you are not getting the value, instead you should be getting an exception.