I'm updating my MS Access db via VB code:
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim query As String = "UPDATE Employee SET EmpName = #EmpName , MobNo = #MobNo , Email_ID = #Email , EmpAdd = #EmpAdd , FatName = #FatName , Sex = #Sex , Pan = #Pan , AccNo = #AccNo , BankName = #BankName , MICR = #MICR , BasicPay = #BasicPay , passwd = #passwd , DOB = #DOB WHERE id = #id "
Try
Call OpenConn()
Dim cmd As New OleDbCommand(query, conn)
cmd.Parameters.Add("#MobNo", OleDbType.VarWChar, 13, "MobNo").Value = txtMobNo.Text
cmd.Parameters.Add("#Email", OleDbType.VarWChar, 50, "Email_ID").Value = txtEmail.Text
cmd.Parameters.Add("#EmpName", OleDbType.VarWChar, 255, "EmpName").Value = txtName.Text
cmd.Parameters.Add("#DOB", OleDbType.VarWChar, 10, "DOB").Value = txtDOB.Value
cmd.Parameters.Add("#EmpAdd", OleDbType.VarWChar, 255, "EmpAdd").Value = txtAddress.Text
cmd.Parameters.Add("#FatName", OleDbType.VarWChar, 255, "FatName").Value = txtFatName.Text
cmd.Parameters.Add("#Sex", OleDbType.VarWChar, 6, "Sex").Value = cboSex.Text
cmd.Parameters.Add("#Nationality", OleDbType.VarWChar, 30, "Nationality").Value = txtNationality.Text
cmd.Parameters.Add("#Pan", OleDbType.VarWChar, 20, "Pan").Value = txtPan.Text
cmd.Parameters.Add("#AccNo", OleDbType.VarWChar, 20, "AccNo").Value = txtAccNo.Text
cmd.Parameters.Add("#BankName", OleDbType.VarWChar, 50, "BankName").Value = txtBankName.Text
cmd.Parameters.Add("#MICR", OleDbType.VarWChar, 20, "MICR").Value = txtMICR.Text
cmd.Parameters.Add("#DedRate", OleDbType.VarWChar, 3, "DedRate").Value = txtDedRate.Text
cmd.Parameters.Add("#BasicPay", OleDbType.VarWChar, 10, "BasicPay").Value = txtBasicPay.Text
'cmd.Parameters.AddWithValue("#CL", Convert.ToInt64(txtNoOfCl.Text))
'cmd.Parameters.AddWithValue("#ML", Convert.ToInt64(txtNoOfMl.Text))
cmd.Parameters.Add("#passwd", OleDbType.VarWChar, 20, "passwd").Value = txtPasswd.Text
cmd.Parameters.Add("#id", OleDbType.BigInt, 3, "id").Value = Val(txtId.Text)
cmd.Prepare()
cmd.ExecuteNonQuery()
Call CloseConn()
MsgBox("Data Updated Successfully", vbInformation, "Update")
DisableTxt(Me)
btnUpdate.Visible = False
btnEdit.Enabled = False
btnEdit.Visible = False
ClearTxt(Me)
Catch ex As Exception
MsgBox("Fatal error occured :" & vbNewLine & "source : " & vbNewLine & ex.Source & vbNewLine & "messege :" & vbNewLine & ex.ToString)
End Try
End Sub
But update is not working. Any ideas?
With OleDB you can't use named parameters. You have to use ? as a placeholder and the order of the parameters is significant.
See here: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx
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()
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.
Im using Visual Studio .NET 2003 the add button works fine when all textbox, combobox fields are filled with data, but upon testing not filling the fields with data leaving it NULL it returns error saying "String was not recognized as a valid DateTime"
I have a textbox named txtPurchasedDate
My Stored procedure
CREATE PROCEDURE AddOfficeEquipmentProfile
(
#OE_ID varchar(11) = NULL,
#OE_Category char(3) = NULL,
#OE_SubCategory char(3) = NULL,
#OE_Name varchar(35) = NULL,
#OE_User varchar(35) = NULL,
#OE_Brand varchar(15) = NULL,
#OE_Model varchar(35) = NULL,
#OE_Specs varchar(1000) = NULL,
#OE_SerialNo varchar(35) = NULL,
#OE_PropertyNo varchar(35) = NULL,
#OE_MacAddress varchar(100) = NULL,
#OE_Static_IP varchar(15) = NULL,
#OE_Vendor varchar(35) = NULL,
#OE_PurchaseDate smalldatetime = NULL,
#OE_WarrantyInclusiveYear int = NULL,
#OE_WarrantyStatus char(2) = NULL,
#OE_Status varchar(15) = NULL,
#OE_Dept_Code char(3) = NULL,
#OE_Location_Code char(8) = NULL,
#OE_Remarks varchar(1000) = NULL
)
AS
INSERT INTO tblOfficeEquipmentProfile (OE_ID, OE_Category, OE_SubCategory, OE_Name, OE_User, OE_Brand, OE_Model, OE_Specs, OE_SerialNo,
OE_PropertyNo, OE_MacAddress, OE_Static_IP, OE_Vendor, OE_PurchaseDate, OE_WarrantyInclusiveYear, OE_WarrantyStatus, OE_Status, OE_Dept_Code,
OE_Location_Code, OE_Remarks )
VALUES (#OE_ID, #OE_Category, #OE_SubCategory, #OE_Name, #OE_User, #OE_Brand, #OE_Model,
#OE_Specs, #OE_SerialNo, #OE_PropertyNo, #OE_MacAddress, #OE_Static_IP, #OE_Vendor, #OE_PurchaseDate, #OE_WarrantyInclusiveYear, #OE_WarrantyStatus,
#OE_Status, #OE_Dept_Code, #OE_Location_Code, #OE_Remarks)
IF ##ERROR<>0
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
ELSE
BEGIN
COMMIT TRANSACTION
RETURN 1
END
GO
My Vb.net Add Button Code
Dim cmd As SqlCommand = sqlconn.CreateCommand
sqlconn.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "AddOfficeEquipmentProfile"
cmd.Parameters.Add("#OE_ID", SqlDbType.VarChar, 11, "oeq-su-999")
cmd.Parameters.Add("#OE_Category", SqlDbType.Char, 3, "COM")
cmd.Parameters.Add("#OE_SubCategory", SqlDbType.Char, 3, "SU")
cmd.Parameters.Add("#OE_Name", SqlDbType.VarChar, 35, "adminpmis01")
cmd.Parameters.Add("#OE_User", SqlDbType.VarChar, 35, "Ivan")
cmd.Parameters.Add("#OE_Brand", SqlDbType.VarChar, 15, "DELL")
cmd.Parameters.Add("#OE_Model", SqlDbType.VarChar, 35, "optiplex")
cmd.Parameters.Add("#OE_Specs", SqlDbType.VarChar, 1000, "dualcore")
cmd.Parameters.Add("#OE_SerialNo", SqlDbType.VarChar, 35, "sgh5960")
cmd.Parameters.Add("#OE_PropertyNo", SqlDbType.VarChar, 35, "j7h7h6g6f2")
cmd.Parameters.Add("#OE_MacAddress", SqlDbType.VarChar, 100, "j7h7:h6g6f2")
cmd.Parameters.Add("#OE_Static_IP", SqlDbType.VarChar, 15, "192.168.1.5")
cmd.Parameters.Add("#OE_Vendor", SqlDbType.VarChar, 35, "ADWAYS")
cmd.Parameters.Add("#OE_PurchaseDate", SqlDbType.SmallDateTime)
cmd.Parameters.Add("#OE_WarrantyInclusiveYear", SqlDbType.Int)
cmd.Parameters.Add("#OE_WarrantyStatus", SqlDbType.Char, 2, "IN")
cmd.Parameters.Add("#OE_Status", SqlDbType.VarChar, 15, "Good")
cmd.Parameters.Add("#OE_Dept_Code", SqlDbType.Char, 3, "ADM")
cmd.Parameters.Add("#OE_Location_Code", SqlDbType.Char, 8, "ADM_OFC")
cmd.Parameters.Add("#OE_Remarks", SqlDbType.VarChar, 1000, "ACTIVE")
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 Added Equipment Profile")
sqlconn.Close()
If you want to store null values, you have to use conditional logic in your vb code to either not send those parameters to your stored procedure, or send a value of dbnull. Even your char fields are storing empty strings, which might not be your intent.
Use
If (Not String.IsNullOrEmpty(txtPurchaseDate.Text)) Then
cmd.Parameters("#OE_PurchaseDate").Value = txtPurchaseDate.Text
End If
Why not use DateTimePicker for your PurchaseDate instead of a textbox in txtPurchaseDate.Text you could name your control dtpPurchaseDate then you have:
cmd.Parameters("#OE_PurchaseDate").Value = dtpPurchaseDate.Value
The Default Value (or the Minimum Value) for DateTime Picker is 1/1/1980 so you could initialize the value of dtpPurchaseDate to 1/1/1980 like:
dtpPurchaseDate = System.DateTime.Parse("1/1/1980")
So, which means by the way when stored in your database it has the value of 1/1/1980 and not NULL.
If you want to purse with the Textbox then you could check if empty then assign a minimum date value which is actually 1/1/0001 like this:
If (If (String.IsNullOrEmpty(txtPurchaseDate.Text)) Then
cmd.Parameters("#OE_PurchaseDate").Value = DBNull.Value
Else
Dim tempPurchaseDate as DateTime
System.DateTime.TryParse(txtPurchaseDate.Text, tempPurchaseDate)
cmd.Parameters("#OE_PurchaseDate").Value = tempPurchaseDate
End If
But again it has a stored value in your database as 1/1/0001
You could display it empty in your TextBox like:
Given you have a DataRow named dr
If (dr["OE_PurchaseDate"] = System.DateTime.Parse("1/1/0001")) Then
txtPurchaseDate.Text = ""
Else
txtPurchaseDate.Text = dr["OE_PurchaseDate"]
End If
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.
Dim cmd As SqlCommand = sqlconn.CreateCommand
sqlconn.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "udpateELSEinsertEquipmentProfile"
'declare the variables
cmd.Parameters.Add("#OE_ID", SqlDbType.VarChar, 11, "oeq-su-999")
cmd.Parameters.Add("#OE_Category", SqlDbType.Char, 3, "COM")
cmd.Parameters.Add("#OE_SubCategory", SqlDbType.Char, 3, "SU")
cmd.Parameters.Add("#OE_Name", SqlDbType.VarChar, 35, "adminpmis01")
cmd.Parameters.Add("#OE_User", SqlDbType.VarChar, 35, "Ivan")
cmd.Parameters.Add("#OE_Brand", SqlDbType.VarChar, 15, "DELL")
cmd.Parameters.Add("#OE_Model", SqlDbType.VarChar, 35, "optiplex")
cmd.Parameters.Add("#OE_Specs", SqlDbType.VarChar, 1000, "dualcore")
cmd.Parameters.Add("#OE_SerialNo", SqlDbType.VarChar, 35, "sgh5960")
cmd.Parameters.Add("#OE_PropertyNo", SqlDbType.VarChar, 35, "j7h7h6g6f2")
cmd.Parameters.Add("#OE_MacAddress", SqlDbType.VarChar, 100, "j7h7:h6g6f2")
cmd.Parameters.Add("#OE_Static_IP", SqlDbType.VarChar, 15, "192.168.1.5")
cmd.Parameters.Add("#OE_Vendor", SqlDbType.VarChar, 35, "ADWAYS")
cmd.Parameters.Add("#OE_PurchaseDate", SqlDbType.SmallDateTime)
cmd.Parameters.Add("#OE_WarrantyInclusiveYear", SqlDbType.Int)
cmd.Parameters.Add("#OE_WarrantyStatus", SqlDbType.Char, 2, "IN")
cmd.Parameters.Add("#OE_Status", SqlDbType.VarChar, 15, "Good")
cmd.Parameters.Add("#OE_Dept_Code", SqlDbType.Char, 3, "ADM")
cmd.Parameters.Add("#OE_Location_Code", SqlDbType.Char, 8, "ADM_OFC")
cmd.Parameters.Add("#OE_Remarks", SqlDbType.VarChar, 1000, "ACTIVE")
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 Added Equipment Profile")
sqlconn.Close()
My insert and update stored procedures are working when there is no null values, but how about when I need to make some columns really null values?
I'm using SQL Server stored procedures in vb.net 2003
Any suggestions about codes example
You can use DBNull.Value as the parameter to your stored procedure. Ensure that your table allows null value for the fields.
For example if you wanted OE_Brand to be null you can do:
cmd.Parameters("#OE_Brand").Value = DBNull.Value
Setting it to Nothing will suppress the parameter. Use DBNull.Value instead:
cmd.Parameters("#OE_Remarks").Value = DBNull.Value