public static double PaymentsvalueSum(int id) { string strconn = myproject.Properties.Settings.Default.SalariesConnectionString; OleDbConnection conn = new OleDbConnection(strconn); if (conn.State == ConnectionState.Closed) { conn.Open(); } OleDbCommand command = new OleDbCommand("select SUM(Payment_Value)from imprests_payments WHERE Imprest_ID=@id;", conn); command.Parameters.Add(new OleDbParameter("@id", id)); double sumvalue = (double)command.ExecuteScalar(); try { } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } return sumvalue; } when button_click event I write private void button1_Click(object sender, EventArgs e) { double val = ImprestsPaysMgr.PaymentsvalueSum(int.Parse(GridViewImprestsPays.CurrentRow.Cells["Imprest_ID"].Value.ToString())); textBox1.Text = val.ToString(); }
double sumvalue = (double)command.ExecuteScalar();
double sumvalue = 0; Double.TryParse(command.ExecuteScalar(),out sumvalue);
SqlCommand.ExecuteScalar
null
object obj = command.ExecuteScalar(); if ( obj == null) { // handle the no-result case, for instance set sumvalue=0 } else { sumvalue = (double) obj; }
Double.TryParse
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)