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
Related
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
I have inserted two rows with the same content of a table still the code for searching existing data is not returning the desired result.
This is my VB.Net code:
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Try
If DateTimePicker1.Value.Date >= DateTime.Parse(TxtDateFrom.Text) And DateTimePicker1.Value.Date <= DateTime.Parse(TxtDateTo.Text) Then
Dim cmd1 As New SqlCommand
cmd1 = New SqlCommand("check_sched", connection)
cmd1.CommandType = CommandType.StoredProcedure
Dim rd As SqlDataReader
cmd1.Parameters.AddWithValue("#empid", Label17.Text)
cmd1.Parameters.AddWithValue("#datesched", DateTimePicker1.Value.Date)
cmd1.Parameters.AddWithValue("#timefrom", TimePicker1.Text)
cmd1.Parameters.AddWithValue("#timeto", TimePicker1.Text)
rd = cmd1.ExecuteReader()
If rd.HasRows Then
MsgBox("Employee already assigned a job for the particular time period", MsgBoxStyle.Information, "Change Time Period")
Else
Dim cmd As New SqlCommand
cmd = New SqlCommand("insert_schedule1", connection)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#actid", SqlDbType.Int).Value = Label2.Text
cmd.Parameters.Add("#activity", SqlDbType.NVarChar).Value = CmbActivity.Text
cmd.Parameters.Add("#serviceid", SqlDbType.Int).Value = Label14.Text
cmd.Parameters.Add("#servicename", SqlDbType.NVarChar).Value = TxtServiceType.Text
cmd.Parameters.Add("#pdatefrom", SqlDbType.NVarChar).Value = TxtDateFrom.Text
cmd.Parameters.Add("#pdateto", SqlDbType.NVarChar).Value = TxtDateTo.Text
cmd.Parameters.Add("#deptname", SqlDbType.NVarChar).Value = Label16.Text
cmd.Parameters.Add("#emp", SqlDbType.NVarChar).Value = CmbEmp.Text
cmd.Parameters.Add("#empid", SqlDbType.Int).Value = Label17.Text
cmd.Parameters.Add("#datesched", SqlDbType.DateTime).Value = myDate1
cmd.Parameters.Add("#timefrom", SqlDbType.Time).Value = TimePicker1.Value.TimeOfDay
cmd.Parameters.Add("#timeto", SqlDbType.Time).Value = TimePicker2.Value.TimeOfDay
cmd.Parameters.Add("#status", SqlDbType.Int).Value = "1"
cmd.ExecuteNonQuery()
MsgBox("Scheduled Successfully", MsgBoxStyle.Information, "Task Schedule")
CmbActivity.Text = ""
TxtServiceType.Text = ""
TxtDateFrom.Text = ""
TxtDateTo.Text = ""
'CmbSactivity.Text = ""
CmbEmp.Text = ""
DateTimePicker1.Text = Now
TimePicker1.Text = TimeOfDay
TimePicker2.Text = TimeOfDay
End If
Else
MsgBox("Please select a date within the duration", MsgBoxStyle.Information, "Invalid Date")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Can anyone help me find the error?
This is my stored procedure
create procedure check_sched
#empid int,
#datesched datetime,
#timefrom datetime,
#timeto datetime
as
begin
SELECT *
FROM tbl_schedule
WHERE empid = #empid
AND datesched = #datesched
AND timefrom >= #timefrom
AND timeto <= #timeto
end
Table schema:
CREATE TABLE [dbo].[tbl_schedule]
(
[actid] [int] NULL,
[activity] [nvarchar](350) NULL,
[serviceid] [int] NULL,
[servicename] [nvarchar](50) NULL,
[pdatefrom] [nvarchar](50) NULL,
[pdateto] [nvarchar](50) NULL,
[sactid] [int] NULL,
[sactivity] [nvarchar](500) NULL,
[deptname] [nvarchar](150) NULL,
[emp] [nvarchar](150) NULL,
[empid] [int] NULL,
[datesched] [datetime] NULL,
[timefrom] [datetime] NULL,
[timeto] [datetime] NULL,
[actimefrom] [datetime] NULL,
[actimeto] [datetime] NULL,
[completed] [int] NULL,
[status] [int] NULL
) ON [PRIMARY]
Your check schedule uses the TimePicker1 value for both #timeFrom and #timeTo fields, while your insert uses TimePicker1 and TimePicker2 respectively. This is causing your check to look at the wrong dates.
To avoid this issue in the future and to make it easier to find these bugs, try refactoring the names to match what they are for (such as fromTimePicker and toTimePicker).
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
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