Conversion failed to convert varchar 'No' value to tinyint data type

Asked

Viewed 1,367 times

3

I am trying to solve a patch error of converting a variable created by me to store data that is in an Excel sheet.

The error happens when I try to insert the data that is in this variable in SQL Server, ie in (query).

code:

variable declaration:

string valmoradafiscaligualmoradalocal = "";

information search code:

case 47://coluna 21

if (((WS.Cells[linha, Contcoluna] as Excel.Range).Value) != null)
{
    valmoradafiscaligualmoradalocal = Convert.ToString((WS.Cells[linha, Contcoluna] as Excel.Range).Value);  
}
continue;

insert code in sql server:

cmd.CommandText = "INSERT tabela_sacc (Morada_Fiscal_Igual_a_Morada_Local) VALUES ('" + valmoradafiscaligualmoradalocal.ToString() + "')";

connection.Open();
cmd.Connection = connection;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
connection.Close();
valgrupo = null;
  • It’s really not possible to convert a nay or a yes for a tinyint, there is no function for this, what can be done is a case or a function that does this. in any case your question is not clear enough to make a good answer.

2 answers

0

  • good afternoon, is not an integer is a string more concretely the word (no).

  • What is the type of the column Address_fiscal_igual_a_address_local and what is the value that is in valmoradafiscaligualalocal by what I am seeing is a conversion to boleano, wouldn’t be the case ? some people use the tinyint type to represent the Boolean in sql server instead of the bit type, and it seems your data is retouched the word No, if that’s the case you will have to do an if: valmoradafiscaligualalocal ="No"? 0:1;

  • From now on I want to thank you for your help , I’m very new in programming and as such I’m having a hard time understanding what you want me to do in if? The variable valmoradafiscaligualaloca will contain the word Not that is correct. But the goal is not to convert to boleano , but rather string

  • Could this be it? string variavelauxiliar = ""; if (variavelauxiliar == null) { valmoradafiscaligualalocal = variavelauxiliar; } cmd.Commandtext = " INSERT tabela_sacc (Address_fiscal_igual_a_address_local) VALUES ('" + variavelauxiliar + "')";

  • It did not work I am entering the null value not the content of the variable valmoradafiscaligualalocal

  • But that is that you may be mistaken, you can not convert the word No to string if the type in your database is not varchar or nvarchar, your conversion within the sql query has to be to a type similar to that of the database, for this reason. In the case it seems to be a tinyint that accepts values from 0 to 255 different from a bit that only accepts 0 and 1.

  • I understand but my column in the database accepts varchar(50) should not be because of it. I appreciate if it helps ma if it is very useful!

  • @Diogogomes, you commented that your column is varchar(50), but there must be something incorrect, because the error message is from conversion to tinyint, can confirm the correct field type Morada_Fiscal_Igual_a_Morada_Local at the bank?

Show 3 more comments

0

Friend, try to make your code using sql Parameter, follow example

 var serverName = "LAPTOP-EVEJOG4C";
        var databaseName = "MyDatabase";
        var dataString = "Não";
        using (SqlConnection conn = new SqlConnection($"Server={serverName};Database={databaseName};Trusted_Connection=True;"))
        {
            conn.Open();
            string sql = "insert Example01 (DadoTinyInt) values (@DadoTinyInt)";

            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.Parameters.Add("@DadoTinyInt", System.Data.SqlDbType.TinyInt).Value =
                    dataString == "Não" ? 0 : 1;
                cmd.ExecuteNonQuery();
            }
        }
  • I’m realizing where you want to go but I can’t do it in your suggested way, because the variable valmoradafiscaligualalocal can be any other string for example (yes or maybe) Grateful for the understanding.

  • So instead of declaring it as tinyint declare as nvarchar cmd.Parameters.Add("@Given", System.Data.Sqldbtype.Nvarchar, 50). Value = its value

  • I will still have the same problem, because the value will be different if I use another excel sheet ! I have to do this for another 172 columns is not feasible to do it that way you suggested.I appreciate the availability

Browser other questions tagged

You are not signed in. Login or sign up in order to post.