I would not reccomendrecommend putting your dal code in a try/catchtry-catch block. At least you should be logging or doing something about it , currently you're just returning false (or no value). If there is an exception occuredoccur, let it occurhappen, or put a try/catchtry-catch on the function that calls it. Remember that exceptions vary (did the db server stop? was the sql malformed? these all are different cases.)
Using "using"using is a better practice for all IDisposableIDisposable objects, thus embrace it. If you run visual studio'sVisual Studio's code analysis tools (ultimate editionUltimate Edition only iI guess) it would also tell you to do so.
myMy version of your code is similar to Jeff's (which I wanted to vote up but didn't have enough rep yet) with an exception, sqlcommandSqlCommand is also disposable and can/should be used with using block.
public bool spInsertFeedback(string name, string subject, string message)
{
int rows = 0;
using (var connection = new SqlConnection(cnnString))
{
connection.Open();
using (var command = new SqlCommand("[dbo].[spInsertFeedback]", connection))
{
command.CommandType = CommandType.StoredProcedure;
// params
SqlParameter messageName = new SqlParameter("@name", SqlDbType.VarChar);
messageName.Value = name;
SqlParameter messageSubject = new SqlParameter("@subject", SqlDbType.VarChar);
messageSubject.Value = subject;
SqlParameter messageText = new SqlParameter("@message", SqlDbType.VarChar);
messageText.Value = message;
// add params
command.Parameters.Add(messageName);
command.Parameters.Add(messageSubject);
command.Parameters.Add(messageText);
rows = command.ExecuteNonQuery();
}
}
return (rows != 0);
}
Also I would suggest you to use "connectionString"connectionString instead of "cnnString"cnnString, which is not a proper naming.