Hi,

I have a question regarding my vb.net code. I have a button called 'Add New Item', when the user clicks on that button, the text fields should be clear. I tried this code 'txtAddNewItem.clear() and this code too 'txtAddNewItem.Text = "" ' , but I am getting an error, which reads as follows 'Data type mismatch in criteria expression'. Does anyone know how to fix this issue? Thanks..

Hi,

Here is the code:

Private Sub btnAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click


Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\nwind.mdb;User Id=admin;Password=;")
Dim myCommand As New OleDbCommand
Dim mySQL As New StringBuilder

Try

myConnection.Open()

txtCustomerID.Text = ""
txtCustomerName.Text = ""
txtTelephoneNumber.Text = ""

mySQL.Append("Insert into Cusotmers ")
mySQL.Append("(CustomerID, CustomerName, Telephone) ")
mySQL.Append("values (@CustomerID, @ CustomerName, @Telephone)")

myCommand.Connection = myConnection
myCommand.CommandType = CommandType.Text
myCommand.CommandText = mySQL.ToString()

Dim parCustomerID As New OleDbParameter("@CustomerID", txtCustomerID.Text)
Dim parCustomerName As New OleDbParameter("@CustomerName", txtCustomerName.Text)
Dim parTelephone As New OleDbParameter("@Telephone", txtTelephoneNumber.Text)

myCommand.Parameters.Add(parCustomerID)
myCommand.Parameters.Add(parCustomerName)
myCommand.Parameters.Add(parTelephone)

myCommand.ExecuteNonQuery()

myConnection.Close()

Catch ex As Exception
MsgBox(ex.Message)
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Try

txtCustomerID.Text = ""
txtCustomerName.Text = ""
txtTelephoneNumber.Text = ""

why you put this code on your add New item event??
it will make none data to insert in database cause you have to clear them before you add them into db.
if you want to clear textbox make a new button to handle this.
ex:

Private Sub btnClearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearForm.Click
txtCustomerID.Text = ""
txtCustomerName.Text = ""
txtTelephoneNumber.Text = ""
End Sub

So erase that codes from your input button.

Comments
good explain

Hi,

Well, on clicking 'add new item', it should clear the textfields, so that the user can input their new details.

Hi,

Then how would I go about this? See, the concept is when I do a search, the info will be populated onto all the textboxes and if I tried to add a new item after that, the data from the search is still there and I want that to be cleared when I tried to add a new item.

so make a new button to clear all textbox like my suggestion on my previous post..

Private Sub btnClearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearForm.Click
txtCustomerID.Text = ""
txtCustomerName.Text = ""
txtTelephoneNumber.Text = ""
End Sub

Thanks for your help!

I just thought maybe I could do it on just one button, but I guess it doesn't work... Once again, thanks for your help .. really appreciate it:)

Hi,

I have a quick question.. regarding my data.. when I make the customerID as Number in the access database, it is prompting a error 'data type mismatch in criteria expression' however, when I make that to string in the database, it is working fine.. why is that? Is there something wrong with my code that I had posted previously because this error only occurs when I tried to add new item.

yes its make error.
you have same type input and database.
if your database is number you must insert number.
try this following code :

Dim parCustomerID As New OleDbParameter("@CustomerID", trim(CInt(txtCustomerID.Text)))

Hi,

When I tried to add your code, it reads an error saying 'Option strict On disallows implicit conversions from 'Integer' to 'String'.

yeah option strict on will not allowed you to do this, cause Option Strict is On, you have to turn it off.
Put this code on top of all code (REALLY NOT RECOMMENDED) :

Option Strict Off

the better way is input the current value. if your database column is numeric type you must add numeric input.

This question has already been answered. Start a new discussion instead.