After browsing this site for some time, I have been able to make a start on a piece of code that adds information from a set of text boxes into a pre-set database.
Here is that code:
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;data source=[[Rather not release private information, but trust me, this part works]]";
var _ID = LblID.Text;
var _FirstName = TxtUsername.Text;
var _LastName = TxtPassword.Text;
var _DOB = TxtFirstName.Text;
var _Number1 = TxtLastName.Text;
var _Number2 = TxtDOB.Text;
var _Email = TxtNumber2.Text;
var _Username = TxtEmail.Text;
var _Password = TxtNumber1.Text;
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand cmd = new OleDbCommand("INSERT into Users ([_ID], [_FirstName], [_LastName], [_DOB], [_Number1], [_Number2], [_Email], [_Username], [_Password]) Values(@ID Number, @First Name, @Last Name, @Date of Birth, @Phone Number 1, @Phone Number 2, @Email, @Username, @Password)");
cmd.Connection = conn;
conn.Open();
if (conn.State == ConnectionState.Open)
{
cmd.Parameters.Add("@ID Number", OleDbType.VarChar).Value = _ID;
cmd.Parameters.Add("@First Name", OleDbType.VarChar).Value = _FirstName;
cmd.Parameters.Add("@Last Name", OleDbType.VarChar).Value = _LastName;
cmd.Parameters.Add("@Date of Birth", OleDbType.VarChar).Value = _DOB;
cmd.Parameters.Add("@Phone Number 1", OleDbType.VarChar).Value = _Number1;
cmd.Parameters.Add("@Phone Number 2", OleDbType.VarChar).Value = _Number2;
cmd.Parameters.Add("@Email", OleDbType.VarChar).Value = _Email;
cmd.Parameters.Add("@Username", OleDbType.VarChar).Value = _Username;
cmd.Parameters.Add("@Password", OleDbType.VarChar).Value = _Password;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Data Added");
conn.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
conn.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
}
Its a bit long, but stay with me here.
This code is very similar to one I have found from an answer to a previous question, of course, renaming variables and such to integrate the code into my application.
However, after doing a step by step breakdown of how the application is processing this, it seems to fail here:
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Data Added");
conn.Close();
}
More specifically, the cmd.ExecuteNonQuery().
It seems that the try stops at this part, and skips straight to the catch.
My question is, what am I doing wrong? How badly have I fuc- messed this up?
If you need any specifics, just comment and I'll respond as soon as I can.
Thanks in advance.
Edit 1: Forgot to add the error message, my apologies.
"Syntax error (missing operator) in query expression '@ID Number'."
Edit 2: Not sure if this should be an edit, but now I'm getting an entirely new problem.
After reading the comments, I have changed the area names (both in the application and in the database), and now I'm getting an entirely new error message.
"The INSERT INTO statement contains the following unknown field name: '_ID'."
I have a feeling it will complain the same for the rest of them.
Sorry about all of the trouble.