Wednesday, March 28, 2012

Problems inserting into database :(

Hello everyone,

I am having some problems inserting some data into a database - I have been stuck on this almost all day now and I'm almost pulling my hair out! I'm quite new to ASP.net and have been thrown in the deep end with this assignment... And advice or help would be greatly appreciated. Thanks.Smile

Protected Sub CreateUserWizard1_CreatedUser1(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles CreateUserWizard1.CreatedUser'Assign new user to role of Applicant Roles.AddUserToRole(CreateUserWizard1.UserName,"Applicant")'Create Connection String UserName = CreateUserWizard1.UserNameDim DBConnAs New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True")Dim DBCmdAs New SqlCommand DBConn.Open()'Add Insert Statement DBCmd =New SqlCommand("INSERT INTO aspnet_Users(MobileAlias) VALUES (@.Applicant) WHERE UserName=@.UserName", DBConn)'Add Database Parameters DBCmd.Parameters.Add("@.Applicant", SqlDbType.NVarChar).Value ="Applicant" DBCmd.ExecuteNonQuery()'Close Database connection 'and Dispose Database objects DBCmd.Dispose() DBConn.Close() DBConn =Nothing End Sub

------------------------

error generated:

Incorrect syntax near the keyword 'WHERE'.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'WHERE'.

One thing I notice right off the bat is that you forgot to add a "UserName" Parameter.

|||

Sorry it seemed to cut half my code off!

DBCmd =New SqlCommand("INSERT INTO aspnet_Users(MobileAlias) VALUES (@.Applicant) WHERE UserName=@.UserName", DBConn)

That was the full sql command I was using. I have tried several different variations of this with backets etc arond the WHERE part but just keeps throwing up the same error.

|||

You cannot use INSERT with WHERE. It doesnt make sense. You just INSERT into the table. You can SELECT/UPDATE with WHERE.

|||

How'd I miss that? I'd say that was pretty obvious.

|||

Thanks so much!!! Got it working now with the UPDATE & WHERE. Makes sense now. Sorry for wasting your time...

SuperSharp

No comments:

Post a Comment