i need help because i dont why this is null
if u need smt (sql or smt ab my visual code lmk please)
28 Replies
I don't know what listar is but it's returning null
ToList?
1 second
it supposed to find the user that I have already created in SQL
so the Documento is Document It should be "161616" and contraseña is password it should be "123"
but for some reason he can't find it
is listar() your own function?
yes
show the code for that
the code is in spanish because english is no my main language
so I'm sorry for not knowing how to ask very well but it's really difficult to ask something in a language you don't understand
1 second
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Entidad;
namespace Datos
{
public class D_Usuario
{
public List<Usuario> listar () {
List<Usuario> lista = new List <Usuario>();
using (SqlConnection oconexion = new SqlConnection(Conexion.cadena)) {
try {
string query = "Select Id_usuario,Documento,Nombre_Completo,correo,Clave,Estado from usuario";
SqlCommand cmd = new SqlCommand(query, oconexion);
cmd.CommandType = CommandType.Text;
oconexion.Open();
using (SqlDataReader dr = cmd.ExecuteReader()) {
while(dr.Read()) {
lista.Add(new Usuario()
{
Id_usuario = Convert.ToInt32(dr["Id_usuario"]),
Documento = dr["Documento"].ToString(),
Nombre_Completo = dr["Nombre_Completo"].ToString(),
correo = dr["correo"].ToString(),
Clave =dr["clave"].ToString(),
Estado = Convert.ToBoolean(dr["Estado"])
});
}
}
}
catch (Exception ex) {
lista = new List<Usuario>();
}
}
return lista;
}
}
}
the program is supposed to work with the sql database
oh which line did you break on? It looks like line 29, which is before that code's executed
The code is made in capes so it is a little difficult
i got 4 capes
im gonna show u the conecction cape
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 Negocio;
using Entidad;
namespace ProyectoBD
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnCancelar_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnIngresar_Click(object sender, EventArgs e)
{
List<Usuario> PRUEBA = new N_Usuario().listar();
Usuario osuario = new N_Usuario().listar().Where(u => u.Documento == txtdocumento.Text && u.Clave == txtclave.Text).FirstOrDefault();
if(osuario != null ) {
Inicio form = new Inicio();
form.Show();
this.Hide();
form.FormClosing += Frm_closing;
}else
{
MessageBox.Show("No se encontre el usuario","Mensaje",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
private void Frm_closing(object sender , FormClosingEventArgs e) {
txtdocumento.Text = "";
txtclave.Text = "";
this.Show();
}
}
}
this is the full code of that picture
sure
I need to see that red/yellow icon that's cut off
set the breakpoint on line 32 instead, and hit that
i did put the problem is
the program doesnt recognize the data base i think
great
what's the error when that happens
because the idea is u can login with this user
but when you try to put that it comes out null (so it doesn't recognize it) I guess there must be something wrong in the "Where" but I don't know what it is
and thats what im asking why when u put this user and this password says "null"
oh I see
remove the try/catch here
then your program will crash with the real error message
sure
im little confuse idk if u can just send me that i need to remove
it should be looks like this https://gyazo.com/4863ad940a14e5930124ec292172af21
Gyazo
Gyazo
but idk what i did wrong
remove the try/catch and the catch statement. I won't give you the exact code because then you don't learn anything
https://gyazo.com/0889d72ca63baef68474b665b61cc570 where is the error i think
Gyazo
Gyazo
yes, that's the error you need to solve