I have an insert statement with a number of DataGridViewComboBoxColumns. When I try and insert the values of these DataGridViewComboBoxColumns into my database the text "False" is being inserted into the fields.
Why is this happening?
Dim para As New SqlClient.SqlParameter
Dim dateValue As String = CStr(DateTimePicker1.Value)
Dim calendar = Microsoft.VisualBasic.Left(dateValue, 10)
para.ParameterName = "#company"
para.Value = company.Selected.ToString
para.ParameterName = "#division"
para.Value = division.Selected.ToString
para.ParameterName = "#date"
para.Value = DateTimePicker1.Value
para.ParameterName = "#supplier"
para.Value = supplier.Selected.ToString
para.ParameterName = "#material_group"
para.Value = materialGroup.Selected.ToString
para.ParameterName = "#cost"
para.Value = netprice.Selected.ToString
para.ParameterName = "#dsc"
para.Value = description.Selected.ToString
para.ParameterName = "#email"
para.Value = cmbEmail.SelectedValue
para.ParameterName = "#userID"
para.Value = id
para.ParameterName = "#Name"
para.Value = txtEmployee.Text
para.ParameterName = "#val"
para.Value = newName
para.ParameterName = "#quantity"
para.Value = quantity.Selected.ToString
para.ParameterName = "#wbs"
para.Value = wba.Selected.ToString
para.ParameterName = "#marketingCode"
para.Value = markCodes.Selected.ToString
para.ParameterName = "#hccNumber"
para.Value = hccNumber.Selected.ToString
para.ParameterName = "#currency"
para.Value = currency.Selected.ToString
Insert details:
Dim con As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
myCommand.Parameters.Add("#company", SqlDbType.NVarChar, 30).Value = company.Selected.ToString
myCommand.Parameters.Add("#division", SqlDbType.NVarChar, 30).Value = division.Selected.ToString
myCommand.Parameters.Add("#date", SqlDbType.NVarChar, 30).Value = DateTimePicker1.Value
myCommand.Parameters.Add("#supplier", SqlDbType.NVarChar, 30).Value = supplier.Selected.ToString
myCommand.Parameters.Add("#material_group", SqlDbType.NVarChar, 30).Value = materialGroup.Selected.ToString
myCommand.Parameters.Add("#cost", SqlDbType.NVarChar, 30).Value = netprice.Selected.ToString
myCommand.Parameters.Add("#dsc", SqlDbType.NVarChar, 30).Value = description.Selected.ToString
myCommand.Parameters.Add("#email", SqlDbType.NVarChar, 30).Value = cmbEmail.SelectedValue
myCommand.Parameters.Add("#userID", SqlDbType.NVarChar, 30).Value = id
myCommand.Parameters.Add("#Name", SqlDbType.NVarChar, 30).Value = newName
myCommand.Parameters.Add("#quantity", SqlDbType.NVarChar, 40).Value = quantity.Selected.ToString
myCommand.Parameters.Add("#wbs", SqlDbType.NVarChar, 40).Value = wba.Selected.ToString
myCommand.Parameters.Add("#marketingCode", SqlDbType.NVarChar, 40).Value = markCodes.Selected.ToString
myCommand.Parameters.Add("#hccNumer", SqlDbType.NVarChar, 40).Value = hccNumber.ToString
myCommand.Parameters.Add("#currency", SqlDbType.NVarChar, 40).Value = currency.Selected.ToString
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim sql As String = "Insert into details (company, division, date, supplier, material_group, cost, dsc, email, userID, fullName, marketingCode, wbs, qty, currency) VALUES (#company, #division, #date, #supplier, #material_group, #cost, #dsc, #email, #userID, #Name, #marketingCode, #wbs, #quantity, #currency)"
myCommand.Connection = con
myCommand.CommandText = sql
myAdapter.InsertCommand = myCommand
myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
You are storing if something is selected.
para.Value = company.Selected.ToString
Selected is a boolean!
I'm not certain on what you are trying to achieve here, but I don't think this script will work how you wish. You're not building a list of parameters with values, but replacing one parameter object over and over.
para.ParameterName = "#company"
para.Value = company.Selected.ToString
para.ParameterName = "#division"
para.Value = division.Selected.ToString
This will just first set the para object to be #company, and value: company.Selected.ToString, then set it again to be #division and value: division.Selected.ToString.
Your datatypes are all NVarChar and you are explicitly setting your boolean values to a string (.ToString()).
If you wish to store boolean values, change your data types to bit. Then change your values so you are passing the boolean value, not an explicitly converted boolean to string:
myCommand.Parameters.Add("#currency", SqlDbType.bit).Value = currency.Selected
company.selected returns true if the item is selected, and false if not...
You probably want to use company.ToString
Related
I have a SQL Lite database and I want to insert the time in a 24 hour system.
table
error msg
Dim com As New SQLiteCommand("INSERT INTO ROSY_TB(ROSY_CODE,ROSY_DESCREPTION,ROSY_FROM,ROSY_TO,ROSY_ALLOWFR,ROSY_ALLOWTO,ROSY_APSENTA,ROSY_APSENTB,ROSY_WORKHOUR,ROSY_APSENTNUM,ROSY_CHECK) VALUES (#ROSY_CODE,#ROSY_DESCREPTION,#ROSY_FROM,#ROSY_TO,#ROSY_ALLOWFR,#ROSY_ALLOWTO,#ROSY_APSENTA,#ROSY_APSENTB,#ROSY_WORKHOUR,#ROSY_APSENTNUM,#ROSY_CHECK)", con)
com.Parameters.AddWithValue("#ROSY_CODE", DbType.String).Value = ROSYUPDATE_F.LAB_CODEVAL.Text
com.Parameters.AddWithValue("#ROSY_DESCREPTION", DbType.String).Value = ROSYUPDATE_F.TEXT_DESCREPTION.Text
com.Parameters.AddWithValue("#ROSY_FROM", DbType.DateTime).Value = Format(ROSYUPDATE_F.DATE_FROM.Value, "HH:mm tt") 'ROSYUPDATE_F.DATE_FROM.Value.ToLongTimeString
com.Parameters.AddWithValue("#ROSY_TO", DbType.DateTime).Value = Format(ROSYUPDATE_F.DATE_TO.Value, "HH:mm tt")
com.Parameters.AddWithValue("#ROSY_ALLOWFR", DbType.Int64).Value = CInt(Val(ROSYUPDATE_F.TEXT_ALLOWFR.Text))
com.Parameters.AddWithValue("#ROSY_ALLOWTO", DbType.Int64).Value = CInt(Val(ROSYUPDATE_F.TEXT_ALLOWTO.Text))
com.Parameters.AddWithValue("#ROSY_APSENTA", DbType.String).Value = ROSYUPDATE_F.COM_APSENTA.SelectedItem
com.Parameters.AddWithValue("#ROSY_APSENTB", DbType.String).Value = ROSYUPDATE_F.COM_APSENTB.SelectedItem
com.Parameters.AddWithValue("#ROSY_WORKHOUR", DbType.Int64).Value = CInt(Val(ROSYUPDATE_F.TEXT_WORKHOUR.Text))
com.Parameters.AddWithValue("#ROSY_APSENTNUM", DbType.Int64).Value = CInt(Val(ROSYUPDATE_F.COUNTSPENT))
com.Parameters.AddWithValue("#ROSY_CHECK", DbType.Boolean).Value = True
con.Open()
com.ExecuteNonQuery()
con.Close()
VB.Net Code
' Getting Records Before Transfer to GL
Call OpenAccConnection(lblUserName.Text, lblPassword.Text)
Dim odcTotalsForTransferGL As OleDbCommand = New OleDbCommand("spPet_TotalsForTransferGL", conAccounts)
odcTotalsForTransferGL.CommandType = CommandType.StoredProcedure
' Parameter Assigning
Dim strCompanyCode As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#ComCod", OleDbType.VarChar, 2)
strCompanyCode.Direction = ParameterDirection.Input
Dim strLocationCode As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#LocCod", OleDbType.VarChar, 2)
strLocationCode.Direction = ParameterDirection.Input
Dim strPettyCashDate As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#PetDat", OleDbType.VarChar, 8)
strPettyCashDate.Direction = ParameterDirection.Input
Dim strBegVNo As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#BegVNo", OleDbType.Integer)
strBegVNo.Direction = ParameterDirection.Output
Dim strEndVNo As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#EndVNo", OleDbType.Integer)
strEndVNo.Direction = ParameterDirection.Output
Dim strVouTotal As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#VouTotal", OleDbType.Integer)
strVouTotal.Direction = ParameterDirection.Output
Dim decPetTotal As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#PetTotal", OleDbType.Decimal)
decPetTotal.Direction = ParameterDirection.Output
Dim intFinancialDates As OleDbParameter = odcTotalsForTransferGL.Parameters.Add("#FinancialDates", OleDbType.Integer)
intFinancialDates.Direction = ParameterDirection.Output
' Passing Parameters
' Company Code
strCompanyCode.Value = cboCompanyCode.SelectedItem.ToString.Substring(0, 2)
' Location Code
strLocationCode.Value = cboLocationCode.SelectedItem.ToString.Substring(0, 2)
' Petty Cash Date(Year & Month)
strPettyCashDate.Value = dtPettyCashDate.Value.Year.ToString + dtPettyCashDate.Value.Month.ToString("D2") + "01"
' Accounts Database Open
conAccounts.Open()
' Stored Procedure Process
Dim odrTotalsForTransferGL As OleDbDataReader = odcTotalsForTransferGL.ExecuteReader()
If odrTotalsForTransferGL.HasRows Then
Do While odrTotalsForTransferGL.Read
lblAccPeriod.Text = odrTotalsForTransferGL.GetValue(4).ToString.Substring(0, 4) + "/" + odrTotalsForTransferGL.GetValue(4).ToString.Substring(5, 4)
lblFiscalMonth.Text = odrTotalsForTransferGL.GetValue(4).ToString.Substring(9, 2)
lblBegVNo.Text = odrTotalsForTransferGL.GetValue(0).ToString
lblEndVNo.Text = odrTotalsForTransferGL.GetValue(1).ToString
lblPettyTotal.Text = odrTotalsForTransferGL.GetValue(3).ToString
Loop
End If
Stored Procedure
ALTER PROCEDURE [dbo].[spPet_TotalsForTransferGL]
-- Add the parameters for the stored procedure here
#ComCod as varchar(2),
#LocCod as varchar(2),
#PetDat as varchar(8),
#BegVNo as int OUT,
#EndVNo as int OUT,
#VouTotal as int OUT,
#PetTotal as decimal(12,2) OUT,
#FinancialDates as varchar(10) OUT
AS
BEGIN
SELECT MIN(PettyDetail.DPetVouNo),
MAX(PettyDetail.DPetVouNo),
MAX(PettyDetail.DPetVouNo) - MIN(PettyDetail.DPetVouNo),
ISNULL(SUM(PettyDetail.DPetAmount), 0)
FROM PettyDetail
WHERE (PettyDetail.DPetComCode = #ComCod) AND
(PettyDetail.DPetLocCode = #LocCod) AND
(YEAR(PettyDetail.DPetDate) = YEAR(CONVERT(Date,#PetDat,111))) AND
(MONTH(PettyDetail.DPetDate) = MONTH(CONVERT(Date,#PetDat,111)))
/* Getting Financial Dates */
EXECUTE #FinancialDates = dbo.fnApp_GetFinancialDates #PetDat
END
Scalar Function
ALTER FUNCTION [dbo].[fnApp_GetFinancialDates]
(
-- Add the parameters for the function here
#PetDat as varchar(8)
)
--RETURNS int(10)
RETURNS varchar(10)
AS
BEGIN
-- Declare the return variable here
--DECLARE #FinancialDates int(10)
DECLARE #FinancialDates varchar(10)
-- Add the T-SQL statements to compute the return value here
IF MONTH(CONVERT(date,#PetDat,111)) BETWEEN 4 AND 12
BEGIN
SELECT #FinancialDates = (SELECT
CAST((YEAR(CONVERT(date,#PetDat,111))) as varchar) +
CAST((YEAR(CONVERT(date,#PetDat,111)) + 1) as varchar) +
REPLICATE('0',(2-(LEN(CAST((MONTH(CONVERT(date,#PetDat,111)) - 3) as varchar))))) + (CAST((MONTH(CONVERT(date,#PetDat,111)) - 3) as varchar)))
END
ELSE
BEGIN
SELECT #FinancialDates = (SELECT
CAST((YEAR(CONVERT(date,#PetDat,111)) - 1)as varchar) +
CAST((YEAR(CONVERT(date,#PetDat,111))) as varchar) +
CAST((MONTH(CONVERT(date,#PetDat,111)) + 9) as varchar))
END
-- Return the result of the function
RETURN #FinancialDates
END
Above function #FinancialDates value didn't return to the .Net form. But other results are return to form.
Can any one please help me to solve this problem. Procedure & Function run correctly in Query Manager.
Two options :
Option 1 : change EXECUTE #FinancialDates = dbo.fnApp_GetFinancialDates #PetDat,
to :
SET #FinancialDates = dbo.fnApp_GetFinancialDates(#PetDat)
Option 2 : Include your fnApp_GetFinancialDates function in your SELECT statement (and you could just remove the #FinancialDates as varchar(10) OUT parameter statement.
ALTER PROCEDURE [dbo].[spPet_TotalsForTransferGL]
-- Add the parameters for the stored procedure here
#ComCod as varchar(2),
#LocCod as varchar(2),
#PetDat as varchar(8)
AS
BEGIN
SELECT MIN(PettyDetail.DPetVouNo) AS 'BegVNo',
MAX(PettyDetail.DPetVouNo) AS 'EndVNo',
MAX(PettyDetail.DPetVouNo) - MIN(PettyDetail.DPetVouNo) AS 'VouTotal',
ISNULL(SUM(PettyDetail.DPetAmount), 0) AS 'PetTotal'
dbo.fnApp_GetFinancialDates (#PetDat) AS 'FinancialDates'
FROM PettyDetail
WHERE (PettyDetail.DPetComCode = #ComCod) AND
(PettyDetail.DPetLocCode = #LocCod) AND
(YEAR(PettyDetail.DPetDate) = YEAR(CONVERT(Date,#PetDat,111))) AND
(MONTH(PettyDetail.DPetDate) = MONTH(CONVERT(Date,#PetDat,111)))
END
and for the option 2 VB code :
If odrTotalsForTransferGL.HasRows Then
Do While odrTotalsForTransferGL.Read
lblAccPeriod.Text = odrTotalsForTransferGL("FinancialDates").ToString.Substring(0, 4) + "/" + odrTotalsForTransferGL("FinancialDates").ToString.Substring(5, 4)
lblFiscalMonth.Text = odrTotalsForTransferGL("FinancialDates").ToString.Substring(9, 2)
lblBegVNo.Text = odrTotalsForTransferGL("BegVNo").ToString
lblEndVNo.Text = odrTotalsForTransferGL("EndVNo").ToString
lblPettyTotal.Text = odrTotalsForTransferGL("PetTotal").ToString
Loop
End If
edited : and don't forget to remove the following code because they are not necessary and will produce errors since the stored proc has no output parameters anymore:
Dim strBegVNo As OleDbParameter =
odcTotalsForTransferGL.Parameters.Add("#BegVNo", OleDbType.Integer)
strBegVNo.Direction = ParameterDirection.Output
Dim strEndVNo As OleDbParameter =
odcTotalsForTransferGL.Parameters.Add("#EndVNo", OleDbType.Integer)
strEndVNo.Direction = ParameterDirection.Output
Dim strVouTotal As OleDbParameter =
odcTotalsForTransferGL.Parameters.Add("#VouTotal", OleDbType.Integer)
strVouTotal.Direction = ParameterDirection.Output
Dim decPetTotal As OleDbParameter =
odcTotalsForTransferGL.Parameters.Add("#PetTotal", OleDbType.Decimal)
decPetTotal.Direction = ParameterDirection.Output
Dim intFinancialDates As OleDbParameter =
odcTotalsForTransferGL.Parameters.Add("#FinancialDates", OleDbType.Integer)
intFinancialDates.Direction = ParameterDirection.Output
To get started I created a program at work to read our network tools and store the retrieved data to our database. No problem but the way I set it up originally was every tool had its own instance and was hard coded for each tool.
Now I was tasked with making the program more user friendly so I can send it to other places. So now I database the IP address of tool and location on assembly line so I can cross reference while the code is executing.
On the new program I created an array of my Tool Reader class:
Dim TB(50) As ReadAtlascopco
Then I run through this loop on form load:
Private dict As New Dictionary(Of Timer, Label)()
Private Sub CreateTimers()
For i = 0 To IPCount
TB(i) = New ReadAtlascopco
' timer(i) = New Timerarray
Next
For i As Integer = 1 To IPCount
C = i - 1
timer = New Timer() With {.Interval = 250, .Enabled = True, .Tag = i.ToString}
AddHandler timer.Tick, (AddressOf timer_Tick)
' timer.Interval = (100 * i)
label = New Label()
label.Name = "label" & i
label.Location = New Point(10, 10 + i * 25)
label.Font = New Font("Sans Serif", 9, FontStyle.Bold)
label.AutoSize = True
'label.Width = 100
'label.AutoEllipsis = False
label.TabIndex = i
label.Visible = True
Me.Controls.Add(label)
dict(timer) = label
TB(C).ipaddress = TBData(C)
TB(C).StartConnection()
timer.Enabled = True
timer.Start()
Next
Then I go through the timer tick event. Everything works great up until this point. Once I go through the tick event it starts lagging and I miss data.
Is there a way to speed this process up?
Here is the tick event:
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
Dim Stop_Num(50) As String
Dim Line_Name(50) As String
Dim t As Timer = DirectCast(sender, Timer)
Dim s As Integer = Val(t.Tag) - 1
Param1 = TBData(s)
CWE(s) = False
If Activestat(s) = True Then
Dim SQL1 As String = "SELECT Stop_Num FROM TBL_Added_Boxes Where(IP_Address = N'" + Param1 + "')"
Dim output1 As String
Using cn = New SqlConnection(My.Settings.Torque_InfoConnectionString)
Using cmd = New SqlCommand(SQL1, cn)
cn.Open()
Try
Dim dr1 = cmd.ExecuteReader()
While dr1.Read()
output1 = dr1("Stop_Num").ToString()
End While
Catch ex As SqlException
WriteToErrorLog("Error pulling data from the database in reguards to torque box info (Stop Number).", ex.ToString(), "Failed to retreive Torque controler Info .")
End Try
cn.Close()
End Using
End Using
Stop_Num(0) = output1
Dim SQL2 As String = "SELECT Line_On FROM TBL_Added_Boxes Where(IP_Address = N'" + Param1 + "')"
Dim output2 As String
Using cn = New SqlConnection(My.Settings.Torque_InfoConnectionString)
Using cmd = New SqlCommand(SQL2, cn)
cn.Open()
Try
Dim dr2 = cmd.ExecuteReader()
While dr2.Read()
output2 = dr2("Line_On").ToString()
End While
Catch ex As SqlException
WriteToErrorLog("Error pulling data from the database in reguards to torque box info (Line On).", ex.ToString(), "Failed to retreive Torque controler Info .")
End Try
cn.Close()
End Using
End Using
Line_Name(0) = output2
If Line_Name(0) = "Line 1" Then
Newlinename(s) = "Line1Serial"
ElseIf Line_Name(0) = "Line 2" Then
Newlinename(s) = "Line2Serial"
ElseIf Line_Name(0) = "Line 3" Then
Newlinename(s) = "Line3Serial"
End If
Param3 = Stop_Num(0)
Param2 = Newlinename(s)
Dim SQL As String = "SELECT " + Newlinename(s) + " FROM TBL_RunningLineStatus Where(Stop_Number = N'" + Stop_Num(0) + "')"
Dim output As String
Using cn = New SqlConnection(My.Settings.ScanBypassConnectionString)
Using cmd = New SqlCommand(SQL, cn)
cn.Open()
Try
Dim dr = cmd.ExecuteReader()
While dr.Read()
output = dr(Param2).ToString()
End While
Catch ex As SqlException
WriteToErrorLog("Error Pulling Data From The Database In reguards to Seat Data.", ex.ToString(), "Failed to retreive buildline data.")
End Try
cn.Close()
End Using
End Using
TBSData(s) = output
If Killall = "True" Then
t.Stop()
Else
Try
TB(s).GetData()
If TB(s).Colorselect = 0 Then
dict(t).BackColor = System.Drawing.Color.GreenYellow
dict(t).ForeColor = Color.Black
ElseIf TB(s).Colorselect = 1 Then
TB(s).ipaddress = TBData(s)
TB(s).StartConnection()
dict(t).BackColor = System.Drawing.Color.Red
dict(t).ForeColor = Color.White
ElseIf TB(s).Colorselect = 2 Then
dict(t).BackColor = System.Drawing.Color.Purple
dict(t).ForeColor = Color.White
End If
If Olddata(s) <> TB(s).TorboxData(20) Then
Cname(s) = TB(s).TorboxData(2)
Tstatus(s) = TB(s).TorboxData(7)
TValue(s) = TB(s).TorboxData(19)
Sdata(s) = TBSData(s)
Tfinal(s) = TB(s).TorboxData(18)
TPset(s) = TB(s).TorboxData(30)
TFinalAngle(s) = TB(s).TorboxData(24)
RDAmin(s) = TB(s).TorboxData(25)
RDAmax(s) = TB(s).TorboxData(26)
RDAactual(s) = TB(s).TorboxData(27)
RDAStat(s) = TB(s).TorboxData(15)
Anglemin(s) = TB(s).TorboxData(21)
Anglemax(s) = TB(s).TorboxData(22)
AngleFT(s) = TB(s).TorboxData(23)
Anglestat(s) = TB(s).TorboxData(13)
Tormin(s) = TB(s).TorboxData(16)
Tormax(s) = TB(s).TorboxData(17)
TTTights(s) = TB(s).TorboxData(28)
TTSerial(s) = TB(s).TorboxData(29)
Olddata(s) = TB(s).TorboxData(20)
CWE(s) = True
dict(t).Text = t.Tag + " ) " + "Controler Name : " + TB(s).TorboxData(2) + ": Status : " + TB(s).Connectiontext + " : Date : " + Now() + " :: SERIAL # : " + Sdata(s) + " : From : " + Newlinename(s) + " :: Stop : " + Param3 + ""
' .Text = t.Tag + " ) " + "Controler Name : " + TB(s).TorboxData(2) + ": Status : " + TB(s).Connectiontext + " : Date : " + Now() + " :: SERIAL # : " + Sdata(s) + " : From : " + Newlinename(s) + " :: Stop : " + Param3 + "")
dict(t).BackColor = System.Drawing.Color.Yellow
Else
If TB(s).networkconnected = True Then
dict(t).AutoSize = True
'dict(t).Text = "Controler Name : " + TB(s).TorboxData(2) + ": Status : " + TB(s).Connectiontext + " : Date : " + Now()
dict(t).BackColor = System.Drawing.Color.Yellow
dict(t).ForeColor = Color.Black
Else
dict(t).Text = TB(s).Connectiontext + " :::: " + Now() + " :: " + TBData(s)
dict(t).BackColor = System.Drawing.Color.Red
dict(t).ForeColor = Color.White
End If
End If
Catch ex As Exception
WriteToErrorLog("Error Reading the torque box .", ex.Message, "Failed read torque data.")
Finally
'If CWE(s) = True Then
' Call Data_Entry()
'End If
If TValue(s) <> "" And CWE(s) = True Then
Try
con.ConnectionString = My.Settings.ScanBypassConnectionString
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO TBL_Torque_Value1 (Serial_Num, Controler_Name, Torque_Status, Torque_Value, Date_Time,Extra_1, Extra_2, Extra_3,RundownMin,RundownMax,RundownAct,RundownStat,AngleMin,AngleMax,AngleFT,AngleStat,TorqueMin,TorqueMax,ToolTightens,Toolserialnum) VALUES(#p1,#p2, #p3, #p4, #p5, #p6, #p7, #p8,#p9, #p10, #p11, #p12, #p13, #p14, #p15, #p16, #p17, #p18, #p19, #p20)"
cmd.Parameters.Add("#p1", SqlDbType.NVarChar, 50)
cmd.Parameters.Add("#p2", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p3", SqlDbType.NVarChar, 50)
cmd.Parameters.Add("#p4", SqlDbType.NVarChar, 50)
cmd.Parameters.Add("#p5", SqlDbType.DateTime2, 7)
cmd.Parameters.Add("#p6", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p7", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p8", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p9", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p10", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p11", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p12", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p13", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p14", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p15", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p16", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p17", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p18", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p19", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p20", SqlDbType.NVarChar, 250)
cmd.Parameters("#p1").Value = Sdata(s)
cmd.Parameters("#p2").Value = Cname(s)
cmd.Parameters("#p3").Value = Tstatus(s)
cmd.Parameters("#p4").Value = TValue(s)
cmd.Parameters("#p5").Value = Now()
cmd.Parameters("#p6").Value = Tfinal(s)
cmd.Parameters("#p7").Value = TFinalAngle(s) + " Deg"
cmd.Parameters("#p8").Value = "P:" + TPset(s)
cmd.Parameters("#p9").Value = RDAmin(s)
cmd.Parameters("#p10").Value = RDAmax(s)
cmd.Parameters("#p11").Value = RDAactual(s)
cmd.Parameters("#p12").Value = RDAStat(s)
cmd.Parameters("#p13").Value = Anglemin(s)
cmd.Parameters("#p14").Value = Anglemax(s)
cmd.Parameters("#p15").Value = AngleFT(s)
cmd.Parameters("#p16").Value = Anglestat(s)
cmd.Parameters("#p17").Value = Tormin(s)
cmd.Parameters("#p18").Value = Tormax(s)
cmd.Parameters("#p19").Value = TTTights(s)
cmd.Parameters("#p20").Value = TTSerial(s)
cmd.ExecuteNonQuery()
Catch ex As Exception
WriteToErrorLog("Error Inserting Data into The Database In reguards to Torque Data.", ex.Message, "Failed to Insert Torque Data.")
con.Close()
cmd.Parameters.Clear()
Cname(s) = ""
Tstatus(s) = ""
TValue(s) = ""
Sdata(s) = ""
Tfinal(s) = ""
TFinalAngle(s) = ""
TPset(s) = ""
RDAmin(s) = ""
RDAmax(s) = ""
RDAactual(s) = ""
RDAStat(s) = ""
Anglemin(s) = ""
Anglemax(s) = ""
AngleFT(s) = ""
Anglestat(s) = ""
Tormin(s) = ""
Tormax(s) = ""
TTTights(s) = ""
TTSerial(s) = ""
Finally
con.Close()
cmd.Parameters.Clear()
Cname(s) = ""
Tstatus(s) = ""
TValue(s) = ""
Sdata(s) = ""
Tfinal(s) = ""
TFinalAngle(s) = ""
TPset(s) = ""
RDAmin(s) = ""
RDAmax(s) = ""
RDAactual(s) = ""
RDAStat(s) = ""
Anglemin(s) = ""
Anglemax(s) = ""
AngleFT(s) = ""
Anglestat(s) = ""
Tormin(s) = ""
Tormax(s) = ""
TTTights(s) = ""
TTSerial(s) = ""
End Try
End If
End Try
End If
Else
dict(t).AutoSize = True
dict(t).Text = "Torque Box is not active :: " + TBData(s) + " ::"
dict(t).BackColor = System.Drawing.Color.Black
dict(t).ForeColor = Color.White
End If
End Sub
The class that is being referenced is one that I created and is working but like I said everything is slow. It seems to hang for 3 to 5 seconds in a single tick event.
As of now I only have 21 tools setup and it will take about 15 to 30 seconds to cycle through all.
I am new to visual basic database I am connecting to database using .sdf
i am getting the following error in visual studio 2010 when i run this function.
There was an error parsing the query. [ Token line number = 1, Token
line offset = 21, Token in eror =-]
Can any one please tell how to fix this error.
Private Sub Save_Record()
Dim con As SqlCeConnection = New SqlCeConnection
Dim cmd As New SqlCeCommand
Dim sSQL As String = String.Empty
con.ConnectionString = Get_Constring()
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.Text
Try
If Me.txtFirstName.Tag = 0 Then
sSQL = "INSERT INTO patients-2015 ( first_name, last_name, gender, phone_number, age, address, height, standing_bp, heart_rate, notes, reffered, disease, sub_diseases, registered_date, updated_date, prescribed_medicines)"
sSQL = sSQL & " VALUES ( #first_name, #last_name, #gender, #phone_number, #age, #address, #height, #standing_bp, #heart_rate, #notes, #reffered, #disease, #sub_diseases, #registered_date, #updated_date, #prescribed_medicines)"
cmd.CommandText = sSQL
Else
sSQL = "UPDATE patients-2015 SET first_name = #first_name, last_name = #last_name, gender = #gender, phone_number = #phone_number, age = #age, address = #address , height = #height, standing_bp = #standing_bp, heart_rate = #heart_rate, notes = #notes, reffered = #reffered, disease = #disease, sub_diseases = #sub_diseases, registered_date = #registered_date, standing_bp = #standing_bp, prescribed_medicines = #prescribed_medicines WHERE id = #id"
cmd.CommandText = sSQL
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.UInt64, .ParameterName = "#id"}).Value = Me.txtFirstName.Tag
End If
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#first_name"}).Value = IIf(Len(Trim(Me.txtFirstName.Text)) > 0, Me.txtFirstName.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#last_name"}).Value = IIf(Len(Trim(Me.txtLastName.Text)) > 0, Me.txtLastName.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#gender"}).Value = IIf(Not Me.ComboGender.Text = "Select", Me.ComboGender.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#phone_number"}).Value = IIf(Len(Trim(Me.TextPhoneNumber.Text)) > 0, Me.TextPhoneNumber.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#age"}).Value = IIf(Len(Trim(Me.txtAge.Text)) > 0, Me.txtAge.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#address"}).Value = IIf(Len(Trim(Me.txtAddress.Text)) > 0, Me.txtAddress.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#height"}).Value = IIf(Len(Trim(Me.txtHeight.Text)) > 0, Me.txtHeight.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#standing_bp"}).Value = IIf(Len(Trim(Me.txtBp.Text)) > 0, Me.txtBp.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#heart_rate"}).Value = IIf(Len(Trim(Me.txtHeartRate.Text)) > 0, Me.txtHeartRate.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#notes"}).Value = IIf(Len(Trim(Me.txtNotes.Text)) > 0, Me.txtNotes.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#reffered"}).Value = IIf(Len(Trim(Me.txtRefferer.Text)) > 0, Me.txtRefferer.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#disease"}).Value = IIf(Len(Trim(Me.ComboDisease.Text)) > 0, Me.ComboDisease.Text, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#sub_diseases"}).Value = IIf(Len(Trim(Me.CheckedList_SubTopic.SelectedItems.ToString)) > 0, Me.CheckedList_SubTopic.SelectedItems.ToString, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.DateTime, .ParameterName = "#registered_date"}).Value = IIf(Len(Trim(DateAndTime.Now.ToLongTimeString)) > 0, DateAndTime.Now.ToLongTimeString, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.DateTime, .ParameterName = "#updated_date"}).Value = IIf(Len(Trim(DateAndTime.Now)) > 0, DateAndTime.Now, DBNull.Value)
cmd.Parameters.Add(New SqlCeParameter With {.DbType = DbType.String, .ParameterName = "#prescribed_medicines"}).Value = IIf(Len(Trim(Me.txtPrescribedMedicines.Text)) > 0, Me.txtPrescribedMedicines.Text, DBNull.Value)
' Dim Result As Integer = cmd.ExecuteNonQuery
Dim Result As Integer = cmd.ExecuteNonQuery
'If the record is new then we have to get its ID so that we can edit it rightaway after the insertion.
If Me.txtFirstName.Tag = 0 Then
cmd.CommandText = "Select ##Identity"
'Set textbox tag property with the ID of new record
Me.txtFirstName.Tag = cmd.ExecuteScalar()
End If
If Result <> 1 Then
MessageBox.Show("Insert failed.")
Else
MessageBox.Show("Data has been saved.")
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
con.Close()
End Try
End Sub
Update code lines with [patients-2015] where you have written table name. For example below,
sSQL = "INSERT INTO [patients-2015] ( first_name, last_name, gender, phone_number, age, address, height, standing_bp, heart_rate, notes, reffered, disease, sub_diseases, registered_date, updated_date, prescribed_medicines)"
So you need to apply this change - everywhere in the code where you have written table name without [ ].
Hope this will solve your error.
i updated my code i added .Value = DBNull.Value in the lines
any suggestions regarding updating database that accepts null values on date and time textbox any recommendations/suggestions are highly appreciated
Dim cmd As SqlCommand = sqlconn.CreateCommand
sqlconn.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "UpdateEquipmentProfile"
'declare the variables'
cmd.Parameters.Add("#OE_ID", SqlDbType.VarChar, 11, "oeq-su-999").Value = DBNull.Value
cmd.Parameters.Add("#OE_Category", SqlDbType.Char, 3, "COM").Value = DBNull.Value
cmd.Parameters.Add("#OE_SubCategory", SqlDbType.Char, 3, "SU").Value = DBNull.Value
cmd.Parameters.Add("#OE_Name", SqlDbType.VarChar, 35, "adminpmis01").Value = DBNull.Value
cmd.Parameters.Add("#OE_User", SqlDbType.VarChar, 35, "Ivan").Value = DBNull.Value
cmd.Parameters.Add("#OE_Brand", SqlDbType.VarChar, 15, "DELL").Value = DBNull.Value
cmd.Parameters.Add("#OE_Model", SqlDbType.VarChar, 35, "optiplex").Value = DBNull.Value
cmd.Parameters.Add("#OE_Specs", SqlDbType.VarChar, 1000, "dualcore").Value = DBNull.Value
cmd.Parameters.Add("#OE_SerialNo", SqlDbType.VarChar, 35, "sgh5960").Value = DBNull.Value
cmd.Parameters.Add("#OE_PropertyNo", SqlDbType.VarChar, 35, "j7h7h6g6f2").Value = DBNull.Value
cmd.Parameters.Add("#OE_MacAddress", SqlDbType.VarChar, 100, "j7h7:h6g6f2").Value = DBNull.Value
cmd.Parameters.Add("#OE_Static_IP", SqlDbType.VarChar, 15, "192.168.1.5").Value = DBNull.Value
cmd.Parameters.Add("#OE_Vendor", SqlDbType.VarChar, 35, "ADWAYS").Value = DBNull.Value
cmd.Parameters.Add("#OE_PurchaseDate", SqlDbType.SmallDateTime).Value = DBNull.Value
cmd.Parameters.Add("#OE_WarrantyInclusiveYear", SqlDbType.Int).Value = DBNull.Value
cmd.Parameters.Add("#OE_WarrantyStatus", SqlDbType.Char, 2, "IN").Value = DBNull.Value
cmd.Parameters.Add("#OE_Status", SqlDbType.VarChar, 15, "Good").Value = DBNull.Value
cmd.Parameters.Add("#OE_Dept_Code", SqlDbType.Char, 3, "ADM").Value = DBNull.Value
cmd.Parameters.Add("#OE_Location_Code", SqlDbType.Char, 8, "ADM_OFC").Value = DBNull.Value
cmd.Parameters.Add("#OE_Remarks", SqlDbType.VarChar, 1000, "ACTIVE").Value = DBNull.Value
cmd.Parameters("#OE_ID").Value = txtOEID.Text
cmd.Parameters("#OE_Category").Value = cmbCategory.Text
cmd.Parameters("#OE_SubCategory").Value = cmbSubCategory.Text
cmd.Parameters("#OE_Name").Value = txtName.Text
cmd.Parameters("#OE_User").Value = txtUser.Text
cmd.Parameters("#OE_Brand").Value = cmbBrand.Text
cmd.Parameters("#OE_Model").Value = cmbModel.Text
cmd.Parameters("#OE_Specs").Value = txtSpecs.Text
cmd.Parameters("#OE_SerialNo").Value = txtSerialNo.Text
cmd.Parameters("#OE_PropertyNo").Value = txtPropertyNo.Text
cmd.Parameters("#OE_MacAddress").Value = txtMacAddress.Text
cmd.Parameters("#OE_Static_IP").Value = txtStaticIp.Text
cmd.Parameters("#OE_Vendor").Value = txtVendor.Text
cmd.Parameters("#OE_PurchaseDate").Value = txtPurchaseDate.Text
cmd.Parameters("#OE_WarrantyInclusiveYear").Value = txtWarrantyInclusiveYear.Text
cmd.Parameters("#OE_WarrantyStatus").Value = txtWarrantyStatus.Text
cmd.Parameters("#OE_Status").Value = txtStatus.Text
cmd.Parameters("#OE_Dept_Code").Value = cmbDeptCode.Text
cmd.Parameters("#OE_Location_Code").Value = cmbLocationCode.Text
cmd.Parameters("#OE_Remarks").Value = txtRemarks.Text
cmd.ExecuteNonQuery()
MsgBox("Successfully Updated Equipment Profile")
sqlconn.Close()
1.First of all if you have not set that DateTime field's allow null property,set it to allow null,so that it accepts null.
2.Then in your UI code dont pass the parameter if Date is not entered in Textbox.
3.Then in your code for passing parameter value check if the parameter value is null the pass that parameter's value as as DBNull.Value.