OleDBCommand: what might be the problem?

Sorry you all if I bother you lately.  I'm new to c# that's why I look out for assisatance often. 

I'm trying to insert user name and password into an access database through a create button.  However, anytime I click on the create button there is an exception which suggests that there is a problem with my sql insert statements.  I need help to ascertain what the problem is and how I can rectify that problem. 



private void btnCreate_Click(object sender, System.EventArgs e)

{

OleDbConnection sgConn = null;

string sgConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source= C:\\Documents and Settings\\flexcube\\My Documents\\Visual Studio Projects\\sgssbproject\\sgDataBase.mdb";

if(txtUserName.Text=="")

{

MessageBox.Show("Please Check Empty Field");

}

else

{

try

{

string name,password;

name = txtUserName.Text;

password = txtPassword.Text;

string commandString ="Insert into Users (username,password)values(name,password)";

sgConn = new OleDbConnection(sgConnStr);

OleDbCommand myUsersCommand = new OleDbCommand(commandString,sgConn);

sgConn.Open();

myUsersCommand.ExecuteNonQuery();

txtUserName.Text ="";

txtPassword.Text = "";

}

catch(Exception exc)

{

MessageBox.Show(exc.ToString());

}

finally

{

sgConn.Close();

}

}



 


Thank you very much.

Regards,

Derry


Answer this question

OleDBCommand: what might be the problem?

  • dwith

    Thank you jason. It worked perfectly. Can I have your email ad so that we could have some coding correspondence. 

    Thank you.

    Derry

  • FinallyInSeattle

    If the above is an exact copy of your code, the problem should be solved with this:

    string commandString ="Insert into Users (username,password)values('" + name + "','" + password + "')";

  • Goye

    Well, the answer is obvious when you give it a little thought. Password is a reserved keyword so:

    string commandString ="Insert into Users (username,[password])values('" + name + "','" + password + "')";

  • ahr

    Thank you jason for your prompt response. I tried your code and I got the same exception "Syntax error in the insert into statement."

    What I really want to do is this:
    I have a form that create accounts for users of an application.  To create the account, you have to enter your username and password in the textboxes on the user.cs form.  When the create button is clicked, the username and the password in the textboxes should be written in the users table of the project database. The users table has username and password as fields, with all of them having types text.  This led me to declare new strings  name and passord and let their values  equal txtUserName.Text and txtPassword.Text. I then use those strings to as my values in the insert statement. Hope u get my drift now.

    Thank you for your response anyway.

    Regards,
    Derry

  • MMMSobo

    imsinc at gmail dot com
  • OleDBCommand: what might be the problem?