Click here to Skip to main content
15,885,065 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.Data.OleDb;

namespace Personal_Program
{
    public partial class Form1 : Form
    {
        private OleDbConnection dataconnection = new OleDbConnection();
        public Form1()
        {
            InitializeComponent();
            dataconnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Cristopher\\Documents\Personaldb.accdb; Persist Security Info=True;";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void CreateBT_Click(object sender, EventArgs e)
        {
                dataconnection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = dataconnection;
                command.CommandText = "INSERT into CreateAccount (FullName, PrefUsername, Birthday, Gender, MobileNo, Password, ConfirmPass) VALUES ('" + FullNameTB.Text + "', '" + PrefUserTB.Text + "', '" + BirthdayDT.Text + "', '" + GenderCB.Text + "', '" + MobileTB.Text + "', '" + SetPassTB.Text + "', '" + ConfirmPassTB.Text + "')";
                
                command.ExecuteNonQuery();
                MessageBox.Show("Account Created. Please login to your account.");
                dataconnection.Close();
        }
    }
}

What I have tried:

I keep on having error with the line where I am trying to open the connectiin of my system with the MS Access database or should I say the line "dataconnection.Open(); I tried renaming the file name and location of my database file but I keep on running for the same error.Please help.
Posted
Updated 9-Jun-20 12:05pm
Comments
Member 14857740 9-Jun-20 4:36am    
I keep on having error with the line where I am trying to open the connectiin of my system with the MS Access database or should I say the line "dataconnection.Open(); I tried renaming the file name and location of my database file but I keep on running for the same error.Please help.
Member 14857740 9-Jun-20 4:38am    
I already tried this:

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.Data.OleDb;

namespace Personal_Program
{
public partial class Form1 : Form
{
private OleDbConnection dataconnection = new OleDbConnection();
public Form1()
{
InitializeComponent();
dataconnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Cristopher\Documents\Personaldb.accdb; Persist Security Info=False;";
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void CreateBT_Click(object sender, EventArgs e)
{
dataconnection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = dataconnection;
command.CommandText = "INSERT into CreateAccount (FullName, PrefUsername, Birthday, Gender, MobileNo, Password, ConfirmPass) VALUES ('" + FullNameTB.Text + "', '" + PrefUserTB.Text + "', '" + BirthdayDT.Text + "', '" + GenderCB.Text + "', '" + MobileTB.Text + "', '" + SetPassTB.Text + "', '" + ConfirmPassTB.Text + "')";

command.ExecuteNonQuery();
MessageBox.Show("Account Created. Please login to your account.");
dataconnection.Close();
}
}
}
Member 14857740 9-Jun-20 4:39am    
I also tried putting try{} and catch{} codes. Still the same error.
CHill60 9-Jun-20 4:41am    
Instead of posting lots of comments to your own question use the "Improve Question" link to add them to your original post

C#
command.CommandText = "INSERT into CreateAccount (FullName, PrefUsername, Birthday, Gender, MobileNo, Password, ConfirmPass) VALUES ('" + FullNameTB.Text + "', '" + PrefUserTB.Text + "', '" + BirthdayDT.Text + "', '" + GenderCB.Text + "', '" + MobileTB.Text + "', '" + SetPassTB.Text + "', '" + ConfirmPassTB.Text + "')";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
Maciej Los 9-Jun-20 4:57am    
5ed!
Patrice T 9-Jun-20 5:16am    
Thank you
You are missing an escape character - see
C#
dataconnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Cristopher\\Documents\Personaldb.accdb; Persist Security Info=True;";
It should be
C#
dataconnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Cristopher\\Documents\\Personaldb.accdb; Persist Security Info=True;";
Ok - sorry I forgot that the text would be formatted . . . the Source bit of your text should read
C:\ \ Users \ \ Christopher \ \ Documents \ \ Personaldb.accdb;
without those spaces I've added. You only have one backslash in front of Personaldb
 
Share this answer
 
v4
Comments
CHill60 9-Jun-20 4:45am    
?? I did not edit this 3 times!
Maciej Los 9-Jun-20 4:59am    
It happens when servers are little busy...
BTW: 5ed!
CHill60 9-Jun-20 5:21am    
Thanks for the 5 - and the explanation. I thought I was going a little mad - perhaps I am anyway :-)
Maciej Los 9-Jun-20 5:49am    
:laugh:
You have to be consistent: "\\" is a single backslash character, "\P" is a bad character and wouldn't compile - you will get an error:
CS1009	Unrecognized escape sequence
C#
"...;Data Source=C:\\Users\\Cristopher\\Documents\Personaldb.accdb; ..."
                                      ^^         ^

Either use double backslash throughout, or use teh @ before your string:
C#
dataconnection.ConnectionString = "...;Data Source=C:\\Users\\Cristopher\\Documents\\Personaldb.accdb;...";
C#
dataconnection.ConnectionString = @"...;Data Source=C:\Users\Cristopher\Documents\Personaldb.accdb;...";


Better still, don't hardcode connection strings - always load them from a configuration file or similar. That way you don't have to change the code and release it untested when you release the software, and you don't need a different EXE file for each different PC ...

The way I do it is more complex than you probably need, but ... Instance Storage - A Simple Way to Share Configuration Data among Applications[^]
 
Share this answer
 
Comments
Maciej Los 9-Jun-20 4:59am    
5ed!
You are missing a \ in the connection string before the access file name Personaldb.accdb

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Cristopher\\Documents\Personaldb.accdb; Persist Security Info=True;"


should read

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Cristopher\\Documents\\Personaldb.accdb; Persist Security Info=True;"
 
Share this answer
 
v4
Comments
Maciej Los 9-Jun-20 4:59am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900