2

I have 2 tables in a database. One is with Users and and another one with Societies. Something like this:

Users table:                Societies table:
ID   User   Pass            ID   UserID   Societies
1    Mark   123             1      1      Pepsi
2    John   abc             2      1      Lays
3    Paul   a1b             3      2      Unilever
                            4      3      Nestle

UserID have a foreign key to ID from Users table.

And 2 forms: A Login panel for users and one to show in a Datagridview societies.

Now, I try to figure it out, when I'm logging in with a user, to show only Societies with the specific UserID for that user.

I made this method in Second Form to show specific societies, but I don't know what to put in Equals brackets. I tried a lot of possibilities, but nothing worked...

private void btnLoad_Click(object sender, EventArgs e)
{
   DCApp db = DCApp.NewDC();

   User logedUser = db.Users.FirstOrDefault(s => s == Program._us);

   List<Society> ListSociety = db.Societies.Where(s => s.UserID.Equals(logedUser)).ToList();

   _bs.DataSource = ListSociety;
   dgwUser.DataSource = _bs;
   dgwUser.Refresh();
   _bs.ResetBindings(false);    
}

If you have some tips, please, give me a path.

Rest of my code:

Program.cs

using System;
using System.Windows.Forms;

namespace Users
{
 static class Program
 {
  public static User _us;
  [STAThread]
  static void Main()
  {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   frmLogin f = new frmLogin();
   if (f.Run(out _us))
   {
    Application.Run(new frmUsers());
   }
   else
   {
    Application.Exit();
   }
  }
 }
}

frmLogin.cs

namespace Users
{
 public partial class frmLogin : Form
 {
  bool _Result = false;
  User _user;

  public frmLogin()
  {
   InitializeComponent();
  }
  internal bool Run(out User us)
  {
   us = _user;
   ShowDialog();
   return _Result;
  }

  private void btnLogin_Click(object sender, EventArgs e)
  {
   if (cmdPassword.Text.Length == 0 || cmdUser.Text.Length == 0)
   {
    errH.SetError(btnLogin, "!");
    return;
   }
   using (DCApp db = DCApp.NewDC())
   {
   List<User> logInP = db.Users.Where(s => s.UserPassword.ToLower().Equals(cmdPassword.Text.ToLower().Trim()) && s.UserName.ToLower().Equals(cmdUser.Text.ToLower())).ToList();
    if (logInP.Count == 0)
    {
     _Result = false;
    }
    else if (logInP.Count > 1)
    {
     _Result = false;
    }
    else
    {
     _Result = (bool) logInP.FirstOrDefault().IsActive;
     _user = logInP.First();
    }
   }
   Close();
  }

  private void btnCancel_Click(object sender, EventArgs e)
  {
   Application.Exit();
  }
 }
}

frmUsers.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Users
{
 public partial class frmUsers : Form
 {
  BindingSource _bs = new BindingSource();

  public frmUsers()
  {
   InitializeComponent();
   InitControls();   
  }
  private void InitControls()
  {
   dgwUser.AutoGenerateColumns = false;
  }
  private void btnLoad_Click(object sender, EventArgs e)
  {
   DCApp db = DCApp.NewDC();

   User logedUser = db.Users.FirstOrDefault(s => s == Program._us);

   List<Society> ListSociety = db.Societies.Where(s => s.UserID.Equals(logedUser)).ToList();

   _bs.DataSource = ListSociety;
   dgwUser.DataSource = _bs;
   dgwUser.Refresh();
   _bs.ResetBindings(false);    
  }
 }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Raul
  • 17
  • 1
  • 1
  • 7
  • What is `_bs` ? – Eldaniz Ismayilov Nov 23 '16 at 08:42
  • Can you check `gwUser.DataSource=ListSociety` – Eldaniz Ismayilov Nov 23 '16 at 08:42
  • You'll need to use something like `.Where(s => s.UserID.Equals(UserIdOfLoggedInUser))`. But how to find `UserIdOfLoggedInUser` is upto you because we don't see all your code. – Peter B Nov 23 '16 at 08:44
  • @Eldeniz _bs is variable name for a BindingSourice. BindingSource _bs = new BindingSource(); – Raul Nov 23 '16 at 08:57
  • @PeterB I know I have to use something like that, but I will put now all the rest of my code – Raul Nov 23 '16 at 08:57
  • Just make `.Equals(logedUser.Id)` that should give you the list for all the societies that user related to. I am assuming `logedUser` is your logged in user's class instance. Also one more advice instead of fetching user again from database just store logged in user and then use that. – Mahesh Nov 23 '16 at 09:12
  • It is very bad practice to store a password in plaintext in a database. Your passwords should be hashed using a cryptographically secure algorithm (SHA256 for example). For added protection against rainbow lookup tables you should use a unique random salt for each user (perhaps email address etc). see http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it and http://stackoverflow.com/questions/31274890/password-hashing-industry-standards – Steve Ford Nov 23 '16 at 09:45

3 Answers3

0

Are you using asp.net membership provider? if yes then you can use the bellow code to get the id :

MembershipUser user = Membership.GetUser();
string id = user.ProviderUserKey.ToString();
Voice Of The Rain
  • 495
  • 1
  • 8
  • 24
  • You just need to retrieve your logged in user id from somewhere like a text file or an XML. – Voice Of The Rain Nov 23 '16 at 09:16
  • I don't use web services. – Raul Nov 23 '16 at 09:40
  • Basically you are storing a user object in login form and the program class, the users form don't have a clue bout the user object you need to pass your user object to users form constructor to become available 'code' public frmUsers(User loggedUser) { InitializeComponent(); InitControls(); } – Voice Of The Rain Nov 23 '16 at 09:57
0

I think problem is dgwUser.AutoGenerateColumns = false;

Either

private void InitControls()
  {
   dgwUser.AutoGenerateColumns = true;
  }

Or

 private void AddColumns()
    {

        var col1 = new DataGridViewTextBoxColumn();
        var col2 = new DataGridViewCheckBoxColumn();
        var col3 = new DataGridViewCheckBoxColumn();

        col1.HeaderText = "YourHeaderText";
        col1.Name = "ID";

        col2.HeaderText = "YourHeaderText";
        col2.Name = "UserID";

        col3.HeaderText = "YourHeaderText";
        col3.Name = "Socities";

        dgwUser.Columns.AddRange(new DataGridViewColumn[] {col1,col2,col3});
    }




     ...
      List<Society> ListSociety = db.Societies.Where(s => s.UserID.Equals(logedUser.ID)).ToList();
      _bs.DataSource = ListSociety;
       dgwUser.DataSource = _bs;
       AddColumns();
       dgwUser.Refresh();
       _bs.ResetBindings(false);   
       ...
0

I solved the problem! This is what i did:

In frmLogin.cs

...
  internal bool Run(out User user)
  {      
   ShowDialog();
   user = _user;
   return _Result;
  }
...

And in frmUsers.cs

private void btnLoad_Click(object sender, EventArgs e)
  {
   DCApp db = DCApp.NewDC();

   User logedUser = db.Users.FirstOrDefault(s => s == Program._user);

   List<Society> ListSociety = db.Societies.Where(s => s.UserID == logedUser.ID).ToList();  

   _bindingSource.DataSource = ListSociety;
   dgwUser.DataSource = _bindingSource;
   dgwUser.Refresh();
   _bindingSource.ResetBindings(false);    
  }

Thanks everyone for advices!

Raul
  • 17
  • 1
  • 1
  • 7