Question

I have seven fields that need to be populated in seven text boxes. The data is coming from a SQL Compact DB...

Here's my code so far, but I'm stuck. What do I need to do to populate the text boxes on Form Load... thanks much.

Woody

private void mcContactSubmit_Load(object sender, EventArgs e)
{
    // Setup our SQL connection.
    SqlCeConnection dataSource = new SqlCeConnection(
                 @"Data Source=|DataDirectory|\..\..\ContactInformation.sdf;
               Persist Security Info=False");
        SqlCeDataReader myReader = null;

    // Create our command text.
    string sqlQuery = String.Format(@"SELECT TOP (1) FirstName, LastName, Title, 
    Department, Category, Phone, Comments FROM ContactInformation 
    ORDER BY FirstName DESC");

    // Open the SQL connection.
    dataSource.Open();

    SqlCeCommand myCommand = new SqlCeCommand(sqlQuery, dataSource);
    myReader = myCommand.ExecuteReader();
}
Was it helpful?

Solution

You can either use the index or the column name to get the actual data, as follows:

myReader = cmd.ExecuteReader();

// Run through the results
while (myReader.Read())
{
    string fname = myReader.GetString(0);

    // or alternatively:

    string fname2 = myReader["FirstName"];

    // Either of these should work
}

After which, it's simple assignment to the TextBox. Otherwise you could also directly insert the Data into the TextBox, but rather not as validation should be done before this in most cases.

If you need more help, have a look here:

MSDN - SqlCeDataReader

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top