Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Iam a beginner in asp.net and i cannot send data from text box to database using this code please help me

this is code
all feilds are character


    protected void enterdata(object sender, EventArgs e)
    {
        try
        {
   
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Sree\\Documents\\krishna.accdb;Persist Security Info=True");
            con.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO masterusertable (email ,password) Values (@UN, @PW)", con);
            oleDbCommand1.Parameters.AddWithValue("@UN", txtusername.Text);
            oleDbCommand1.Parameters.AddWithValue("@PW", txtpassword.Text);
            oleDbCommand1.ExecuteNonQuery();
            con.Close();
           }
        catch (Exception exp)
        {
            Response.Write(exp);
        }
    }


[edit]Code block added - OriginalGriff[/edit]



iam getting an exception


System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at Newreg.enterdata(Object sender, EventArgs e)
Posted
Updated 24-Jun-11 2:31am
v4
Comments
Christian Graus 24-Jun-11 4:21am    
First of all, you've been told about why this code is terrible. I'd fire you if you wrote this and I was your boss. Second, if you get an error like this, get your SQL in the debugger, and run it direct in SQL Server to get a better error message. Then you can try to work out what is wrong. I suspect you have a ' in your username or password. Parameterised queries will fix this for you.

You should make sure you hav following component installed as prerequsit.

2007 Office System Driver: Data Connectivity Components[^] Then try your code.

In case you are geting any specific error, please include that as well.
 
Share this answer
 
So many things...

If there is an error message, then we need to know what it is - use the "Improve question" widget and edit your question to include it.

Secondly, don't ever create your database strings like that! Concatenating strings leaves you wide open to an accidental or delibetare SQL Injeection attack which could destroy your database. Use parametrized queries instead.
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO user (email ,password) Values (@UN, @PW)", con);
oleDbCommand1.Parameters.AddWithValue("@UN", txtusername.Text);
oleDbCommand1.Parameters.AddWithValue("@PW", txtpassword.Text);
oleDbCommand1.ExecuteNonQuery();


Thirdly, never store passwords in clear text - it is a serious security risk. There is an explanation here: Password Storage: How to do it.[^]


"I had done it but same exception occurs"

Ah! It may be simple: enclose the name of your password field in square brackets:
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO masterusertable (email ,password) Values (@UN, @PW)", con);
becomes
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO masterusertable (email ,[password]) Values (@UN, @PW)", con);
"Password" is a reserved word in JET databases, that may apply to ACE as well.
 
Share this answer
 
v2
Comments
Monjurul Habib 24-Jun-11 3:37am    
nice answer,my 5.
SREENATH GANGA 24-Jun-11 4:01am    
the problm is not being corrected
OriginalGriff 24-Jun-11 4:06am    
If you are still getting the same exception, copy and paste your code fragment between the con.Open and con.Close so we can see exactly what you are using.
SREENATH GANGA 24-Jun-11 8:31am    
I had done it but same exception occurs
OriginalGriff 24-Jun-11 8:42am    
Answer updated

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



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