I am trying to assign a booking to a specific room but I keep getting the error below would anyone be able to explain to me why this error is coming up?
I added the code for the "AddToRoom" function.
SQL logic error near "If" : syntax error
Private Sub IbtnAdd_Click(sender As Object, e As EventArgs) Handles ibtnCheckIn.Click
Dim Result As Integer
Dim SelectedValue As Integer = CInt(cmbBookingData.SelectedValue)
Dim selectedRoom As Integer = CInt(cmbRoomNumber.Text)
Dim d1 = ValidateDates(txtDate1.Text)
Dim d2 = ValidateDates(txtDate2.Text)
Try
Result = AddToRoom(selectedRoom, d1, d2, SelectedValue, "Taken")
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
Dim dt As DataTable = Nothing
If Result = 1 Then
MsgBox("DoubleBooking NO RECORD HAS BEEN ADDED!")
Else
MsgBox("New RECORD HAS BEEN ADDED!")
Try
dt = GetBookedRooms()
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
End If
dgvBookedRooms.DataSource = dt
End Sub
Public Function AddToRoom(brRoomID As Integer, ArrDate As String, DepDate As String, BCustomerID As Integer, Status As String) As Integer
Dim ValidateSql As String = "SELECT brRoomID, arrivaldate, departuredate
FROM bookedrooms
Join booking on bookedrooms.brBookingID = booking.BookingID
WHERE (brRoomID = #Room)
AND (arrivaldate <= #ArrDate )
AND (departuredate >= #DepDate);"
Dim InsertSql As String = "INSERT INTO bookedrooms(brBookingID,brRoomID,Status)
VALUES (#brBookingID,#brRoomID,#Status)"
Dim CommandSql = $"If Not Exists ({ValidateSql}) {InsertSql}"
Dim Result As Integer
Using con As New SQLiteConnection(ConnectionString),
cmd As New SQLiteCommand(CommandSql, con)
With cmd.Parameters
.Add("#Room", DbType.Int32).Value = brRoomID
.Add("#ArrDate", DbType.String).Value = ArrDate
.Add("#DepDate", DbType.String).Value = DepDate
.Add("#brBookingID", DbType.Int32).Value = BCustomerID
.Add("#brRoomID", DbType.Int32).Value = brRoomID
.Add("#Status", DbType.String).Value = Status
End With
con.Open()
Result = cmd.ExecuteNonQuery
End Using
Return Result
End Function
Related
This was the error msg im gettingenter image description hereI have a database that has a row "Total_Time" (time).
It is in the format HH:MM:SS. I need the code to convert the "Total_time" to minutes.
For example, if Total_time = 01:30:00, the answer should be Total_minutes = 90,
and I want to multiply the total_minutes with "Other" (int variable).
Below is what I have tried:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
con = New System.Data.SqlClient.SqlConnection
Try
con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
con.Open()
Dim cm As SqlClient.SqlCommand
cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE Id=#id", con)
cm.Parameters.AddWithValue("#id", TextBox5.Text)
dr = cm.ExecuteReader()
While dr.Read()
Dim tt As Double
tt = dr("Total_Time").ToString
Dim other As Double
other = dr("Other").ToString
Dim str() As String
Dim strmin As Double
str = Split(tt.ToString, ":")
strmin = (CDbl(str(1)) * 60 + CDbl(str(2)) + CDbl(str(3)) / 60).ToString
Dim total As Decimal
total = strmin + other
Label7.Text = total.ToString
End While
Catch ex As Exception
End Try
End Sub
but when i click nothing is happening label7 is not displaying any values
Thanks in advance.
Dim Total_minutes As Double = CDate("1:23:45").TimeOfDay.TotalMinutes ' 83.75
To avoid similar errors, I would highly recommend using Option Strict
Dim Total_Time As DateTime = Convert.ToDateTime(dr!Total_Time)
Dim Total_minutes# = Total_Time.TimeOfDay.TotalMinutes
Dim Other# = Val(dr!Other)
Dim total# = Total_minutes * Other
Label7.Text = total.ToString
Try below
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
con = New System.Data.SqlClient.SqlConnection
Try
con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
con.Open()
Dim cm As SqlClient.SqlCommand
cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE Id=#id", con)
cm.Parameters.AddWithValue("#id", TextBox5.Text)
dr = cm.ExecuteReader()
While dr.Read()
Dim other As TimeSpan
Dim tt As TimeSpan
If TimeSpan.TryParse(dr("Total_Time"), tt) Then
If TimeSpan.TryParse(dr("Other"), other) Then
tt = tt.Add(other)
Else
'Do something like show error message for incorrect data for dr("Other")
End If
Label7.Text = tt.TotalMinutes.ToString
Else
'Do something like show error message for incorrect data for dr("Total_Time")
End If
End While
Catch ex As Exception
End Try
End Sub
If time is more than 24:00:00, use below code
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
con = New System.Data.SqlClient.SqlConnection
Try
con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
con.Open()
Dim cm As SqlClient.SqlCommand
cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE Id=#id", con)
cm.Parameters.AddWithValue("#id", TextBox5.Text)
dr = cm.ExecuteReader()
While dr.Read()
Try
Dim dataTime As String = dr("Total_Time").ToString
dataTime = dataTime.Split("."c)(0).ToString
Dim tt As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2)))
dataTime = dr("Other").ToString
dataTime = dataTime.Split("."c)(0).ToString
Dim other As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2)))
tt = tt.Add(other)
dataTime = tt.TotalMinutes.ToString
Catch ex As Exception
'do something here as string is not a time
End Try
End While
Catch ex As Exception
End Try
End Sub
Don't keep switching to strings... and use a Timespan
If your label never changes then the routine must be erroring out, examine the error from the catch statement. However from your image it looks like Other has no value.
And you should ALWAYS test for dbnull when using databases. DBNull has some weird behaviours in Math and Boolean comparisons.
Replace your while loop with this
If Not Dr.read OrElse IsDBNull(Dr("Total_time")) OrElse IsDBNull(Dr("Other")) Then
Label7.text = "ERR"
Else
Dim ts As TimeSpan = TimeSpan.Parse(dr("Total_time").ToString)
Label7.text = (ts.TotalMinutes * Dr("Other")).ToString
End If
PS: Your question says multiply by Other but your form/code says Add... I went with the question.
I am loading the CSV file to vb.net datatable .
then I am using Bulk copy command to write to database.
The problem is if one date cell in the data table is empty, i am receiving
ORA:-01840 input value not long enough for date format.
How to resolve this.
Public Class Form1
Dim cn As New OracleConnection("Data Source =(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = ipaddressofserver)(PORT = portofserver))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = oracleservicename)) ) ; User Id=useridofdb;Password=passwordofdb")
cn.Open()
MsgBox("connection opened")
Dim dt As DataTable = ReadCSV("D:\test.csv")
DataGridView1.DataSource = dt
Try
Dim _bulkCopy As New OracleBulkCopy(cn)
_bulkCopy.DestinationTableName = "TEST_TABLE"
_bulkCopy.BulkCopyTimeout = 10800
_bulkCopy.WriteToServer(dt)
cn.Close()
cn.Dispose()
cn = Nothing
MsgBox("Finished")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Function ReadCSV(ByVal path As String) As System.Data.DataTable
Try
Dim sr As New StreamReader(path)
Dim fullFileStr As String = sr.ReadToEnd()
sr.Close()
sr.Dispose()
Dim lines As String() = fullFileStr.Split(ControlChars.Lf)
Dim recs As New DataTable()
Dim sArr As String() = lines(0).Split(","c)
For Each s As String In sArr
recs.Columns.Add(New DataColumn())
Next
Dim row As DataRow
Dim finalLine As String = ""
For Each line As String In lines
row = recs.NewRow()
finalLine = line.Replace(Convert.ToString(ControlChars.Cr), "")
row.ItemArray = finalLine.Split(","c)
recs.Rows.Add(row)
Next
Return recs
Catch ex As Exception
Throw ex
End Try
End Function
End Class
I am currently trying to pass a two table value parameters from a .net application to a database.
The error that I am getting is
DBNull value for parameter '#StageNotes' is not supported. Table-valued parameters cannot be DBNull.
I have checked that the parameter value is not null when I pass it, and the error that I get is not a sqlException, rather just a general Exception that passes this error back
The parameters are declared like so
Dim stageNotesparamTable2SQL As New SqlParameter("#StageNotes", SqlDbType.Structured)
Dim responsesParamTable2SQL As New SqlParameter("#Responses", SqlDbType.Structured)
and the values are assigned like this
Dim responseTable = ResponsesToDt()
responsesParamTable2SQL.Value = responseTable
Dim stageTable = StageNotesToDt()
stageNotesparamTable2SQL.Value = stageTable
The parameters are declared in the stored proc like this
#StageNotes [App].[StageNotesTableType] READONLY,
#Responses [app].QuestionResponseTableType READONLY,
When debugging I see that the stageNotesparamTable2SQL.Value is showing that there is a datatable in there and has the data so it is definitely not null.
I would be grateful if anyone could help me with this.
thanks
-- edit ---
Protected Function ResponsesToDt() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("CheckId", GetType(Integer))
dt.Columns.Add("QuestionId", GetType(Integer))
dt.Columns.Add("AnswerId", GetType(Integer))
dt.Columns.Add("StaffNumber", GetType(String)).MaxLength = 15
Dim dictValues = _dicOfControlsAndValues.Where(Function(x) x.Key.Contains("ddl_Stage_"))
For Each item In dictValues
Dim ddlIdBreakDown = item.Key.ToString().Split("_").ToList
Dim checkid As Integer = item.Key.ToString().Split("_").Last
Dim questionId As Integer = Convert.ToInt16(ddlIdBreakDown(4))
Dim answerId As Integer = Convert.ToInt16(item.Value)
Dim staffNumber As String = GetLoggedOnStaffNumber()
dt.Rows.Add(checkid, questionId, answerId, staffNumber)
Next
Return dt
End Function
Protected Function StageNotesToDt() As DataTable
Dim dt As New DataTable
dt.Columns.Add("CheckId", GetType(Integer))
dt.Columns.Add("StageId", GetType(Integer))
dt.Columns.Add("StageNotes", GetType(String))
dt.Columns.Add("ModifiedDate", GetType(String))
Dim dictValues = _dicOfControlsAndValues.Where(Function(x) x.Key.Contains("textArea_Stage"))
For Each item In dictValues
Dim stageNote As String = String.Empty
Dim ddlIdBreakDown = item.Key.ToString().Split("_").ToList
If String.IsNullOrEmpty(item.Value.ToString()) Then
stageNote = "."
Else
stageNote = item.Value.ToString()
End If
Dim checkid As Integer = item.Key.ToString().Split("_").Last
Dim stageId As Integer = Convert.ToInt16(ddlIdBreakDown(2))
Dim stageNotes As String = stageNote
Dim modifiedDate As DateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
dt.Rows.Add(checkid, stageId, stageNotes, modifiedDate)
Next
Return dt
End Function
-- edit 2 --
DAL.ExecCommand("[App].[usp_SaveCheckDetails]", "",
checkIdParam,
userIdParam,
caseNoteParam,
checkNoteParam,
checkCompletedParam,
CheckFeedbackSentToUserId,
stageNotesparamTable2SQL,
responsesParamTable2SQL)
Then the DAL im having to call.
The DAL is a shared repo that Im not able to edit at the moment:
Public Function ExecCommand(ByVal spName As String, ByVal connStringName As String, ByVal ParamArray SPParms() As SqlClient.SqlParameter) As Boolean
Dim SqlConnection As New SqlConnection
Dim sqlCommand As New SqlClient.SqlCommand
Dim returnCode As Boolean = False
Try
If connStringName <> "" Then
SqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings(connStringName).ConnectionString
Else
SqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
End If
sqlCommand.CommandType = CommandType.StoredProcedure
sqlCommand.CommandText = spName
For Each p As SqlParameter In SPParms
If p.Direction <> ParameterDirection.Output Then
If Not p.Value.GetType() = GetType(Integer) Then
If p.Value.ToString() = "" OrElse p.Value = Nothing Then
p.Value = DBNull.Value
End If
End If
End If
sqlCommand.Parameters.Add(p)
Next
sqlCommand.Connection = SqlConnection
SqlConnection.Open()
sqlCommand.ExecuteNonQuery()
returnCode = True
Catch sqlEx As SqlException
HelpersErrorHandling.WriteAppLogEntry(Me.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().ToString, sqlEx)
Catch ex As Exception
If Not ex.Message.ToString().Contains("Thread was being aborted") Then
HelpersErrorHandling.WriteAppLogEntry(Me.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().ToString, ex)
End If
Finally
Try
sqlCommand.Parameters.Clear()
sqlCommand.Dispose()
SqlConnection.Close()
SqlConnection.Dispose()
Catch ex As Exception
HelpersErrorHandling.WriteAppLogEntry(Me.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().ToString, ex)
End Try
End Try
Return returnCode
End Function
I've been receiving errors with the following code below saying that the index is incorrect. I'm assuming this is an error with the SQL statement but I'm unsure what's wrong.
Private Sub btnStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStock.Click
frmStock.Visible = True
Dim SQLCmd As String
SQLCmd = "SELECT PartID,PartName,PartStockLevel,MakeName,ModelName FROM tblParts WHERE PartStockLevel <= StockWarnLevel;"
RunSQLCmd("dt", SQLCmd)
Dim inc As Integer = 0
Dim NoLowStock As Boolean
If DataTable IsNot Nothing AndAlso DataTable.Rows.Count > 0 Then
frmStock.txtPartID.Text = DataTable.Rows(inc).Item(0)
frmStock.txtName.Text = DataTable.Rows(inc).Item(1)
frmStock.NUDStockLvl.Value = DataTable.Rows(inc).Item(2)
frmStock.txtMake.Text = DataTable.Rows(inc).Item(3)
frmStock.txtModel.Text = DataTable.Rows(inc).Item(4)
Else
frmStock.lblLowStock.Visible = True
frmStock.btnFirstRecord.Visible = False
frmStock.btnIncDown.Visible = False
frmStock.btnIncUp.Visible = False
frmStock.btnLastRecord.Visible = False
NoLowStock = True
End If
If NoLowStock = False Then
frmStock.Panel1.Visible = False
End If
End Sub
Public Sub RunSQLCmd(ByVal DTorDS As String, ByRef SQLCmd As String)
DataAdapter = New OleDb.OleDbDataAdapter(SQLCmd, con)
ConnectDB()
Try
If DTorDS = "dt" Then
DataTable = New DataTable
DataAdapter.Fill(DataTable)
Else
DataSet = New DataSet
DataAdapter.Fill(DataSet, "srcDataSet")
End If
Catch ex As Exception
con.Close()
End Try
con.Close()
End Sub
Thanks in advance!
The error probably comes from you looking at a row index that doesn't exist in the results. You should check that the table has rows before trying to get the data.
Something like this:
If DataTable IsNot Nothing AndAlso DataTable.Rows.Count >0 Then
... Code here...
End If
Try this simple way
dim dt as new datatable
using connection as new sqlconnection("server=SERVER;database=DATABASE;uid=USERID;pwd=PASSWORD")
connection.open()
using cmd as new sqlcommand("SELECT PartID,PartName,PartStockLevel,MakeName,ModelName FROM tblParts WHERE PartStockLevel <= StockWarnLevel", connection)
dt.load(cmd.executereader)
end using
end using
Why my set of codes didn't update in DataSet? Then it goes to Error. Please anyone check this code and point me out where I am missing. Thanks in advance!
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim conxMain As New SqlConnection("Data Source=SERVER;Initial Catalog=DBTest;Persist Security Info=True;User ID=username;Password=pwds")
Dim dadPurchaseInfo As New SqlDataAdapter
Dim dsPurchaseInfo As New DataSet1
Try
Dim dRow As DataRow
conxMain.Open()
Dim cmdSelectCommand As SqlCommand = New SqlCommand("SELECT * FROM Stock", conxMain)
cmdSelectCommand.CommandTimeout = 30
dadPurchaseInfo.SelectCommand = cmdSelectCommand
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dadPurchaseInfo)
dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")
For Each dRow In dsPurchaseInfo.Tables("Stock").Rows
If CInt(dRow.Item("StockID").ToString()) = 2 Then
dRow.Item("StockCode") = "Re-Fashion[G]"
End If
Next
dadPurchaseInfo.Update(dsPurchaseInfo, "Stock")
Catch ex As Exception
MsgBox("Error : ")
Finally
If dadPurchaseInfo IsNot Nothing Then
dadPurchaseInfo.Dispose()
End If
If dsPurchaseInfo IsNot Nothing Then
dsPurchaseInfo.Dispose()
End If
If conxMain IsNot Nothing Then
conxMain.Close()
conxMain.Dispose()
End If
End Try
End Sub
Does your condition in the loop get executed (set a break point!)? Where is the error thrown? What error?
Also, why does it use ToString at all? This seems redundant.
If CInt(dRow.Item("StockID")) = 2 Then
Should be enough.
Finally, you’re performing redundant cleanup:
If conxMain IsNot Nothing Then
conxMain.Close()
conxMain.Dispose()
End If
Dispose implies Close – no need to perform both operations:
Close and Dispose are functionally equivalent.
[Source: MSDN]
Does your dataAdapter has update command ?
(it looks like it doesn't - so it doesn't know what do to with update....)
Here is an Update Command example:(for an employee table with 3 columns - as listed below:
UPDATE [Employee]
SET [name] = #name
, [manager] = #manager
WHERE (([id] = #Original_id) AND
((#IsNull_name = 1 AND [name] IS NULL) OR
([name] = #Original_name)) AND
((#IsNull_manager = 1 AND [manager] IS NULL) OR
([manager] = #Original_manager)));
SELECT id
, name
, manager
FROM Employee
WHERE (id = #id)
You can see it is a general update that can handle changes in any field.
I got it from the error correction of my program by Konard Rudolph!
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim conxMain As New SqlConnection("Data Source=SERVER;Initial Catalog=DBTest;Persist Security Info=True;User ID=username;Password=pwds")
Dim dadPurchaseInfo As New SqlDataAdapter
Dim dsPurchaseInfo As New DataSet1
Try
Dim dRow As DataRow
conxMain.Open()
dadPurchaseInfo.SelectCommand = New SqlCommand("SELECT * FROM Stock", conxMain)
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dadPurchaseInfo)
dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")
For Each dRow In dsPurchaseInfo.Tables("Stock").Rows
If CInt(dRow.Item("StockID")) = 2 Then
dRow.Item("StockCode") = "Re-Fashion(H)"
End If
Next
dadPurchaseInfo.Update(dsPurchaseInfo, "Stock")
Catch ex As Exception
MsgBox("Error : " & vbCrLf & ex.Message)
Finally
If dadPurchaseInfo IsNot Nothing Then
dadPurchaseInfo.Dispose()
End If
If dsPurchaseInfo IsNot Nothing Then
dsPurchaseInfo.Dispose()
End If
If conxMain IsNot Nothing Then
conxMain.Dispose()
End If
End Try
End Sub
The above set of code work to update with DataSet! Thanks to stackoverflow community and who answered my question.
Here is ref:
How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual Basic .NET
How to update a database from a DataSet object by using Visual Basic .NET
p.s: Like o.k.w said : The Table must have primary key. Thanks o.k.w!
--MENU--
Dim login As New LoginClass
login.ShowDialog()
--CONEXION--
Private conec As SqlConnection
Dim stringCon As String = "Data Source= ;Initial Catalog=;Persist Security Info=True;User ID=;Password="
Public ReadOnly Property prConec() As Object
Get
Return conec
End Get
End Property
Public Sub Conectar()
Try
conec = New SqlConnection(stringCon)
If conec.State <> ConnectionState.Open Then
conec.Open()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
--BUSCAR--
funciones.Conectar()
Dim coman As New SqlCommand("sp_cliente", funciones.prConec)
Dim dt As New DataTable
coman.CommandType = CommandType.StoredProcedure
coman.Parameters.Add("#i_operacion", SqlDbType.Char, 1, ParameterDirection.Input).Value = "B"
dt.Load(coman.ExecuteReader())
grdClientes.DataSource = dt
--INSERTAR--
funciones.Conectar()
Dim coman As New SqlCommand("sp_articulo", funciones.prConec)
coman.CommandType = CommandType.StoredProcedure
coman.Parameters.Add("#i_operacion", SqlDbType.Char, 1, ParameterDirection.Input).Value = "I"
coman.ExecuteNonQuery()
Buscar()
Limpiar()
--COMBO--
Dim dt As New DataTable
dt.Columns.Add("Codigo")
dt.Columns.Add("Descripcion")
Dim dr1 As DataRow = dt.NewRow
dr1.Item("Codigo") = "A"
dr1.Item("Descripcion") = "Activo"
dt.Rows.Add(dr1)
Dim dr2 As DataRow = dt.NewRow
dr2.Item("Codigo") = "I"
dr2.Item("Descripcion") = "Inactivo"
dt.Rows.Add(dr2)
cmbEstado.DataSource = dt
cmbEstado.ValueMember = "Codigo"
cmbEstado.DisplayMember = "Descripcion"
--GRIDVIEW--
--1--
Dim grdFila As DataGridViewRow = grdClientes.CurrentRow
txtCedula.Text = grdFila.Cells(0).Value
--2--
If DataGridProductos.CurrentCell.ColumnIndex = 0 Then
Dim FLstArticulos As New FLstArticulos
FLstArticulos.ShowDialog()
DataGridProductos.CurrentRow.Cells(0).Value = FLstArticulos.PrIdArticulo
End If
--GRIDVIEW.CELLENDEDIT--
If DataGridProductos.CurrentCell.ColumnIndex = 3 Then
Dim precio As New Double
Dim cantidad As New Double
precio = CDbl(grdRow.Cells(2).Value)
cantidad = CDbl(grdRow.Cells(3).Value)
DataGridProductos.CurrentRow.Cells(4).Value = PLTotalFilaItem(cantidad, precio)
PLCargaTotales()
End If
Sub PLCargaTotales()
Dim subTotal As Double
Dim iva As Double
For Each grd As DataGridViewRow In DataGridProductos.Rows
If Not String.IsNullOrEmpty(grd.Cells(4).Value) Then
subTotal = subTotal + CDbl(grd.Cells(4).Value)
End If
Next grd
txtSubtotal.Text = subTotal.ToString
iva = Decimal.Round(subTotal`enter code here` * 0.12)
txtIva.Text = iva.ToString
txtTotalPagar.Text = (subTotal + iva).ToString
End Sub