I tried with the code below it doesn't work, is there something wrong with my code, please guide me. or if there is another solution
Thanks
Private Sub Btnsave_Click(sender As Object, e As EventArgs) Handles Btnsave.Click
If txtWCSKILL.Text = "" Or txtWCEXPERIENCE.Text = "" Or txtWCAPPEARANCE.Text = "" Or txtWCEDUCATION.Text = "" Then
MessageBox.Show("WSKILL,WEXPERIENCE,WAPPEARANCE,WEDUCATION cannot be empty.", "Report Status",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtWCSKILL.Focus()
Exit Sub
End If
Dim count As Integer
Using cmd As OleDbCommand = con.CreateCommand()
cmd.CommandText = "Select COUNT(*) FROM tblWeightingCriteria"
con.Open()
If count = CInt(cmd.ExecuteScalar() > 1) Then
MessageBox.Show("One Record Already Exist!", "Report Status",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
con.Close()
Exit Sub
Else
Try
con.Open()
cmd.CommandText = "INSERT INTO tblWeightingCriteria" & "([WCSKILL],[WCEXPERIENCE],[WCAPPEARANCE],[WCEDUCATION]) " & "VALUES(@WCSKILL,@WCEXPERIENCE,@WCAPPEARANCE,@WCEDUCATION)"
cmd.Parameters.AddRange(New OleDbParameter() {
New OleDbParameter("@WCSKILL", DbNullOrStringValue(txtWCSKILL.Text)),
New OleDbParameter("@WCEXPERIENCE", DbNullOrStringValue(txtWCEXPERIENCE.Text)),
New OleDbParameter("@WCAPPEARANCE", DbNullOrStringValue(txtWCAPPEARANCE.Text)),
New OleDbParameter("@WCEDUCATION", DbNullOrStringValue(txtWCEDUCATION.Text))})
Dim result = cmd.ExecuteNonQuery()
If result = 0 Then
MsgBox("No Data has been Inserted!")
Else
MsgBox("New Data is Inserted succesfully!")
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information)
End Try
End If
End Using
con.Close()
End Sub


Several points to consider.
You have this line:
countis of typeIntegerandCInt(cmd.ExecuteScalar() > 1)returns aBooleanvalue, so obviously you have theOption Strictset toOffand the implicit conversions are allowed. If you turn the optionOn, you'll get the BC30512 error:To avoid getting unexpected results and errors like this, make sure to turn this and other on/off options on by code:
Or through the project's properties:
Project->YourProjectName Properties->Compile.and turn the
Explicit,Strict, andInferOn. I'm sure your debugger will start complaining and report some errors to fix. At least, consider doing this in your next project.The
COUNT(*)orCOUNT(TheIdField)query returns thenrows/records of the given table. If the table is empty, then you'll get0for sure. So, if you want to allow inserting just one record:Note, don't keep the data access objects (
OleDbConnection,OleDbCommand,OleDbDataAdapter, ...etc) in class fields. Create them in the CRUD methods inUsingblock. This way, you don't need to explicitly close a connection or dispose of a disposable object. TheUsingstatement will do that for you.Separate your code and create specialized methods.
To create a connection:
For the
CRUDoperations and helpers methods:Note, the OLE DB provider does not support named parameters. Use
?as placeholders and add the parameters in the same order of the database fields as shown above.Now your
savecaller should look like this:Also, consider implementing the n-tier or a similar purpose architecture. Have the UI or the presentation in a layer/tier in one project. The data access in another layer/tier (DAL) and project. And a middle business logic layer (BLL) to communicate between them.
Further reading and examples.