Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi friends,

I am trying to update single record in access table but following error encounters

Conversion from string "UPDATE LeaveMaster SET From_Date" to type long string is not valid

Please help

What I have tried:

LvMasterCmd.CommandText = "UPDATE LeaveMaster SET From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LesveType WHERE Sap_No= '" & TxtSapID.Text And "Where MNT= '" & CmbMonth.Text And "Where YR= '" & CmbYear.Text

'Sap_No = @Sap_No,

Try
' LvMasterCmd.Parameters.AddWithValue("@Sap_No", CInt(TxtSapID.Text))
LvMasterCmd.Parameters.AddWithValue("@From_Date", DtpFrom.Value.ToString)
LvMasterCmd.Parameters.AddWithValue("@To_Date", DtpTo.Value.ToString)
LvMasterCmd.Parameters.AddWithValue("@LeaveType", CmbLeaveType.Text)
LvMasterCmd.Parameters.AddWithValue("@Days", LblDays.Text)
LvMasterCmd.Parameters.AddWithValue("@LeaveType", "Y")

LvMasterReader.Close()

LvMasterCmd.ExecuteNonQuery()
LvMasterCmd.Parameters.Clear()
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Posted
Updated 23-Jul-18 7:17am

You have already used parameters in the SET clause. To correctly define the WHERE clause you should also use parameters in it.

At the moment you have two WHERE clauses. Instead of that you shoudl have only one. In other words:

VB
LvMasterCmd.CommandText = "UPDATE LeaveMaster SET From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LesveType WHERE Sap_No= @sapno And MNT= @month And YR=@year"
 
Share this answer
 
Comments
J_Mohan 23-Jul-18 14:59pm    
Thanks friend, I got it and I will try it.
J_Mohan 23-Jul-18 15:09pm    
I have Updated syntax as per your instructions:

LvMasterCmd.CommandText = "UPDATE LeaveMaster SET From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LeaveStatus WHERE Sap_No= @Sap_No AND MNT = @Mnt And YR = @yr"

It Shows "Data type mismatch in criteria expression"
Wendelius 23-Jul-18 16:57pm    
Data type mismatch happens if the type of the column is not the same as the type of the parameter. Re-check all the parameters and their types compared to the corresponding column types. For example, could it be that the columns are numeric in the database? If that would be the case, define the parameters numeric as well.
The basic error in your SQL is that the additional conditions are just "and" and not "And where"

The scratch-my-head problem is why the first half of your query uses parameters and the second does not. THIS NEEDS TO BE FIXED!

Part 1 will be to fix your WHERE clauses:
VB
LvMasterCmd.CommandText = "UPDATE LeaveMaster SET From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LesveType WHERE Sap_No= @SapNo AND MNT = @Mnt And YR = @yr"


And Part 2 will be to add the new parameters:
VB
LvMasterCmd.Parameters.AddWithValue("@From_Date", DtpFrom.Value.ToString)
LvMasterCmd.Parameters.AddWithValue("@To_Date", DtpTo.Value.ToString)
LvMasterCmd.Parameters.AddWithValue("@LeaveType", CmbLeaveType.Text)
LvMasterCmd.Parameters.AddWithValue("@Days", LblDays.Text)
LvMasterCmd.Parameters.AddWithValue("@LeaveType", "Y")

' added params
LvMasterCmd.Parameters.AddWithValue("@Sap_No", TxtSapID.Text)
LvMasterCmd.Parameters.AddWithValue("@MNT", CmbMonth.Text )
LvMasterCmd.Parameters.AddWithValue("@YR", CmbYear.Text)
 
Share this answer
 
Comments
J_Mohan 23-Jul-18 14:59pm    
Thanks friend, I got it and I will try it.
J_Mohan 23-Jul-18 15:10pm    
I have Updated syntax as per your instructions:

LvMasterCmd.CommandText = "UPDATE LeaveMaster SET From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LeaveStatus WHERE Sap_No= @Sap_No AND MNT = @Mnt And YR = @yr"

It Shows "Data type mismatch in criteria expression"
MadMyche 23-Jul-18 17:32pm    
You may need to match the data-types between your application and the SQL table; the Parameters.AddWithValue() method will cast based on the the value passed in; @SAP_NO could possible be an INTeger

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900