Click here to Skip to main content
15,886,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim conn As New OleDb.OleDbConnection
        Dim connStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\teejay.accdb;Persist Security Info=False"
        Dim cmd As New OleDb.OleDbCommand

        Try
            conn.ConnectionString = connStr
            conn.Open()
            cmd.Connection = conn

            cmd.CommandText = "INSERT INTO Statistic ([Date],[Bowen],[Nursing],[Bowen1],[Nursing1],[Total Bowen],[Total Nursing]) " & _
                         "VALUES ([@Date],[@Bowen],[@Nursing],[@Bowen1],[@Nursing1],[@Total Bowen],[@Total Nursing]); "
           
            cmd.Parameters.AddWithValue("@Date", DateDateTimePicker.Text)
            cmd.Parameters.AddWithValue("@Bowen", BowenTextBox.Text)
            cmd.Parameters.AddWithValue("@Nursing", NursingTextBox.Text)
            cmd.Parameters.AddWithValue("@Bowen1", Bowen1TextBox.Text)
            cmd.Parameters.AddWithValue("@Nursing1", Nursing1TextBox.Text)
            cmd.Parameters.AddWithValue("@Total Bowen", Total_BowenTextBox.Text)
            cmd.Parameters.AddWithValue("@Total Nursing", Total_NursingTextBox.Text)

            cmd.ExecuteNonQuery()
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class


What I have tried:

i dont know how to make this error go away
Posted
Updated 2-Aug-17 0:43am
v2

1 solution

Start with looking at what you are passing, and the column data types you are saving them into.
This one:
C#
cmd.Parameters.AddWithValue("@Date", DateDateTimePicker.Text)
should almost certainly be a DATE, DATETIME, or DATETIME2 column - if it isn't your database needs fixing quick - so don't pass the Text property from the DateTimePicker, pass the Value property instead. That way, the DTP is converted to a string using the default culture for the PC, just so it can be converted back to a date time by SQL, using a (potentially very different) default culture.

Similarly, if any of your other columns are not VARCHAR or NVARCHAR, use the various TryParse methods on your TextBoxes to convert them to "proper" native types (reporting problems back to the user) and pass the native values instead.

Your problem should go away.
 
Share this answer
 
Comments
Member 13341316 2-Aug-17 6:33am    
i have done all you suggested but am still getting the same error, please is it possible for me to send the project to you, so you can help me rectify the problem. thanks
OriginalGriff 2-Aug-17 6:44am    
I formatted your question so it's easier to read - try removing the spaces from the parameter names - I don't think those are allowed.

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