Link to home
Start Free TrialLog in
Avatar of Adam Trask
Adam Trask

asked on

Getting confirmation from the database when data is saved

Hi,

I am relatively new to C# and in the process of building an application using a 3- layered structure (User interface, Business Logic  and Data layer).
One of the forms in the user interface contains a Save button which lets the user save the data on the form in an SQL Server table.
The button sends its code to the business layer and the business layer communicates the data to the Data layer which uses a method to save the data.

All I need is some sort of a message that tells the user the data was saved successfully. Thank you.
Avatar of pritaeas
pritaeas
Flag of Netherlands image

Depending on how you insert/update your data, your data access methods will return true on success, or false on failure. You can pass that as function result back through the layers.

Another method is to use exceptions.
Avatar of Adam Trask
Adam Trask

ASKER

Thanks,

I think my problem is that I don't know how to capture the true or false returned by the save method.
Here is the code for my save method:


 public void SaveData(DateTime dD, string sUnitName, string sOwnerName, string sComments)
        {
            SqlConnection con = default(SqlConnection);
            SqlCommand comm = default(SqlCommand);
            con = new SqlConnection("Server=TARIQ_MEDIA;Database=CleaningTime;integrated security=True");
            // create the INSERT command
            string strQuery = "INSERT INTO Units (UnitName,OwnerName,DueDate,Comments) VALUES (@UnitName,@OwnerName,@DueDate,@Comments);";
            comm = new SqlCommand(strQuery, con);
            comm.Parameters.AddWithValue("@DueDate", System.Data.SqlDbType.Date);//Date
            comm.Parameters["@DueDate"].Value = dD.ToShortDateString();
            comm.Parameters.AddWithValue("@UnitName", System.Data.SqlDbType.NVarChar);// Name
            comm.Parameters["@UnitName"].Value = sUnitName;
            comm.Parameters.AddWithValue("@OwnerName", System.Data.SqlDbType.NVarChar);// Subject
            comm.Parameters["@OwnerName"].Value = sOwnerName;
            comm.Parameters.AddWithValue("@Comments", System.Data.SqlDbType.NVarChar);// remarks
            comm.Parameters["@Comments"].Value = sComments;
            try
            {
                con.Open();
                // Execute the command
                comm.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you
Not a problem Adam, glad to help.