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.
Related
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'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
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
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
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