The data types text and varchar are incompatible in the equal to operator

Total Post:110

Points:774
C# 
C#
 3172  View(s)
Ratings:
Rate this:

I have used text as a datatype for device name. while adding the data into the form, it throws an error : The data types text and varchar are incompatible in the equal to operator

try
{
    cmd = new SqlCommand("insert into license1 values(@l_id,@customer_id,@d_id,@device_name,@from,@to)", cn);
    cmd.Parameters.AddWithValue("@l_id", license_id.Text);
    cmd.Parameters.AddWithValue("@customer_id", c_comboBox4.Text);
    cmd.Parameters.AddWithValue("@d_id", d_id_comboBox4.Text);
    cmd.Parameters.AddWithValue("@device_name", d_name_comboBox5.Text);
    cmd.Parameters.AddWithValue("@to", DateTime.Parse(date_to.Text));
    cmd.Parameters.AddWithValue("@from", DateTime.Parse(date_from.Text));
    cn.Open();
    a = cmd.ExecuteNonQuery();
    if (a > 0)
    {
        MessageBox.Show("Data Submitted");
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

  1. Post:412

    Points:2888
    Re: The data types text and varchar are incompatible in the equal to operator

    Hi Jayden,


    I would try to change this line

    cmd.Parameters.Add("@device_name", SqlDbType.Text).Value = d_name_comboBox5.Text;

    The AddWithValue treats the string values as a NVarChar datatype and looking at error message a Text type is expected.

Answer