why datatable change the format of date - sql

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

Related

Unable to add days to datetime after 12 days

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

Syntax error in UPDATE statement. via cmd.nonexecutequery

If Me.TextBox1.Tag & "" = "" Then
cmd.CommandText = "INSERT INTO Table1(CandidateID, Fname, Mname, Lname, Partylist, Pst, course) " & _
" VALUES (" & Me.TextBox1.Text & ", '" & Me.TextBox2.Text & "', '" & Me.TextBox3.Text & "', '" & _
Me.TextBox4.Text & "', '" & Me.ComboBox1.Text & "', '" & Me.ComboBox2.Text & "', '" & Me.ComboBox3.Text & "')"
cmd.ExecuteNonQuery()
Else
cmd.CommandText = "UPDATE table " & _
" SET CandidateID=" & Me.TextBox1.Text & _
", Fname='" & Me.TextBox2.Text & "'" & _
", Mname='" & Me.TextBox3.Text & "'" & _
", Lname='" & Me.TextBox4.Text & "'" & _
", Partylist='" & Me.ComboBox1.Text & "'" & _
", Pst='" & Me.ComboBox2.Text & "'" & _
", Course='" & Me.ComboBox3.Text & "'" & _
" WHERE CandidateID=" & Me.TextBox1.Tag
cmd.ExecuteNonQuery()
cmd.CommandText = "UPDATE Table1 " & _
" SET CandidateID=" & Me.TextBox1.Text & _
", Fname='" & Me.TextBox2.Text & "'" & _
", Mname='" & Me.TextBox3.Text & "'" & _
", Lname='" & Me.TextBox4.Text & "'" & _
", Partylist='" & Me.ComboBox1.Text & "'" & _
", Pst='" & Me.ComboBox2.Text & "'" & _
", Course='" & Me.ComboBox3.Text & "'" & _
" WHERE CandidateID=" & Me.TextBox1.Tag
cmd.ExecuteNonQuery()
my table was incorrect.
thank you for those who replied to my thread

insert statement error vb.net

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))

Syntax error in INSERT INTO Statement in vb.net

I am getting and syntax error in insert into statement. IF statement is working just fine just getting this error when it's trying to save the information
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If txt12Per.Text >= TextBox12.Text And txtGPer.Text >= TextBox11.Text And TextBox1.Text >= TextBox10.Text Then
Try
'Dim da As OleDb.OleDbDataAdapter
Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Taher\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\Database1.accdb;Persist Security Info=False;"
Me.con = New OleDb.OleDbConnection()
con.ConnectionString = dbprovider
con.Open()
Dim sqlquery As String = "INSERT INTO MCAscheduled (URno,SName,hsc,gper,pgper,pstatus,cname,hrname,position,hscinter,ginter,pginter,comments)" + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "');"
Dim sqlcommand As New OleDb.OleDbCommand(sqlquery)
With sqlcommand
.CommandText = sqlquery
.Connection = con
.ExecuteNonQuery()
con.Close()
txtUrn.Text = ""
txt12Per.Text = ""
txtGPer.Text = ""
txtName.Text = ""
cmbNameofGCourse.Text = ""
End With
MsgBox("Record Added")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Else
MsgBox("Student Not eligible for the requested company")
End If
End Sub
Can somebody help me with this....
You are trying to insert into 13 columns but provide only 11 values!
You have 13 fields to insert values in, but only 11 values.
Use brackets and it will work fine.
Dim sqlquery As String = "INSERT INTO MCAscheduled **([URno],[SName],[hsc],[gper],[pgper],[pstatus],[cname],[hrname],[position],[hscinter],[ginter],[pginter],[comments])"** + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "');"
had to change the position field to something else don't know why but it worked
Dim sqlquery As String = "INSERT INTO MCAscheduled (URno,sname,hsc,gper,pgper,pstatus,cname,hrname,hscinter,ginter,pginter,comments,post,course,pcourse)" + "VALUES (" & CInt(txtUrn.Text) & ",'" & txtName.Text & "'," & CInt(txt12Per.Text) & "," & CInt(txtGPer.Text) & "," & CInt(TextBox1.Text) & ",'" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "'," & CInt(TextBox12.Text) & "," & CInt(TextBox11.Text) & "," & CInt(TextBox10.Text) & ",'" & TextBox9.Text & "','" & ComboBox1.Text & "','" & cmbNameofGCourse.Text & "','" & TextBox5.Text & "');"

app.config connection string does not work

this piece of code works fine....
Private Sub save()
Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Chuttu VB\Projects\LIC\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
"VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
& NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
& PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)
MsgBox(sql.CommandText)
con.Open()
MsgBox(con.State.ToString)
Dim i As Integer = sql.ExecuteNonQuery
MsgBox(i.ToString)
con.Close()
sql.Dispose()
con.Dispose()
ToolStripStatusLabelMessage.Text = "Saved"
End Sub
as soon as i change the connection string to the connection string from app.config it stops working(adding data to DB)
Private Sub save()
Dim con As New SqlClient.SqlConnection(LIC.My.Settings.LICConnectionString)
Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
"VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
& NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
& PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)
MsgBox(sql.CommandText)
con.Open()
MsgBox(con.State.ToString)
Dim i As Integer = sql.ExecuteNonQuery
MsgBox(i.ToString)
con.Close()
sql.Dispose()
con.Dispose()
ToolStripStatusLabelMessage.Text = "Saved"
End Sub
NOTE: I get no errors.
Here is a short deal explanation.
This is how you use a connection string from the config file.
Dim sqlConn as SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)
Here is a link to how to do a parameterized queries
Try changing LIC.My.Settings.LICConnectionString
to ConfigurationManager.ConnectionStrings["LICConnectionString"].ConnectionString