this is the structure of my project
DataAccess Project
ConnectionToSql class here is my connection to the db
namespace DataAccess
{
public abstract class ConnectionToSql
{
private readonly string connectionString;
public ConnectionToSql() {
connectionString = "Server=******; Password=*******; user=sa; DataBase=Tareas";
}
protected SqlConnection GetConnection() {
return new SqlConnection(connectionString);
}
}
}
class UserDao in this class I call the procedures or the queries but when I bring the ListarProyectos procedure it doesn't bring it to me in the data dataGridView I don't know very well in which layer to put the code so that it brings me the data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Common.Cache;
namespace DataAccess
{
public class UserDao: ConnectionToSql
{
public bool Login(string user, string pass)
{
using (var connection = GetConnection()) {
connection.Open();
using (var command = new SqlCommand()) {
command.Connection = connection;
command.CommandText = "select *from Users where LoginName=@user and Password=@pass";
command.Parameters.AddWithValue("@user", user);
command.Parameters.AddWithValue("@pass", pass);
command.CommandType = CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read()) {
UserLoginCache.IdUser = reader.GetInt32(0);
UserLoginCache.FirstName = reader.GetString(3);
UserLoginCache.LastName = reader.GetString(4);
UserLoginCache.Position = reader.GetString(5);
UserLoginCache.Email = reader.GetString(6);
}
return true;
}
else
return false;
}
}
}
public DataTable ListarProyectos()
{
using (var connection = GetConnection())
{
connection.Open();
using (var command = new SqlCommand())
{
DataTable Tabla = new DataTable();
command.Connection = connection;
command.CommandText = "ListarProyectos";
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
Tabla.Load(reader);
reader.Close();
return Tabla;
}
}
}
}
}
Domain Project
Class UserModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess;
namespace Domain
{
public class UserModel
{
UserDao userDao = new UserDao();
public bool LoginUser(string user, string pass) {
return userDao.Login(user, pass);
}
}
}
Project Presentation
From the FormProject form I am calling the ListProject procedure but it does not bring me anything in the dataGridView in which layer the code has to go or what am I doing wrong
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 DataAccess;
namespace ProyectoTareas
{
public partial class FormProyectos : Form
{
public FormProyectos()
{
InitializeComponent();
}
private void FormProyectos_Load(object sender, EventArgs e)
{
ListarProyectos();
}
private void ListarProyectos()
{
UserDao objtarea = new UserDao();
dataGridView1.DataSource = objtarea.ListarProyectos();
}
}
}
Try this in your List Projects method