1

I have created a product table to put features of product and when I run the code shown below, I get an error

System.InvalidCastException: Byte cannot converted to Byte[]

What is wrong with that code?

int m_syscode = 10;
string m_code1 = "mcode1";
string m_name = "mcode1";
string m_shortname = "mcode1";
string parentcode = "mcode1";
byte m_abstract = 1;
string category = "mcode1";
byte is_active = 0;

string sql = $"INSERT INTO PRODUCT (M_SYSCODE, M_CODE, M_NAME, M_SHORTNAME, M_PARENTCODE, M_ABSTRACT, M_CATEGORY, IS_ACTIVE) VALUES(@m_syscode, @m_code1, @m_name, @m_shortname, @parentcode, @m_abstract, @category, @is_active )";

SqlCommand command = new SqlCommand(sql, connection);

command.Parameters.Add("@m_syscode", System.Data.SqlDbType.Int, 4).Value = m_syscode;
command.Parameters.Add("@m_code1", System.Data.SqlDbType.VarChar, 15).Value = m_code1;
command.Parameters.Add("@m_name", System.Data.SqlDbType.VarChar, 25).Value = m_name;
command.Parameters.Add("@m_shortname", System.Data.SqlDbType.VarChar, 10).Value = m_shortname;
command.Parameters.Add("@parentcode", System.Data.SqlDbType.VarChar, 15).Value = parentcode;
command.Parameters.Add("@m_abstract", System.Data.SqlDbType.Binary, 1).Value = m_abstract;
command.Parameters.Add("@category", System.Data.SqlDbType.VarChar, 12).Value = category;
command.Parameters.Add("@is_active", System.Data.SqlDbType.Binary, 1).Value = is_active;

int result = command.ExecuteNonQuery();

return result.ToString();

Also here is my Product table in db :IMAGE

1
  • 1
    Btw: allowing nulls on all those columns seems... unlikely. Especially "IS_ACTIVE" and "M_CODE". Commented Nov 8, 2020 at 10:56

2 Answers 2

3

Short version: binary(1) (which is a binary chunk of exactly one byte length, so: one byte, the hard way) looks to be a terrible choice here. It looks like what you want is bit (0 or 1), but if you really meant 0-255, then: tinyint is probably what you wanted.

The binary type (fixed or variable length) represents binary payloads, so it not surprising that the provider would expect types like byte[] for the value.

Sign up to request clarification or add additional context in comments.

Comments

0

is_active means true or false then I think your idea is bit datatype

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.