Operand type clash: text is incompatible with date

J L 26 Reputation points
2022-11-25T17:49:17.467+00:00

I have a form binded to a database table. One of the fields in the table is "DATE" and it is binded to a DateTimePicker tool in the form. The binded property is "Value" (not "Text").
The problem comes when I try to save the record. I get the message "System.Data.SqlClient.SqlException: 'Operand type clash: text is incompatible with date". I tried getting the DateTimePicker tool directly from the"DATE" field in the Data Sources window, but still had the same problem.

Thanks for any help.

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Sreeju Nair 12,666 Reputation points
    2022-11-25T18:12:37.493+00:00

    How you are adding the data to the database? Are you executing the SQL Query, or are you using the entity framework for this? if you are using SQL query to update the database with parameters, try to specify the type like below.

    commandObject.Parameters.Add("@YourParameterName", SqlDbType.Date).Value = Value;

    For the above statement to work, the Value must be a DateTime object; if not, you must convert it. Refer https://learn.microsoft.com/en-us/dotnet/standard/base-types/parsing-datetime to see how you can convert a string to date time.

    Hope this helps


  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-11-25T20:42:35.62+00:00

    Sounds like you are using a TableAdapter.

    Let's say by DataSet is named dataSet1 and the BindingSource is named personBindingSource to get at values we use

    private void CurrentButton_Click(object sender, EventArgs e)  
    {  
        DataSet1.PersonRow current = dataSet1.Person[personBindingSource.Position];  
        MessageBox.Show($"{current.FirstName} {current.LastName}\n{current.CreatedBy1:d}");  
    }  
    

    Here is a screenshot of a typically route for TableAdapter and note the BindingNavigator is only available with my custom BindingNavigator for .NET Core projects.

    public class CoreBindingNavigator : BindingNavigator  
    {  
        public CoreBindingNavigator()  
        {  
            AddStandardItems();  
        }  
    }  
    

    264353-adapter.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.