Nirre
Nirre

Reputation: 51

ASP.Net Error in inserting Data To OleDb

I keep getting this error. I read that OleDb doesn't support named parameters so I changed them to (?, ?).

But is keep getting the same error

System.Data.OleDb.OleDbException: 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 Registration.ImageButton1_Click(Object sender, ImageClickEventArgs e) in c:\Users\Nirre\Desktop\Cti Project\Book Club TRY 2\Registration.aspx.cs:line 51

Here is the code in the .cs class

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    if (IsPostBack)
    {
        OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        conn.Open();
        string insCmd = @"INSERT COUNT(*) INTO Registration(UserName, [Password], EmailAddress, FullName, Country, PasswordRecovery)
                     VALUES (?, ?, ?, ?, ?, ?)";
        OleDbCommand insertUser = new OleDbCommand(insCmd, conn);
        insertUser.Parameters.AddWithValue("@UserName", txtUserName.Text);
        insertUser.Parameters.AddWithValue("@Password", txtPassoword.Text);
        insertUser.Parameters.AddWithValue("@EmailAddress", txtEmail.Text);
        insertUser.Parameters.AddWithValue("@FullName", txtFullName);
        insertUser.Parameters.AddWithValue("@Country", DdlCountry.SelectedItem.ToString());
        insertUser.Parameters.AddWithValue("@PasswordRecovery", txtAnswer.Text);

        try
        {
            insertUser.ExecuteNonQuery();
            conn.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write(er);
            lblError.Visible = true;
            lblError.Text = "An error has ocured pleas try again...!";
        }
        finally
        {
            // Code
        }
    }
} 

Upvotes: 0

Views: 1112

Answers (2)

Dimitri
Dimitri

Reputation: 7013

string insCmd = @"INSERT COUNT(*) INTO Registration(UserName, [Password], EmailAddress, FullName, Country, PasswordRecovery)
                     VALUES (?, ?, ?, ?, ?, ?)";

Should be

string insCmd = @"INSERT INTO Registration(UserName, [Password], EmailAddress, FullName, Country, PasswordRecovery)
                     VALUES (@UserName, @Password, @EmailAddress, @FullName, @Country, @PasswordRecovery)";

I don't understand what did you want to do with COUNT. If you want to then return number of affected rows, just add return parameter and assign @@ROWCOUNT to it

Edit: I've never used question marks as parameter placeholders outside Reporting Services so try replacing them with named parameters as shown above.

Upvotes: 1

Steve
Steve

Reputation: 216243

INSERT COUNT(*) is not a valid SQL statement, simply remove COUNT(*) and the rest of your code seems correct.

See here for refs

Upvotes: 1

Related Questions