none
how to check a values of database column and how to use them with if statement in c# windows form RRS feed

  • Question

  • Hi, i have a form with 8 buttons, also i have 2 database table 1st is userlogin table and 2nd is userhideforms. in userlogin table there is only 3 columns(id,username,password) and in userhideforms table there is 9 columns(username,button1,button2,button3,button4,button5,button6,button7,button8) all buttons column have only "True" or "False" value. i just want that when user login into form then the 8 buttons auto hide or unhide using userhideform column values. e.g i have a username "user1" and i want that when user1 login, the form show only button1, button3, button7 and hide the other buttons.

    only that buttons show that have "True" value in userhideform table and that buttons hide that have "False" value in userhideform table. i'm using sqlite database.

    i don't know how to do that. kindly help me.

    Monday, January 7, 2019 6:36 PM

Answers

  • The following is a basic flow. There are several classes which may seem like overkill but they provide a way to write code the same everytime for the basic of exceptions and opening a database.

    These classes are here and not included below.

    Class to hold read data

    public class UserFormSecurity 
    {
        public int UserId { get; set; }
        public bool Button1 { get; set; }
        public bool Button2 { get; set; }
    }

    Read data

    public class DataOperations : BaseSqlServerConnections
    {
        public DataOperations()
        {
            DefaultCatalog = "Your database name";
        }
        /// <summary>
        /// Given a user identifier get enable state of controls
        /// </summary>
        /// <param name="userIdentifier"></param>
        /// <returns></returns>
        public UserFormSecurity GetFormSecurity(int userIdentifier)
        {
            const string selectStatement = "SELECT Button1, Button2 FROM YourTable WHERE UserId = @UserId";
            var result = new UserFormSecurity();
            using (var cn = new SqlConnection() {ConnectionString = ConnectionString})
            {
                using (var cmd = new SqlCommand() {Connection = cn, CommandText = selectStatement})
                {
                    cmd.Parameters.AddWithValue("@UserId", userIdentifier);
                    cn.Open();
                    var reader = cmd.ExecuteReader();
    
                    if (!reader.HasRows) return result;
                    reader.Read();
    
                    result.UserId = userIdentifier;
                    result.Button1 = reader.GetBoolean(0);
                    result.Button2 = reader.GetBoolean(1);
                }
            }
    
            return result;
        }
    }
    When return to the caller (assuming a form) you can set controls via the values in the class returned.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by John6272 Tuesday, January 8, 2019 4:03 AM
    Monday, January 7, 2019 8:07 PM
    Moderator

All replies

  • Hello,

    First off I recommend having the user id in both tables rather than using user name in the userhideforms table so that if a user name changes you only change it in the login table.

    In regards to column values in the forms table. Use a SqlClient connection and command, the command text for the command would be a SELECT statement with a where clause (and this mean using a parameter rather than string concatenation.

    Open the connection, use ExecuteReader on the Command e.g.

    var reader = cmd.ExecuteReader();

    Then see if you located the row

    if(reader.HasRows)

    If so use reader.Read();

    Now get each field value e.g. reader.GetBool(index) where index is the column index in the SQL SELECT executed above. Once each value returned act upon it to hide/show, enable/disable controls.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 7, 2019 6:51 PM
    Moderator
  • can u please share the code.
    Monday, January 7, 2019 7:06 PM
  • can u please share the code.
    Just returned to my desk, I will slap together some code shortly.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 7, 2019 7:50 PM
    Moderator
  • The following is a basic flow. There are several classes which may seem like overkill but they provide a way to write code the same everytime for the basic of exceptions and opening a database.

    These classes are here and not included below.

    Class to hold read data

    public class UserFormSecurity 
    {
        public int UserId { get; set; }
        public bool Button1 { get; set; }
        public bool Button2 { get; set; }
    }

    Read data

    public class DataOperations : BaseSqlServerConnections
    {
        public DataOperations()
        {
            DefaultCatalog = "Your database name";
        }
        /// <summary>
        /// Given a user identifier get enable state of controls
        /// </summary>
        /// <param name="userIdentifier"></param>
        /// <returns></returns>
        public UserFormSecurity GetFormSecurity(int userIdentifier)
        {
            const string selectStatement = "SELECT Button1, Button2 FROM YourTable WHERE UserId = @UserId";
            var result = new UserFormSecurity();
            using (var cn = new SqlConnection() {ConnectionString = ConnectionString})
            {
                using (var cmd = new SqlCommand() {Connection = cn, CommandText = selectStatement})
                {
                    cmd.Parameters.AddWithValue("@UserId", userIdentifier);
                    cn.Open();
                    var reader = cmd.ExecuteReader();
    
                    if (!reader.HasRows) return result;
                    reader.Read();
    
                    result.UserId = userIdentifier;
                    result.Button1 = reader.GetBoolean(0);
                    result.Button2 = reader.GetBoolean(1);
                }
            }
    
            return result;
        }
    }
    When return to the caller (assuming a form) you can set controls via the values in the class returned.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by John6272 Tuesday, January 8, 2019 4:03 AM
    Monday, January 7, 2019 8:07 PM
    Moderator
  • thanks.
    Tuesday, January 8, 2019 4:03 AM