I need to add 111 days to my STDATE but it stops and give an error after it reaches the 12th day:
Conversion failed when converting date and/or time from character string.
However, if I add 15 months to the same STDATE it works perfectly.
I = 0
For Z = 0 To 111
If CTY = "D" Then
STDATE = STDATE.AddDays(I)
'STDATE2 = STDATE.ToString("dd-MM-yyyy 00:00:01")
PAMTPART = Math.Round(TB3.Text / INSTN, 2)
CINSTUNIQ = Guid.NewGuid()
INTAMTPART = Math.Round(TB4.Text / INSTN, 2)
INSTSTR = "INSERT INTO [dbo].[tb_CaseInst] ([CaseInstUniq],[CaseInst_OrgUniq],[CaseInst_CaseUniq],[CaseInstDueDate],[CaseInstAmount], [CaseInstBalAmt],[CaseInstStatus],[CaseInstRemks],[CaseInstDate],[CaseInstPAmt],[CaseInstInttAmt],[IsActive])" _
& "VALUES ('" & CINSTUNIQ.ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','" & SRDATE & "'," & PAMTPART & "," & INTAMTPART & ",'A')"
CINSTSTR = "INSERT INTO [dbo].[tb_CaseInstD] ([CaseInstDUniq],[CaseInstD_OrgUniq],[CaseInstD_CaseUniq],[CaseInstD_InstUniq],[CaseInstDTranDueDate],[CaseInstDAmt],[CaseInstDBalAmt],[CaseInstDStatus],[CaseInstDRemks],[CaseInstDChqNo],[CaseInstDBankName],[CaseInstDChqDate],[CaseInstDChqAmt],[CaseInstDDate],[CaseInstDIsActive],[CaseInstDTransType],[CaseInstDPAmt],[CaseInstDInttAmt],[CaseInstDType])" _
& "VALUES ('" & Guid.NewGuid().ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & CINSTUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','XX','XX','" & SRDATE & "',0,'" & SRDATE & "','A'," & CASEI & "," & PAMTPART & "," & INTAMTPART & ",'I')"
I = 1
End If
I think that you have to use DateAdd function with DateInterval.Day,1,STDATE parameters :
STDATE = DateAdd(DateInterval.Day, 1, STDATE )
Hope this help you
Related
In the following code, I am facing an issue such that in SQL the STDATE is stored in datetime2 type, but its format gets changed when called via DataTable in vb.net.
For example: when the date in the database is "2020-02-05 11:32:47.0000000" it changes to #2/5/2020 11:32:47 AM#.
My requirement is to add months and days in the date from the server but when I add the month to it, it adds days instead of months[sic]. So I need the date to be of the same format as in SQL
Dim STDATE As Date
'Dim STDATE1 As String = ""
'Dim STDATE2 As String = ""
Dim STMONTH As Integer
Dim STMONDIFF As Integer
Dim CTY As String = ""
Dim I As Integer
Dim J As Integer
'Dim DPT2 As DateTime = DTP2.Value
CASVW()
Dim CASVWDT As New DataTable
CASVWDT = CASVWDS.Tables(0)
If CASVWDT.Rows.Count > 0 Then
J = CASVWDT.Rows.Count - 1
For I = 0 To J
If CASVWDT.Rows(I)("Case_CaseAbNo").ToString = TB1.Text Then
INSTN = CASVWDT.Rows(I)("Case_NoOfInst").ToString
INSTAMT = CASVWDT.Rows(I)("Case_InstAmt").ToString
STDATE = CASVWDT.Rows(I)("Case_Date")
STMONTH = Month(CASVWDT.Rows(I)("Case_InstStartDate"))
CTY = CASVWDT.Rows(I)("Case_PayType").ToString
End If
Next
End If
STMONDIFF = STMONTH - Month(STDATE)
STDATE = STDATE.AddMonths(STMONDIFF)
'STDATE1 = STDATE.ToString("dd-MM-yyyy 00:00:00")
Dim CBSTR1 As String = "SELECT * FROM TB_CASEINST WHERE CaseInst_CaseUniq='" & CSUNIQ.ToString & "'"
Dim CBDA1 As New SqlDataAdapter(CBSTR1, CN)
Dim CBDT1 As New DataTable
CBDA1.Fill(CBDT1)
CBDA1.Dispose()
If CBDT1.Rows.Count = 0 Then
Dim Z As Integer
Dim INSTSTR As String = ""
Dim CINSTSTR As String = ""
Dim CASEINSTSTR As String = ""
Dim CASEINSTSTR1 As String = ""
Dim CINSTUNIQ As Guid
Dim GURU1 As Long = TB3.Text
Dim GURU2 As Long = TB4.Text
Dim GURU As Long = GURU1 + GURU2
CASEINSTSTR = "INSERT INTO [dbo].[tb_CaseInstD] ([CaseInstDUniq],[CaseInstD_OrgUniq],[CaseInstD_CaseUniq],[CaseInstD_InstUniq],[CaseInstDTranDueDate],[CaseInstDAmt],[CaseInstDBalAmt],[CaseInstDStatus],[CaseInstDRemks],[CaseInstDChqNo],[CaseInstDBankName],[CaseInstDChqDate],[CaseInstDChqAmt],[CaseInstDDate],[CaseInstDIsActive],[CaseInstDTransType],[CaseInstDPAmt],[CaseInstDInttAmt],[CaseInstDType])" _
& "VALUES ('" & Guid.NewGuid().ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & CSUNIQ.ToString & "','" & STDATE & "'," & TB3.Text & "," & TB3.Text & ",'D','Loan Amount','" & TB6.Text & "','" & CB2.Text & "','" & DTP2.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") & "'," & TB3.Text & ",'" & SRDATE & "','A'," & CASEI & "," & TB3.Text & "," & TB4.Text & ",'L')"
Dim CASEINSTCMD As New SqlCommand(CASEINSTSTR, CN)
CASEINSTCMD.CommandType = CommandType.Text
CASEINSTCMD.ExecuteNonQuery()
CASEINSTCMD.Dispose()
CASEINSTSTR1 = "INSERT INTO [dbo].[tb_CaseInstD] ([CaseInstDUniq],[CaseInstD_OrgUniq],[CaseInstD_CaseUniq],[CaseInstD_InstUniq],[CaseInstDTranDueDate],[CaseInstDAmt],[CaseInstDBalAmt],[CaseInstDStatus],[CaseInstDRemks],[CaseInstDChqNo],[CaseInstDBankName],[CaseInstDChqDate],[CaseInstDChqAmt],[CaseInstDDate],[CaseInstDIsActive],[CaseInstDTransType],[CaseInstDPAmt],[CaseInstDInttAmt],[CaseInstDType])" _
& "VALUES ('" & Guid.NewGuid().ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & CSUNIQ.ToString & "','" & STDATE & "'," & TB4.Text & "," & GURU & ",'D','Intt Amount','" & TB6.Text & "','" & CB2.Text & "','" & DTP2.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") & "'," & TB3.Text & ",'" & SRDATE & "','A'," & CASEI & "," & TB3.Text & "," & TB4.Text & ",'L')"
Dim CASEINSTCMD1 As New SqlCommand(CASEINSTSTR1, CN)
CASEINSTCMD1.CommandType = CommandType.Text
CASEINSTCMD1.ExecuteNonQuery()
CASEINSTCMD1.Dispose()
I = 0
For Z = 0 To INSTN - 1
If CTY = "M" Then
STDATE = STDATE.AddMonths(I)
'STDATE2 = STDATE.ToString("dd-MM-yyyy 00:00:01")
PAMTPART = Math.Round(TB3.Text / INSTN, 2)
INTAMTPART = Math.Round(TB4.Text / INSTN, 2)
CINSTUNIQ = Guid.NewGuid()
INSTSTR = "INSERT INTO [dbo].[tb_CaseInst] ([CaseInstUniq],[CaseInst_OrgUniq],[CaseInst_CaseUniq],[CaseInstDueDate],[CaseInstAmount], [CaseInstBalAmt],[CaseInstStatus],[CaseInstRemks],[CaseInstDate],[CaseInstPAmt],[CaseInstInttAmt],[IsActive])" _
& "VALUES ('" & CINSTUNIQ.ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','" & SRDATE & "'," & PAMTPART & "," & INTAMTPART & ",'A')"
CINSTSTR = "INSERT INTO [dbo].[tb_CaseInstD] ([CaseInstDUniq],[CaseInstD_OrgUniq],[CaseInstD_CaseUniq],[CaseInstD_InstUniq],[CaseInstDTranDueDate],[CaseInstDAmt],[CaseInstDBalAmt],[CaseInstDStatus],[CaseInstDRemks],[CaseInstDChqNo],[CaseInstDBankName],[CaseInstDChqDate],[CaseInstDChqAmt],[CaseInstDDate],[CaseInstDIsActive],[CaseInstDTransType],[CaseInstDPAmt],[CaseInstDInttAmt],[CaseInstDType])" _
& "VALUES ('" & Guid.NewGuid().ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & CINSTUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','XX','XX','" & SRDATE & "',0,'" & SRDATE & "','A'," & CASEI & "," & PAMTPART & "," & INTAMTPART & ",'I')"
I = 1
ElseIf CTY = "D" Then
STDATE = STDATE.AddDays(I)
'STDATE2 = STDATE.ToString("dd-MM-yyyy 00:00:01")
PAMTPART = Math.Round(TB3.Text / INSTN, 2)
CINSTUNIQ = Guid.NewGuid()
INTAMTPART = Math.Round(TB4.Text / INSTN, 2)
INSTSTR = "INSERT INTO [dbo].[tb_CaseInst] ([CaseInstUniq],[CaseInst_OrgUniq],[CaseInst_CaseUniq],[CaseInstDueDate],[CaseInstAmount], [CaseInstBalAmt],[CaseInstStatus],[CaseInstRemks],[CaseInstDate],[CaseInstPAmt],[CaseInstInttAmt],[IsActive])" _
& "VALUES ('" & CINSTUNIQ.ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','" & SRDATE & "'," & PAMTPART & "," & INTAMTPART & ",'A')"
CINSTSTR = "INSERT INTO [dbo].[tb_CaseInstD] ([CaseInstDUniq],[CaseInstD_OrgUniq],[CaseInstD_CaseUniq],[CaseInstD_InstUniq],[CaseInstDTranDueDate],[CaseInstDAmt],[CaseInstDBalAmt],[CaseInstDStatus],[CaseInstDRemks],[CaseInstDChqNo],[CaseInstDBankName],[CaseInstDChqDate],[CaseInstDChqAmt],[CaseInstDDate],[CaseInstDIsActive],[CaseInstDTransType],[CaseInstDPAmt],[CaseInstDInttAmt],[CaseInstDType])" _
& "VALUES ('" & Guid.NewGuid().ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & CINSTUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','XX','XX','" & SRDATE & "',0,'" & SRDATE & "','A'," & CASEI & "," & PAMTPART & "," & INTAMTPART & ",'I')"
I = 1
ElseIf CTY = "W" Then
STDATE = STDATE.AddDays(I)
'STDATE2 = STDATE.ToString("dd-MM-yyyy 00:00:01")
PAMTPART = Math.Round((TB3.Text / INSTN), 2) * 7
CINSTUNIQ = Guid.NewGuid()
INTAMTPART = Math.Round((TB4.Text / INSTN), 2) * 7
INSTSTR = "INSERT INTO [dbo].[tb_CaseInst] ([CaseInstUniq],[CaseInst_OrgUniq],[CaseInst_CaseUniq],[CaseInstDueDate],[CaseInstAmount], [CaseInstBalAmt],[CaseInstStatus],[CaseInstRemks],[CaseInstDate],[CaseInstPAmt],[CaseInstInttAmt],[IsActive])" _
& "VALUES ('" & CINSTUNIQ.ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','" & SRDATE & "'," & PAMTPART & "," & INTAMTPART & ",'A')"
CINSTSTR = "INSERT INTO [dbo].[tb_CaseInstD] ([CaseInstDUniq],[CaseInstD_OrgUniq],[CaseInstD_CaseUniq],[CaseInstD_InstUniq],[CaseInstDTranDueDate],[CaseInstDAmt],[CaseInstDBalAmt],[CaseInstDStatus],[CaseInstDRemks],[CaseInstDChqNo],[CaseInstDBankName],[CaseInstDChqDate],[CaseInstDChqAmt],[CaseInstDDate],[CaseInstDIsActive],[CaseInstDTransType],[CaseInstDPAmt],[CaseInstDInttAmt],[CaseInstDType])" _
& "VALUES ('" & Guid.NewGuid().ToString & "','" & ORGUNQ.ToString & "','" & CSUNIQ.ToString & "','" & CINSTUNIQ.ToString & "','" & STDATE & "'," & INSTAMT & "," & INSTAMT & ",'D','Inst. Due','XX','XX','" & SRDATE & "',0,'" & SRDATE & "','A'," & CASEI & "," & PAMTPART & "," & INTAMTPART & ",'I')"
I = 6
Z = Z + 6
If Z >= 100 Then
INSTAMT = INSTAMT - (INSTAMT / 7)
End If
End If
Dim INSTCMD As New SqlCommand(INSTSTR, CN)
INSTCMD.CommandType = CommandType.Text
INSTCMD.ExecuteNonQuery()
INSTCMD.Dispose()
Dim CINSTCMD As New SqlCommand(CINSTSTR, CN)
CINSTCMD.CommandType = CommandType.Text
CINSTCMD.ExecuteNonQuery()
CINSTCMD.Dispose()
Next
This worked for me, might shed some light on your Problem, hope it helps.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strSQLconn As String = "Your Connectionstring"
Dim sqlConn As New SqlClient.SqlConnection(strSQLconn)
Dim Command As SqlCommand
Command = New SqlCommand("SELECT TOP(1) convert(varchar, [date], 23) FROM [dbo].[Element]", sqlConn)
Dim x As String
sqlConn.Open()
x = Command.ExecuteScalar()
sqlConn.Close()
MessageBox.Show("SQL Date Converted to String: " & x)
Dim d As Date = Date.ParseExact(x, "yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
MessageBox.Show("VB.net string Converted to Date: " & d.ToString)
MessageBox.Show("VB.net string Converted to Date, and an additional 1 month: " & d.AddMonths(1).ToString)
End Sub
In access 2016 I'm trying to use an update command.I'm creating a database as my internship project as I'm unfamiliar with the coding I have been struggling a lot. I'm referring to a video by Setha Iech: https://www.youtube.com/watch?v=Ri2Y9-16AEo. When I'm using the below code an error continuously pops up
Error 3464
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtID.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO Business(ID, Project_ID, [Date_of_Enquiry], Division, Client, Description, Probability_to_win,Status, [Proposal_Submission_date], [Expected_award_date], Remarks)" & _
"VALUES ('" & Me.txtID & "','" & Me.txtProject_ID & "','" & Me.txtDate_of_Enquiry & "','" & Me.cboDivision & "','" & _
Me.txtClient & "','" & Me.txtDescription & "','" & Me.cboProbability_to_win & "','" & Me.cboStatus & "','" & Me.txtProposal_Submission_date & "','" & Me.txtExpected_award_date & "','" & Me.txtRemarks & "')"
Else
'otherwise (Tag of txtID store the ID of statement to be modified)
CurrentDb.Execute "UPDATE Business" & _
" SET ID=" & Me.txtID & _
", Project_ID='" & Me.txtProject_ID & "'" & _
", Date_of_Enquiry='" & Me.txtDate_of_Enquiry & "'" & _
", Division='" & Me.cboDivision & "'" & _
", Client='" & Me.txtClient & "'" & _
", Description='" & Me.txtDescription & "'" & _
", Probability_to_win='" & Me.cboProbability_to_win & "'" & _
", Status='" & Me.cboStatus & "'" & _
", Proposal_Submission_date='" & Me.txtProposal_Submission_date & "'" & _
", Expected_award_date='" & Me.txtExpected_award_date & "'" & _
", Remarks='" & Me.txtRemarks & "'" & _
" WHERE ID=" & Me.txtID.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
databasesub.Form.Requery
End Sub
Thank you for your answers in advance
how do i insert money row and colomns to datagridview?
like i went to insert to datagridview this
is my code:
Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.Click
Call konek()
Dim simpan As String
Dim hasil As Integer
simpan = ("INSERT INTO T_Penjadwalan(Kode_Penjadwalan,Kelas,Kode_Jam,Jam_Mulai,Jam_Selesai,Senin,Selasa,Rabu,Kamis,Jumat,Sabtu) VALUES ('" & TBPENJADWALAN.Text & "','" & CBKelas.Text &
"','" & KJ1.Text & "','" & KJ2.Text & "','" & KJ3.Text & "','" & KJ4.Text & "','" & KJ5.Text & "','" & KJ6.Text & "','" & KJ7.Text & "','" & KJ8.Text & "','" & KJ9.Text &
"','" & JM1.Text & "','" & JM2.Text & "','" & JM3.Text & "','" & JM4.Text & "','" & JM5.Text & "','" & JM6.Text & "','" & JM7.Text & "','" & JM8.Text & "','" & JM9.Text &
"','" & JS1.Text & "','" & JS2.Text & "','" & JS3.Text & "','" & JS4.Text & "','" & JS5.Text & "','" & JS6.Text & "','" & JS7.Text & "','" & JS8.Text & "','" & JS9.Text &
"','" & Senin1.Text & "','" & Senin2.Text & "','" & Senin3.Text & "','" & Senin4.Text & "','" & Senin5.Text & "','" & Senin6.Text & "','" & Senin7.Text &
"','" & Selasa1.Text & "','" & Selasa2.Text & "','" & Selasa3.Text & "','" & Selasa4.Text & "','" & Selasa5.Text & "','" & Selasa6.Text & "','" & Selasa7.Text &
"','" & Rabu1.Text & "','" & Rabu2.Text & "','" & Rabu3.Text & "','" & Rabu4.Text & "','" & Rabu5.Text & "','" & Rabu6.Text & "','" & Rabu7.Text &
"','" & Kamis1.Text & "','" & Kamis2.Text & "','" & Kamis3.Text & "','" & Kamis4.Text & "','" & Kamis5.Text & "','" & Kamis6.Text & "','" & Kamis7.Text &
"','" & Jumat1.Text & "','" & Jumat2.Text & "','" & Jumat3.Text & "','" & Jumat4.Text & "','" & Jumat5.Text & "','" & Jumat6.Text & "','" & Jumat7.Text &
"','" & Sabtu1.Text & "','" & Sabtu2.Text & "','" & Sabtu3.Text & "','" & Sabtu4.Text & "','" & Sabtu5.Text & "','" & Sabtu6.Text & "','" & Sabtu7.Text & "')")
Try
cmd = New SqlCommand(simpan, conn)
hasil = cmd.ExecuteNonQuery()
If hasil > 0 Then
MessageBox.Show("Data Tersimpan", "Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call tampilgridpenjadwalan()
End If
Catch ex As Exception
MessageBox.Show("Failed : " & ex.Message, "Gagal", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub}
First of all, as others have said, it is better to use parameters when executing SQL queries in VB.NET. It has many benefits and it's fairly simple.
Taking a snippet of your insert query as an example, instead of defining the first value clause as '" & TBPENJADWALAN.Text & "' the first value might be #TBPENJADWALAN.
The string from TBPENJADWALAN.Text is then passed to #TBPENJADWALAN using the following code:
cmd.Parameters.Add("#TBPENJADWALAN", OleDbType.VarChar, 50).Value = TBPENJADWALAN.Text
OleDbType.VarChar = Type of data you're storing
50 = Size of your field
So instead of your code as follows...
simpan = ("INSERT INTO T_Penjadwalan(Kode_Penjadwalan) VALUES ('" & TBPENJADWALAN.Text & "')
...it would be:
simpan = ("INSERT INTO T_Penjadwalan(Kode_Penjadwalan) VALUES (#TBPENJADWALAN)")
cmd.Parameters.Add("#TBPENJADWALAN", OleDbType.VarChar, 50).Value = TBPENJADWALAN.Text
It's much cleaner and more secure. You can take that example and change the rest of your insert query.
In terms of your original question about inserting data to a DataGridView, in my experience, it's best to create a DataSet and Adapter to fill it. Try this:
Dim adapter As SQLDataAdapter
Dim ds As New DataSet
adapter = New SQLDataAdapter(simpan, cmd)
adapter.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
Iam trying to update records in sql through Excel . I have written down some code on a button in excel to insert and update records in sql server. I managed to insert records but getting error on update query . i have checked for solution on stackoverflow already but still getting error. Can anybody check what iam doing wrong in the following lines of code
conn.Execute = "UPDATE dbo.EmployeeInfo" _
& "SET Week= ('" & Week& "') " _
& "Time= ('" & Time & "') " _
& "DateFrom = ('" & DateFrom & "') " _
& "DatoTo = ('" & DatoTo & "') " _
& "Name= ('" & Name & "') " _
& "Description= ('" & Description & "') " _
& "Codes= ('" & Codes& "')" _
& "WHERE Week= (" & .Cells(iLoop, 1) & ")
AND DateFrom = (" & .Cells(iLoop, 3) & ")
AND DatoTo = (" & .Cells(iLoop, 4) & ")
AND Name= (" & .Cells(9, oLoop) & ")"
You need a comma between each column...
conn.Execute = "UPDATE dbo.EmployeeInfo" _
& "SET Week= ('" & Week& "'), " _
& "Time= ('" & Time & "'), " _
& "DateFrom = ('" & DateFrom & "'), " _
& "DatoTo = ('" & DatoTo & "'), " _
& "Name= ('" & Name & "'), " _
& "Description= ('" & Description & "'), " _
& "Codes= ('" & Codes& "')" _
& "WHERE Week= (" & .Cells(iLoop, 1) & ")
AND DateFrom = (" & .Cells(iLoop, 3) & ")
AND DatoTo = (" & .Cells(iLoop, 4) & ")
AND Name= (" & .Cells(9, oLoop) & ")"
following is the code which is use to enter datagridview items into the table.
Dim X As DataGridViewRow
grnno = 123123
glocation = txtlocation.Text
gsupplier = txtsupplier.Text
greceivedby = txtreceivedby.Text
greceiveddate = txtreceiveddate.Text
grn_status = cmbstatus.SelectedItem
ggrossamt = txtgrossamt.Text
gdiscountamount = txtdiscount.Text
gtotalnetamount = txttotalnet.Text
sqlstr = "INSERT INTO POS_GRN_HDR(loc_code,supplier_code,created_by,created_Date,grn_status,gross_amt,disc_Amt,net_Amt) values('" & glocation & "','" & gsupplier & "','" & greceivedby & "','" & greceiveddate & "','" & grn_status & "'," & ggrossamt & "," & gdiscountamount & "," & gtotalnetamount & " )"
sqlcmd = New SqlClient.SqlCommand(sqlstr, AppsCon)
sqlcmd.ExecuteNonQuery()
For Each X In datagridItems.Rows
sqlstr = "INSERT INTO POS_GRN_DTL(GRN_KEY,ITEM_CODE,DESCRIPTION,TYPE,UOM,BATCH_NO,EXPIRY_DATE,RECEIVED_QTY,UNIT_PRICE,AMOUNT,DISCOUNT,NET_AMOUNT) VALUES('" & grnno & "','" & X.Cells(0).Value & "','" & X.Cells(1).Value & "','" & X.Cells(2).Value & "','" & X.Cells(3).Value & "','" & X.Cells(4).Value & "','" & X.Cells(5).Value & "','" & X.Cells(6).Value & "','" & X.Cells(7).Value & "' ,'" & X.Cells(8).Value & "','" & X.Cells(9).Value & "','" & X.Cells(10).Value & "')"
sqlcmd = New SqlClient.SqlCommand(sqlstr, AppsCon)
sqlcmd.ExecuteNonQuery()
Next
the error is in the 2nd insert statement, it gives error cannot convert string to integer.. the cells from x.cell(6) are of integer type and in database also its integer type, now I want to ask should I enclose it in single quotations or not, as enclosing in single quotations give such errors like syntax '' and in double quotations it gives like cannot convert string to int type.please tell where I am doing wrong.
First of all use parametrized queries! It is safer and also more readable. You are passing some value as string but should be integer.
sqlstr = "INSERT INTO POS_GRN_HDR(loc_code,supplier_code,created_by,created_Date,grn_status,gross_amt,disc_Amt,net_Amt) _
values(#glocation, #gsupplier, #greceivedby, #greceiveddate, #grn_status, #ggrossamt, #gdiscountamount, #gtotalnetamount)"
sqlcmd = New SqlClient.SqlCommand(sqlstr, AppsCon)
sqlcmd.Parameters.AddWithValue("#glocation", glocation)
sqlcmd.Parameters.AddWithValue("#gsupplier", gsupplier) //and so on
For Each X In datagridItems.Rows
sqlstr = "INSERT INTO POS_GRN_DTL(GRN_KEY,ITEM_CODE,DESCRIPTION,TYPE,UOM,BATCH_NO,EXPIRY_DATE,RECEIVED_QTY,UNIT_PRICE,AMOUNT,DISCOUNT,NET_AMOUNT) _
VALUES(#grnno, #item_code, #description, ...)"
sqlcmd = New SqlClient.SqlCommand(sqlstr, AppsCon)
sqlcmd.Parameters.AddWithValue("#grnno", grnno)
sqlcmd.Parameters.AddWithValue("#item_code", CType(X.Cells(0).Value, Integer)) //cast to proper type
sqlcmd.ExecuteNonQuery()
Next
Remove the single quote marks ''
For example (and referring only to x.cell(6) as per your post) use " & X.Cells(6).Value & "
sqlstr = "INSERT INTO POS_GRN_DTL(GRN_KEY,ITEM_CODE,DESCRIPTION,TYPE,UOM,BATCH_NO,EXPIRY_DATE,RECEIVED_QTY,UNIT_PRICE,AMOUNT,DISCOUNT,NET_AMOUNT) VALUES('" & grnno & "','" & X.Cells(0).Value & "','" & X.Cells(1).Value & "','" & X.Cells(2).Value & "','" & X.Cells(3).Value & "','" & X.Cells(4).Value & "','" & X.Cells(5).Value & "', " & X.Cells(6).Value & ",'" & X.Cells(7).Value & "' ,'" & X.Cells(8).Value & "','" & X.Cells(9).Value & "','" & X.Cells(10).Value & "')"
You may also need to cast it (assuming it's always going to have a numeric value)
" & CInt(X.Cells(6).Value) &"
I will assume you know of SQL injection and this method of updating a database is generally 'outdated' and 'bad practice' now and you should use parameters instead...
Updated
Since there is a possibility of a null (and you want it to be a 0 where this is the case), you could use something like (not tested as I'm not a VB person)
dim cellSix as integer
if IsNothing(X.Cells(6).Value then
cellSix = 0
else
cellSix = CInt(X.Cells(6).Value)
end if
sqlstr = "INSERT INTO POS_GRN_DTL(GRN_KEY,ITEM_CODE,DESCRIPTION,TYPE,UOM,BATCH_NO,EXPIRY_DATE,RECEIVED_QTY,UNIT_PRICE,AMOUNT,DISCOUNT,NET_AMOUNT) VALUES('" & grnno & "','" & X.Cells(0).Value & "','" & X.Cells(1).Value & "','" & X.Cells(2).Value & "','" & X.Cells(3).Value & "','" & X.Cells(4).Value & "','" & X.Cells(5).Value & "', " & cellSix & ",'" & X.Cells(7).Value & "' ,'" & X.Cells(8).Value & "','" & X.Cells(9).Value & "','" & X.Cells(10).Value & "')"
Or, to keep the code shorter you could use the IIF
cellSix = IIf(isnothing(CInt(X.Cells(6).Value)), 0, CInt(X.Cells(6).Value))