Error while running project in Visual Studio

Asked

Viewed 1,047 times

2

When I run a project in Visual Studio it shows the following error:

A first chance Exception of type 'System.Data.Sqlclient.Sqlexception' occurred in System.Data.dll

Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Logar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conexao = "Data Source=(local);Initial Catalog=Cadastro;User ID=sa;Password=m4st3r!";
            SqlConnection add = new SqlConnection(conexao);
            SqlCommand comado = new SqlCommand("select count (*) Usuario where Usuario =@user and Senha=@senha", add);

            comado.Parameters.Add("@user", SqlDbType.VarChar).Value = textBox1.Text;
            comado.Parameters.Add("@senha", SqlDbType.VarChar).Value = textBox2.Text;

            add.Open();
            int i = (int) comado.ExecuteScalar();
            if (i > 0)
            {
                MessageBox.Show("Login e senha encontrado");
            }
            else
            {
                MessageBox.Show("Erro");
            }
            add.Close();
        }
    }
}

What mistake is this?

  • Have you tried to continue running to see if something else happens? This usually happens as a warning that something is wrong but can still run.

  • 1

    Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

3

This message is showing that there was an exception but it was manipulated by the component you are using. Most of the time you don’t have to do anything and it won’t happen when you’re not thrashing the application. It’s just a warning and you can continue running smoothly.

You can disable this in Visual Studio. Some ways to do this:

  • Right click on the Output Window weaning Exception Messages. (Source)
  • There is a configuration in the menu Debug > Exceptions. Abra Common Language Runtime Exceptions, afterward System and clear user-handled exceptions that you do not want to be shown. (Source)

Blog that better explains the functioning of this type of message.

1

Probably the unconventional error message is caused by the problem pointed out by @Maniero, but there is a problem in your query:

select count (*) Usuario where Usuario =@user and Senha=@senha

Missing clause from

select count (*) from Usuario where Usuario =@user and Senha=@senha
  • Really was missing the from, thank you so much! =)

Browser other questions tagged

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