Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Update Button is not working data type mismatch error is pop up

What I have tried:

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Try
Dim str As String
cn.Open()
str = "Update [Contact] set [QuotesId] ='" & txtquotesid.Text & "',[Date] ='" & date1.Text & "',[Customer Name] ='" & txtname.Text & "',[Company] ='" & txtcompany.Text & "',[Address] ='" & txtaddress.Text & "',[Town] ='" & txttown.Text & "',[State] ='" & txtdstate.Text & "',[Zip] ='" & txtzip.Text & "',[Mobile] ='" & txtmobile.Text & "',[Phone] ='" & txtphone.Text & "',[Ext] ='" & txtext.Text & "',[Email] ='" & txtemail.Text & "',[StartDate] ='" & startdate.Text & "',[EndDate] ='" & enddate.Text & "',[OrAdd] ='" & txtoadd.Text & "',[OrTown] ='" & txtotown.Text & "',[OrState] ='" & txtostate.Text & "',[OrZip] ='" & txtozip.Text & "',[OrTime] ='" & starttime.Text & "',[DestAdd] ='" & txtdadd.Text & "',[DestTown] ='" & txtdtown.Text & "',[DestState] ='" & txtdstate.Text & "',[DestZip] ='" & txtdzip.Text & "',[DestTime] ='" & endtime.Text & "',[Notes] ='" & txtnotes.Text & "',[Price] ='" & txtprice.Text & "',[Price Day] ='" & txtdayprice.Text & "' WHERE ([QuotesId] = '" & txtquotesid.Text & "')"
cmd = New OleDbCommand(str, cn)
i = cmd.ExecuteNonQuery()
If i >= 0 Then
MsgBox("Data Modified")
Else
MsgBox("Sorry Something Wrong")
End If
ds.Clear()
da.Fill(ds, "Contact")
Get_data()
Catch ex As Exception
MsgBox(ex.Message)
End Try
cn.Close()
End Sub
End Class
Posted
Updated 6-Sep-17 1:44am

Check if every field is a string or not.

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 
v2
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The chances are that doing that will actually fix your problem as well - since what it is complaining about is that the type of data you gave it doesn't fit in that column. By checking and converting user input to standard types rather than assuming the user is always right - and he is far from that, just like us all - you catch the errors before they reach the DB and mess you up later if they aren't caught as an SQL error as they are being now.
 
Share this answer
 

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