sum items

show sum when selected index changed from combobox

Page 1 of 1

1 Replies - 1459 Views - Last Post: 06 July 2009 - 05:42 AM Rate Topic: -----

#1 yangma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-May 09

sum items

Posted 06 July 2009 - 01:43 AM

 Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
	  

Try
			Using connection As New SqlConnection("server=localhost;database=att;uid=sa;password=;")
				Using command As New SqlCommand("SELECT SUM(no_of_days) FROM leave_approval where applicant='" & ComboBox2.Text & "' and leave_type='" & ComboBox4.Text & "' ", _
												  connection)
					connection.Open()
					Dim totalQuantity As Integer = CDbl(command.ExecuteScalar())
					
					TextBox6.Text = totalQuantity

				End Using
			End Using
		Catch ex As SqlException
			For Each er As SqlError In ex.Errors
				MessageBox.Show(er.Message)
			Next
		End Try




	End Sub

this is my code ..i want to display the sum of all no_of_days(column name) in textbox6...if i use button then it is fine..when i use in combobox and try to display when selected index is changed then i get error while running program"Conversion from type 'DBNull' to type 'Double' is not valid."
how can i fix this???
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: sum items

#2 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: sum items

Posted 06 July 2009 - 05:42 AM

View Postyangma, on 6 Jul, 2009 - 12:43 AM, said:

 Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
	  

Try
			Using connection As New SqlConnection("server=localhost;database=att;uid=sa;password=;")
				Using command As New SqlCommand("SELECT SUM(no_of_days) FROM leave_approval where applicant='" & ComboBox2.Text & "' and leave_type='" & ComboBox4.Text & "' ", _
												  connection)
					connection.Open()
					Dim totalQuantity As Integer = CDbl(command.ExecuteScalar())
					
					TextBox6.Text = totalQuantity

				End Using
			End Using
		Catch ex As SqlException
			For Each er As SqlError In ex.Errors
				MessageBox.Show(er.Message)
			Next
		End Try




	End Sub

this is my code ..i want to display the sum of all no_of_days(column name) in textbox6...if i use button then it is fine..when i use in combobox and try to display when selected index is changed then i get error while running program"Conversion from type 'DBNull' to type 'Double' is not valid."
how can i fix this???
thanks


Change uor sqlquery something like this:

"SELECT SUM(no_of_days) FROM leave_approval where (no_of_days IS NOT NULL) AND ....(another condition will goes here)

~'J'~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1