Mar 29, 2016 08:22 AM
(This post was last modified: Mar 29, 2016 08:24 AM by DarkETHOS.)
So with the code below, I'm trying to insert data to the database however whenever I click the Register button it does not 'Insert' the value into the db.
Please ignore for not using prepared statement, I'm just testing out. (New to C#)
Please ignore for not using prepared statement, I'm just testing out. (New to C#)
PHP Code
<?php
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 System.Configuration;
using System.Data.SqlClient;
namespace Login_System
{
public partial class RegisterForm : Form
{
SqlConnection connection;
string connectionString;
public RegisterForm()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["Login_System.Properties.Settings.Database1ConnectionString"].ConnectionString;
}
internal void Register()
{
foreach (TextBox textbox in this.Controls.OfType<TextBox>())
{
if (string.IsNullOrWhiteSpace(firstNameTextBox.Text))
{
MessageBox.Show("Please do not leave the field empty!");
}
}
}
private void registerBtn_Click(object sender, EventArgs e)
{
string username = usernameTextBox.Text;
string password = passwordTextBox.Text;
string verifypassword = verifyPasswordTextBox.Text;
string firstname = firstNameTextBox.Text;
string lastname = lastNameTextBox.Text;
try
{
using (connection = new SqlConnection(connectionString))
using (SqlCommand insert = new SqlCommand("INSERT INTO Account (username, password, firstname, lastname) VALUES
('" + username + "', '" + password + "','" + firstname + "','" + lastname + "')", connection))
{
connection.Open();
insert.ExecuteNonQuery();
MessageBox.Show("Test");
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void cancelBtn_Click(object sender, EventArgs e)
{
LoginForm LoginForm = new LoginForm();
LoginForm.Show();
this.Close();
}
}
}


