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();

asked 18 Mar, 10:16

VAer's gravatar image

VAer
20113
accept rate: 0%

edited 18 Mar, 10:26


> Adaptive Server Enterprise

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

permanent link

answered 18 Mar, 10:24

Breck%20Carter's gravatar image

Breck Carter
29.4k487653960
accept rate: 20%

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

permanent link

answered 18 Mar, 10:24

John%20Smirnios's gravatar image

John Smirnios
10.0k385129
accept rate: 38%

The problem how to fix my query? Thanks.

(18 Mar, 10:27) VAer
Replies hidden
1

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.

(18 Mar, 10:46) John Smirnios
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×27

question asked: 18 Mar, 10:16

question was seen: 421 times

last updated: 18 Mar, 10:46