[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 119
  • Last Modified:

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.
0
Adam Trask
Asked:
Adam Trask
  • 2
  • 2
1 Solution
 
pritaeasSoftware EngineerCommented:
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.
0
 
Adam TraskAuthor Commented:
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();
            }
        }
0
 
Fernando SotoCommented:
Hi Adam;

From Microsoft Documentation,
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.
So if you capture the returned value from that call and if that value is greater then zero then that many rows were updated.
int rowsEffected = comm.ExecuteNonQuery();

Open in new window

From that function return false if less then 1 and true otherwise.
1
 
Adam TraskAuthor Commented:
Thank you
0
 
Fernando SotoCommented:
Not a problem Adam, glad to help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now