Skip to content

Commit

Permalink
Connect to DB, Create New User function, and outline for Authenticate…
Browse files Browse the repository at this point in the history
… User
  • Loading branch information
cyhamUMICH committed Dec 1, 2020
1 parent 451640d commit 65ccfe8
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

*.config

# User-specific files
*.suo
*.user
Expand Down
53 changes: 52 additions & 1 deletion VOTE/VOTE/DatabaseInterface.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VOTE
{
Expand All @@ -12,14 +14,22 @@ class DatabaseInterface

private static DatabaseInterface instance = null;

private String connectionString = "";
private String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
private SqlConnection connection;

private DatabaseInterface()
{
connection = new SqlConnection(connectionString);
}

~DatabaseInterface()
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}

public static DatabaseInterface getInstance()
{
if (instance == null)
Expand All @@ -32,7 +42,48 @@ public static DatabaseInterface getInstance()

public void createNewUser(User user)
{
connection.Open();

try
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO [User](username, password, administrator, state, birthdate, gender, race) VALUES (@username, @password, @administrator, @state, @birthdate, @gender, @race)";
command.Parameters.AddWithValue("@username", user.Username);
command.Parameters.AddWithValue("@password", user.Password);
command.Parameters.AddWithValue("@administrator", 0);
command.Parameters.AddWithValue("@state", user.State);
command.Parameters.AddWithValue("@birthdate", user.DateOfBirth);
command.Parameters.AddWithValue("@gender", user.Gender);
command.Parameters.AddWithValue("@race", user.Race);
command.ExecuteNonQuery();
}

}
catch
{

}
finally
{

connection.Close();
}
}

public User authenticateUser(string username, string password)
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM [User] WHERE username=@username";
command.Parameters.AddWithValue("@username", username);

using (SqlDataReader reader = command.ExecuteReader())
{
return null;
}

}
}
}
}
13 changes: 12 additions & 1 deletion VOTE/VOTE/Login.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public Login()
private void loginButton_Click(object sender, EventArgs e)
{
SelectBallot selectBallot = new SelectBallot();
selectBallot.Show();
String username = loginUsernameTextBox.Text;
String password = loginPasswordTextBox.Text;

if (database.authenticateUser(username, password) != null)
{
selectBallot.Show();
}
}

private void registerButton_Click(object sender, EventArgs e)
Expand All @@ -42,6 +48,11 @@ private void registerButton_Click(object sender, EventArgs e)
User user = new User(username, password, state, birthdate, gender, race);

database.createNewUser(user);

this.Hide();
SelectBallot selectBallot = new SelectBallot();
selectBallot.Show();
this.Close();
}
}
}
24 changes: 12 additions & 12 deletions VOTE/VOTE/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ namespace VOTE
{
class User
{
private String username;
private String password;
private String state;
private String dateOfBirth;
private String gender;
private String race;
public String Username { get; set; }
public String Password { get; set; }
public String State { get; set; }
public String DateOfBirth { get; set; }
public String Gender { get; set; }
public String Race { get; set; }

public User(String username, String password, String state, String dateOfBirth, String gender, String race)
{
this.username = username;
this.password = password;
this.state = state;
this.dateOfBirth = dateOfBirth;
this.gender = gender;
this.race = race;
this.Username = username;
this.Password = password;
this.State = state;
this.DateOfBirth = dateOfBirth;
this.Gender = gender;
this.Race = race;
}
}
}
1 change: 1 addition & 0 deletions VOTE/VOTE/VOTE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
1 change: 1 addition & 0 deletions VOTE/VOTE/ViewBallot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class ViewBallot : Form
public ViewBallot()
{
InitializeComponent();
DatabaseInterface db = DatabaseInterface.getInstance();
}
}
}

1 comment on commit 65ccfe8

@cyhamUMICH
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was originally made on November 10, 2020, but was edited to show the author correctly. Since it was edited after the fact, it appears out of order on the GitHub website. However, it will appear in the correct spot when doing git log.

Please sign in to comment.