Home
Manage Your Code
Snippet: Logon Database Query (C#)
Title: Logon Database Query Language: C#
Description: An example of connecting to a database, and checking the username and password. Views: 103
Author: Robert Dunbar Date Added: 4/3/2008
Copy Code  
1try
2            {
3                DataSet dsLogon = new DataSet();
4                String query = "SELECT [User].FirstName, [User].LastName, Panel, Permission FROM [User], [Role] " +
5                               "WHERE " +
6                               "Username = '" + txtUsername.Text + "' AND " +
7                               "Password = '" + txtPassword.Text + "' AND " +
8                               "[User].RoleName = [Role].Name";
9                SqlDataAdapter da = new SqlDataAdapter(query, g_obj.getSQLConnection());
10                da.Fill(dsLogon, "User");
11
12                if (dsLogon.Tables[0].Rows.Count > 0)
13                {
14                    // Set permissions to defaults
15                    g_obj.gPermissions = new Permissions();
16
17                    // Process all permissions
18                    foreach (DataRow row in dsLogon.Tables[0].Rows)
19                        g_obj.gPermissions.setPanelPermission(int.Parse(row["Panel"].ToString()),
20                                                              int.Parse(row["Permission"].ToString()));
21
22                    String userRealName = dsLogon.Tables[0].Rows[0][0].ToString();
23                    userRealName += " " + dsLogon.Tables[0].Rows[0][1].ToString();
24
25                    g_obj.gMain = new frmMain(userRealName);
26                    this.Hide();
27                    g_obj.gMain.Show();
28                }
29                else
30                {
31                    Msg.WarningBox("Invalid Credentials", 
32                        "You have entered an incorrect username or password.");
33                }
34            }
35            catch (Exception ex)
36            {
37                System.Windows.Forms.MessageBox.Show("Error whilst talking to the database.\n" + ex.Message,
38                    "Database Communication Error",
39                    System.Windows.Forms.MessageBoxButtons.OK,
40                    System.Windows.Forms.MessageBoxIcon.Error);
41            }