Failed to convert nvarchar'VINET' value to int data type

Asked

Viewed 1,041 times

0

I have that mistake:

Falha ao converter o nvarchar valor 'VINET' para o tipo de dados int.

Down with my code:

private void button1_Click(object sender, EventArgs e)
        {
            try 
            {
                SqlConnection dataConnection = new SqlConnection();

                dataConnection.ConnectionString = ConfigurationSettings.AppSettings["consBanco"].ToString();

                string sql = "SELECT * " +
                              "FROM Orders WHERE CustomerID = @CustomerIdParam";

                dataConnection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = sql;
                cmd.Connection = dataConnection;
                cmd.CommandType = CommandType.Text;

                //SqlParameter param = new SqlParameter("@CustomerIdParam", SqlDbType.Char, 5);
                //param.Value = customerID;
                //cmd.Parameters.Add(param);
                cmd.Parameters.AddWithValue("@CustomerIdParam", 1);

                // cria o dataadapter...
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;

                // preenche o dataset...
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);

                grdTeste.DataSource = dataSet;
                grdTeste.DataMember = dataSet.Tables[0].TableName;


            }
            catch (SqlException ex)
            {

            }
        }
  • 1

    Only with this information is difficult to help. Where is giving this error. There is no VINET in your code, where does it come from? You know this catch You’re just making it hard to figure out what the mistake is? Here is a start and a number of links (p/ C#) to better understand the operation of exceptions (not so specific p/ Java): http://answall.com/questions/21933/comond-trata-exce%C3%A7%C3%B5es-em-java/21939#21939

  • Is this mistake weird, is it in Portuguese? Anyway, this VINET that you don’t have in your code is a text (nvarchar in the SQL nomenclature or string in C#) and where the error is waiting for a int, then you have to convert it probably with int.Parse(VINET.Text), but it is an assumption since there is not enough information. Or you can make a CAST in SQL but doubt that’s what you need.

  • When I try to load the grid, it gives the wrong message in catch. Not the slightest idea where this error is happening. What I want is to fill a grid. I couldn’t do it with Datareader and so I used Sqldataadapter and I don’t know if that’s it.

  • You have no idea why you’re killing the mistake with the catch. Did you read what I gave you? I know, it’s a lot, but if you don’t understand how exceptions work and keep using them wrong, your life is gonna get complicated. And if even you do not know what is happening with your application, random people on the internet have even less chance to know. I am waiting for you to give better information. Or you are waiting for someone who has had an identical problem that can help you. Nothing else appears on the screen, just this error message?

2 answers

1

It was solved as follows. I put a Executereader and then resoloveu. Another thing, the comic book column was wrong, it was nchar and so it was giving the error of VINET. I was wrong for lack of attention.

0

The error occurs because your grdTeste is waiting for a column called VINET, represented by a whole, but that the column of the bank is represented by a nvarchar or some similar type of data.

Change the column VINET of grid for String that the problem will be solved.

Browser other questions tagged

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