Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a login Database made in Microsoft Access and I created a program in C# that i want to allow the user to create new logins with. Take note that im learning SQL still and this is my first attempt to use it.
Database Info:
Table Name: LoginTable
Username Column Name: User
Password Column Name: Pass

I'm trying to insert new records for the database, Username and Password.
TextBox1 is the username, Textbox 2 Im using to compare the text from TextBox3 to, and TextBox 3 is what Im using for the password to insert into the table.


Im using:
Language: C# 4.0
Platform: Windows 7 - x64
Compiler: Visual Studio Ultimate
Microsoft Access 2007

Here's my code:
C#
if (textBox2.Text == textBox3.Text && String.IsNullOrWhiteSpace(textBox1.Text) == false)
            {
                string insertCMD = "INSERT INTO LoginTable (User,Pass) VALUES (?,?,?)";
                string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\MedDesk\\Login.accdb;";
                using (OleDbConnection MyConn = new OleDbConnection(ConnStr))
                {
                    try
                    {//insertCommand.Parameters.Add("Time", OleDbType.Char).Value = strTime;
                        MyConn.Open();
                        OleDbCommand Cmd = new OleDbCommand(insertCMD, MyConn);
                        Cmd.Parameters.Add("User", OleDbType.Char).Value = textBox1.Text;
                        Cmd.Parameters.Add("Pass", OleDbType.Char).Value = textBox3.Text;
                        Cmd.ExecuteNonQuery();
                        MyConn.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

            }


Any questions please reply, I need help on this ASAP!
Posted
Comments
shelby67 14-Feb-12 16:05pm    
I'm using WPF, not sure if it really helps to know that but...

What's the error you are getting?

I see a couple of things. First...you really should name things more meaningful. textbox1 and textbox 2 is confusing. Start a naming convention with the first three characters telling you what the DataType is, then add a meaningful name. txtUserName and txtPassword are SOOO much easier to understand.

Second item, what are you trying to do here?
C#
if (textBox2.Text == textBox3.Text && String.IsNullOrWhiteSpace(textBox1.Text) == false)


Third, you have two values in the table, but are setting three?
C#
string insertCMD = "INSERT INTO LoginTable (User,Pass) VALUES (?,?,?)";


Fourth, what is the @ for?
C#
string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\MedDesk\\Login.accdb;";


And fifth, if you're just playing around it's probably not an issue, but you really shouldn't store passwords as plain text. Look into hashing them before you save them into the file, then you can compare the hash of something a user types to what you have saved in the file. It's much more secure.
 
Share this answer
 
Comments
shelby67 14-Feb-12 16:14pm    
1.) I'm checking to make sure the Password matches the other Password TextBox, (textbox2, textbox3). then the last part im checking to see if textbox1 is not empty, i dont want to insert nothing into the database..
shelby67 14-Feb-12 16:15pm    
as for the @ symbol, i got that off the web.
lewax00 14-Feb-12 16:28pm    
The @ symbol is so you don't need to escape things, so you should either remove it or replace "G:\\MedDesk\\Login.accdb" with "G:\MedDesk\Login.accdb"
Kschuler 14-Feb-12 16:44pm    
Good to know!
shelby67 14-Feb-12 16:15pm    
even if I delete the third value and fix it to what you mentioned, still fails. The error is "Invalid Syntax in INSERT INTO command".
C#
string insertCMD = "INSERT INTO LoginTable ([User],Pass) VALUES (?,?)"

You must escape "User" by placing it between [].
And you need two question marks only, because you insert two values, not three.
 
Share this answer
 
I think problem is with parameter see here how to use ot.

http://www.java2s.com/Code/CSharp/Database-ADO.net/RunanINSERTstatementwithparameters.htm[^]
 
Share this answer
 

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