Solved

Getting confirmation from the database when data is saved

Posted on 2016-09-06
5
99 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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 63

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

726 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