Solved

Getting confirmation from the database when data is saved

Posted on 2016-09-06
5
77 Views
Last Modified: 2016-09-06
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
Comment
Question by:Adam Trask
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:pritaeas
ID: 41786180
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
 

Author Comment

by:Adam Trask
ID: 41786198
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41786226
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
 

Author Comment

by:Adam Trask
ID: 41786294
Thank you
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41786309
Not a problem Adam, glad to help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question