Dim total As Integer
        For Each row As DataGridViewRow In DataGridView1.Rows
            total += row.Cells("Total").Value
        Next
        TextBox2.Text = total

   // the above was the code i used and it worked good,bt if i do it for another column it is showing an error (which is given below)//



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Anand\Documents\Database3.accdb"

        Dim SQLString As String = "SELECT Received,closed,Total,TRejects FROM cc"
        Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
        Dim DataSet1 As New DataSet()
        Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1)
        OleDBConn1.Open()
        OleDbDataAdapter1.Fill(DataSet1, "cc")
        DataGridView1.DataSource = DataSet1.Tables("cc")

        Dim total As Integer
        For Each row As DataGridViewRow In DataGridView1.Rows
            total += row.Cells("Total").Value
        Next
        TextBox2.Text = total

        Dim trejects As Integer
        For Each row As DataGridViewRow In DataGridView1.Rows
            trejects += row.Cells("TRejects").Value
        Next
        TextBox1.Text = trejects

/>

    pl help me out..........

Invalid CastException Was Unhandled
Operator '+' is not defined for type 'Integer' and type 'DBNull'

In lines 3, 24, and 30 did you try taking out the "+" sign? Also, did you check to see if you have any missing or broken references?

This article has been dead for over six months. Start a new discussion instead.