Previous Topic

Next Topic

How a Profile Is Changed

Clicking the Change button in the application triggers the execution of the profileChangeButton_Click event handler.

Note: This example uses the ingresUpdateCommand6, which was created when the ingresDataAdapter6 was created.

The queries used are as follows:

UPDATE With Image

UPDATE user_profile

SET up_last = ?, up_first = ?, up_email = ?, up_airport = ?, up_image = ? WHERE up_email = ?

UPDATE Without Image

UPDATE user_profile

SET up_last = ?, up_first = ?, up_email = ?, up_airport = ? WHERE up_email = ?

You can write this query using the Query Designer for the IngresCommand by changing the CommandText property, or assign them directly to the IngresCommand CommandText property as a string.

The UPDATE performs the following tasks:

Use the updated DataSet for updating the user_profile table.

Each data row maintains a version of its changes after it is updated, but before the AcceptChanges method is called.

The DataSet values are used as parameters in the UPDATE query and each parameter is associated with the respective column.

The values from the previous version of the row are used as the parameters to the WHERE clause.

Using the DataSet, the UPDATE can be run.

private int ProfileChange(ProfileDataSet dataSet, String lastName,

    String firstName, String email, String airport, String photoFile)

{

    int resAffected = 0;

try

    {

        // Clear the command parameter list

        ingresDataAdapter6.UpdateCommand.Parameters.Clear();

        // Associate the data row columns with the data set columns.

        ingresDataAdapter6.UpdateCommand.Parameters.Add("@up_last",

            Ingres.Client.IngresType.NVarChar, "up_last");

        ingresDataAdapter6.UpdateCommand.Parameters.Add("@up_first",

            Ingres.Client.IngresType.NVarChar, "up_first");

        ingresDataAdapter6.UpdateCommand.Parameters.Add("@up_email",

            Ingres.Client.IngresType.NVarChar, "up_email");

        ingresDataAdapter6.UpdateCommand.Parameters.Add("@up_airport",

            Ingres.Client.IngresType.NChar, "up_airport");

        // If a path to an image is specified convert it into a Byte

        // array value and assign it to the up_image column.

        // If there is no path do not specify up_image as a parameter.

        if (photoFile.Length > 0)

        {

            MemoryStream stream = new MemoryStream();

            Image photo = System.Drawing.Image.FromFile(photoFile);

            DataTable dataTable = dataSet.Tables["user_profile"];

            photo.Save(stream, photo.RawFormat);

            ingresDataAdapter6.UpdateCommand.Parameters.Add("@up_image",

                Ingres.Client.IngresType.LongVarBinary, "up_image");

            dataTable.Rows[0]["up_image"] = stream.ToArray();

            stream.Close();

            ingresDataAdapter6.UpdateCommand.CommandText =

                String.Format(rm.GetString("ProfileUpdateQuery"),

                    rm.GetString("ImageAttribute"));

        }

        else

        {

            ingresDataAdapter6.UpdateCommand.CommandText =

                String.Format(rm.GetString("ProfileUpdateQuery"), "");

        }

        // Specify the previous value of email for the row as the

        // identity of the row for update in the WHERE clause.

        ingresDataAdapter6.UpdateCommand.Parameters.Add("@up_email",

            IngresType.NChar, "up_email").SourceVersion =

            DataRowVersion.Original;

        // Assign the values into the data set table.

        dataSet.Tables["user_profile"].Rows[0]["up_last"] = lastName;

        dataSet.Tables["user_profile"].Rows[0]["up_first"] = firstName;

        dataSet.Tables["user_profile"].Rows[0]["up_email"] = email;

        dataSet.Tables["user_profile"].Rows[0]["up_airport"] = airport;

        // Perform the update.

        resAffected = ingresDataAdapter6.Update(dataSet);

    }

    catch (IngresException ex)

    {

        DialogResult result = DisplayError(ex);

    }

    return (resAffected);

}


© 2006 Ingres Corporation. All rights reserved.