cancel
Showing results for 
Search instead for 
Did you mean: 

Implicit conversion from datatype 'VARCHAR' to 'SMALLINT' is not allowed.

Former Member
0 Kudos
4,585

Trying to link Visual Studio C# project combo box to Sybase database. The error pops up when I run the Visual Studio C# code.

ERROR [42000] [Sybase][ODBC Driver][Adaptive Server Enterprise]Implicit conversion from datatype 'VARCHAR' to 'SMALLINT' is not allowed. Use the CONVERT function to run this query.

See below code: Field1 is smallint, Field3 is varchar(255), there is nothing more, why so many error 42000? However, there are some other fields in the table:

  • Field1: smallint

  • Field2: varchar(255)

  • Field3: varchar(255)

  • Field4: char(2)

  • Field5: smallint

  • Field6: tinyint

  • Field7: smallint

Thanks.

Error Sceenshot: https://drive.google.com/open?id=1T5zy6jcasnVYMGfshgzx7WPYfR_6477F


OdbcConnection Cn = new OdbcConnection("ConnectionString");

Cn.Open();

string Query = "SELECT 'Field1(' + Field1 + ') : ' + Field3 As ComboBoxDisplayItem FROM TableName";

OdbcCommand cmd = new OdbcCommand(Query, Cn);

OdbcDataReader dr = cmd.ExecuteReader();

Accepted Solutions (0)

Answers (2)

Answers (2)

Breck_Carter
Participant

> Adaptive Server Enterprise

This is a forum for SQL Anywhere, previously known as Adaptive Server Anywhere, which is vastly different from Adaptive Server Enterprise.

johnsmirnios
Participant

It looks like you are using Adaptive Server Enterprise (ASE). This forum is for SQLAnywhere.

Former Member
0 Kudos

The problem how to fix my query? Thanks.

johnsmirnios
Participant

Well, I don't know ASE semantics since that's a whole other database product but it would seem that you build a query such as:

SELECT 'Field1(' + Field1 + ') : ' + Field3 As ComboBoxDisplayItem FROM TableName

So, you have an expression which is varchar + smallint + varchar + varchar(255). The first operand is a varchar with the value 'Field1(' The second operand is a smallint is the value of Field1 from a given row etc.

So, either all operands need to go to strings and be concatenated (assuming ASE supports '+' as a concatenation operator -- not sure) or they need to all go to smallint and be added arithmetically. I'm guessing ASE is doing the latter and trying to convert the string 'Field1(' to a smallint. It's also trying to convert '): ' to a smallint. The question is... what are you actually trying to accomplish? I'm guessing you want all strings? If so, try using CAST() (if ASE supports it) or Convert() to cast Field3 to a varchar.