1
I can’t get bank values and direct a form according to what "profile" is in the users table, if I take the part of ExecuteReader
, works the login, however, I need to check the user profile, my code is like this:
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 PesadaoFinal
{
public partial class frmLogin : Form
{
SqlConnection conn = null;
private string conexao = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Breno\Documents\Visual Studio 2013\Projects\PesadaoFinal\PesadaoFinal\bd\pesadao_db.mdf;Integrated Security=True;Connect Timeout=30";
private string consulta = string.Empty;
public frmLogin()
{
InitializeComponent();
}
public void logar(){
conn= new SqlConnection(conexao);
try{
string usuario, senha, cargo;
usuario = txtUsu.Text;
senha=txtSenha.Text;
SqlDataReader verCargo = null;
String usulogado, cpf;
if(usuario==string.Empty && senha==string.Empty){
MessageBox.Show("Usuário e senha devem ser digitados!");
}
else if (usuario != null && senha != null)
{
consulta = @"SELECT * FROM funcionarios WHERE login = @usuario AND senha = @senha";
SqlCommand novoLogin = new SqlCommand(consulta, conn);
novoLogin.Parameters.Add("@usuario", SqlDbType.VarChar).Value = usuario;
novoLogin.Parameters.Add("@senha", SqlDbType.VarChar).Value = senha;
conn.Open();
object rs = novoLogin.ExecuteScalar();
if (rs == null)
{
MessageBox.Show("Usuário ou Senha Incorretos");
}
else
{
verCargo = novoLogin.ExecuteReader(CommandBehavior.CloseConnection);
if (verCargo != null)
{
while (verCargo.Read())
{
usulogado = (verCargo[2].ToString);
cpf = (verCargo[1].ToString);
}
}
}
}
}catch(SqlException erroBD){
MessageBox.Show(erroBD +"Erro no banco");
}
}
private void frmLogin_Load(object sender, EventArgs e)
{
}
private void btnLogar_Click(object sender, EventArgs e)
{
logar();
}
}
}
And the mistake I have is this:
>Error 1 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method? c:\users\breno\documents\visual studio 2013\Projects\PesadaoFinal\PesadaoFinal\Form1.cs 67 46 PesadaoFinal
You know that your code can leave the connection open if any exceptions occur?
– Maniero
Didn’t know, this is good or bad? How should I treat this?
– brenoguto
Bad. It’s something long to explain in a comment. I think I should open a question about it. I think it’s missing in the website. Pole only the
logar()
.– Maniero