Solved

Getting confirmation from the database when data is saved

Posted on 2016-09-06
5
62 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now