Server Error in '/' Application.
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 120: Else
Line 121: Dim total As Integer = dt.AsEnumerable().Sum(Function(row) row.Field(Of Integer)("Qty"))
Line 122: Dim total2 As Integer = dt.AsEnumerable().Sum(Function(row) row.Field(Of Integer)("amount"))
Line 123: Gvgridclient.FooterRow.Cells(2).Text = "Total"
Line 124: Gvgridclient.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Right
Source File: D:\WANDEGEYAIMAGE\UNIFORMWEB2023\UNIFORMWEB2023\Forms\Client.aspx.vb Line: 122
Stack Trace:
Private Sub BindGrid()
Dim query As String = "SELECT invno, date, qty, orderprice, subtotal, ordervat, subtotal2, orderuser, ordertime, scancode, shift, itemcostprice, poststatus, stockitem, saleprice, itemcode, saleunit, saletype,qty*saleprice as amount, hidesale, noitemscarton, batchno, expirydate, batch,
account, clientname, salesperson, keyfield, itemsize FROM ordersubformhidesales where account='" & Account.Text & "'"
Dim constr As String = ConfigurationManager.ConnectionStrings("UNIFORMConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Gvgridclient.DataSource = dt
Gvgridclient.DataBind()
If Gvgridclient.Rows.Count <= 0 Then
Else
Dim total As Integer = dt.AsEnumerable().Sum(Function(row) row.Field(Of Integer)("Qty"))
Dim total2 As Integer = dt.AsEnumerable().Sum(Function(row) row.Field(Of Integer)("amount"))
Gvgridclient.FooterRow.Cells(2).Text = "Total"
Gvgridclient.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Right
Gvgridclient.FooterRow.Cells(3).Text = total.ToString("N2")
' Gvgridclient.FooterRow.Cells(6).Text = total2.ToString("N2")
' DirectCast(Gvgridclient.FooterRow.FindControl("Fqty"), TextBox).Text = total
Totalqty.Text = total
End If
End Using
End Using
End Using
End Using
End Sub
Error comes from here
Dim total2 As Integer = dt.AsEnumerable().Sum(Function(row) row.Field(Of Integer)("amount"))