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.
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.
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=TARI Q_MEDIA;Da tabase=Cle aningTime; integrated security=True");
// create the INSERT command
string strQuery = "INSERT INTO Units (UnitName,OwnerName,DueDat e,Comments ) VALUES (@UnitName,@OwnerName,@Due Date,@Comm ents);";
comm = new SqlCommand(strQuery, con);
comm.Parameters.AddWithVal ue("@DueDa te", System.Data.SqlDbType.Date );//Date
comm.Parameters["@DueDate" ].Value = dD.ToShortDateString();
comm.Parameters.AddWithVal ue("@UnitN ame", System.Data.SqlDbType.NVar Char);// Name
comm.Parameters["@UnitName "].Value = sUnitName;
comm.Parameters.AddWithVal ue("@Owner Name", System.Data.SqlDbType.NVar Char);// Subject
comm.Parameters["@OwnerNam e"].Value = sOwnerName;
comm.Parameters.AddWithVal ue("@Comme nts", System.Data.SqlDbType.NVar Char);// remarks
comm.Parameters["@Comments "].Value = sComments;
try
{
con.Open();
// Execute the command
comm.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
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=TARI
// create the INSERT command
string strQuery = "INSERT INTO Units (UnitName,OwnerName,DueDat
comm = new SqlCommand(strQuery, con);
comm.Parameters.AddWithVal
comm.Parameters["@DueDate"
comm.Parameters.AddWithVal
comm.Parameters["@UnitName
comm.Parameters.AddWithVal
comm.Parameters["@OwnerNam
comm.Parameters.AddWithVal
comm.Parameters["@Comments
try
{
con.Open();
// Execute the command
comm.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
Not a problem Adam, glad to help.
Another method is to use exceptions.