Um.
I see nothing that involves your DataTable outside the method in which you load it (and you'd be better off doing that with a DataAdapter rather than a DataReader - it involves a lot fewer round trips to the DB), so quite what you want to do is not obvious.
But ... two things do spring to mind: Why catch all exceptions if all you are going to do is re-throw them? That doesn't really help at all, it just complicates things. You can just have the
try...finally
block and skip the
catch
if that's what you are trying to do, or even better use a
using
block to Close and Dispose the connection (and another for the Command) instead of the try block.
using (SqlConnection conn = new SqlConnection(strConnect))
{
using (SqlCommand cmd = new SqlCommand(spName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(sqlParams.ToArray());
using (DataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
Secondly, if it's a
double
then it'll fail the
int.TryParse
test - the presence of a '.' in the text string will mean it isn't an integer, so the outer TryParse will fail and it'll never get to the Double.Parse. Use Double.TryParse as the one and only test, and use the value it converted:
private void btnHitung_Click(object sender, EventArgs e)
{
double value;
if (double.TryParse(txtBasoSU.Text, out value))
{
TotalPriceOfBasoSU = BasoSUCost * value;
...
}
}