Register Form (Not Inserting data to Database) - DarkETHOS - Mar 29, 2016
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#)
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(); } } }
* - RivenCode - Mar 29, 2016
Whenever I worked with the Sql library it never gave me errors when I had an query error, so you might wanna double check your fields in the database if they are all varchars. Other then that you should use breakpoints and go step by step through your exection of your code.
If you'll send your teamviewer I'll might help you out!
* - GuRuHaX - Mar 29, 2016
Agree , set a breakpoint and see if there are some values , if there is , check your DB.
Also add your DB from debug folderto server explorer and check DB field values cause when debugging the data goes into that one and not the one originally visible in server explorer.
And check if DB properties (f4) are set to copy if newer.
🤝
* - Odus - Mar 29, 2016
(Mar 29, 2016 08:22 AM)DarkETHOS Wrote: PHP Code
<?php using (SqlCommand insert = new SqlCommand("INSERT INTO Account (username, password, firstname, lastname) VALUES ('" + username + "', '" + password + "','" + firstname + "','" + lastname + "')", connection))
On another note, string.Join will clean that right up. I hope this is a personal project btw.
* - IJustRead - Mar 29, 2016
Just to show you, how it could looks like
And as the others said, check for spelling and datatyp, also debug throw the code and check the exception
i hope that will help you at least a little bit
PHP Code
<?php SqlCommand command = null; try { command = connection.CreateCommand(); command.CommandText = @" INSERT INTO Account ( username, password, firstname, lastname ) VALUES ( @username, @password, @firstname, @lastname )"; command.CreateParameterDB("username", username); command.CreateParameterDB("password", password); command.CreateParameterDB("firstname", firstname); command.CreateParameterDB("lastname", lastname); command.ExecuteNonQuery(); } catch (Exception ex) { Log.WriteException(ex); } finally { if (command != null) { command.Dispose(); command = null; } }
* - Carlsson94 - Mar 30, 2016
This is what I do when I insert values in to my SQL table, it should give you the basic idea how to do it.
Code using (MySqlConnection mySqlConnection = new MySqlConnection(_connectionString))
{
mySqlConnection.Open();
using (MySqlCommand mySqlCommand = new MySqlCommand("INSERT INTO files (filename, filecontent) VALUES (@filename, @filecontent)", mySqlConnection))
{
mySqlCommand.Parameters.AddWithValue("@filename", fileName);
mySqlCommand.Parameters.AddWithValue("@filecontent", tmpString);
mySqlCommand.ExecuteNonQuery();
mySqlConnection.Close();
}
}
You should use sql parameters to avoid sql injection.
* - Zilog80 - Mar 30, 2016
No need to Close or Dispose the using block will take care of that for you. So mySqlConnection.Close(); sentence is useless.
|