The data types text and varchar are incompatible i

2019-08-26 01:31发布

问题:

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:

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.

You could also try to change your column datatype to nvarchar(MAX). The TEXT columns are considered obsolete and will probably removed in future versions of Sql Server



回答2:

try to change the data type of the device_name column to varchar and retest your app