Is this a good practice and is there anything I need to look out for? I was binding repeater to an asp:SqlDataSource. Primary reason for doing this is to gain more control of the SqlCommand (e.g. CommandTimeout).
Example:
try
{
SqlDataReader MyReader = GetSomeResultsFromSqlCommand();
MyRepeater.DataSource = MyReader;
MyRepeater.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
MyReader.Close();
}
This is a perfectly fine thing to do - let me offer a more succinct syntax that will provide the same results and offer the same fail-safe cleanup abilities:
using (SqlDataReader MyReader = GetSomeResultsFromSqlCommand())
{
MyRepeater.DataSource = MyReader;
MyRepeater.DataBind();
}
Your code seems to be OK as written and does not necessarily appear to violate any best practices. You could however redactor the code a bit and utilize the "using" statement instead of the try, catch, finally...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With