views:

516

answers:

1

Hi all...

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();
}
+2  A: 

Hi there,

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

Kyle Rozendo
thanks buddy, easy.
Woody
Great stuff, glad it helped.
Kyle Rozendo
Doh. I spoke too soon. The text boxes aren't being populated... here's what I'm seeing in debug mode:Message "SQL Server Compact does not support calls to HasRows property if the underlying cursor is not scrollable."What the heck does that mean?! LOL...
Woody
I've tried testing for rows right after myReader = cmd. ..., however, the error is thrown right before it. If I try to test it any earlier in the code, I get an Object is not set to a reference of an object error...
Woody
You'll just have to check the DataSet before, so do a bool whatever = myReader.Read(); and if it's false, don't fill the text boxes. Im not sure what that error means and am following Marc Gravell's advice with checking the Reader.
Kyle Rozendo