Insert Statement for ASP.NET - Syntax

Results 1 to 2 of 2

Thread: Insert Statement for ASP.NET - Syntax

  1. #1
    Join Date
    Dec 1969

    Default Insert Statement for ASP.NET - Syntax

    Hi,<BR>I am using to insert values from a web form into a sql database.<BR>My insert statement is:<BR>cmdIN.CommandText = "INSERT INTO SuggestPosts(PName, PTitle) VALUES(Poster, PostTitle)"<BR><BR>I have declared the Poster variables and still get an error like:<BR>Dim Poster As String<BR> Dim PostTitle As String<BR><BR> Poster = SuggestName.Text<BR> PostTitle = SuggestTitle.Text<BR><BR>But I get the following error:<BR>The name &#039;Poster&#039; is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. <BR><BR>Thanks Lizelle<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Insert Statement for ASP.NET - Syntax

    If you want to parameterize the INSERT statement, the syntax depends on the database-specific class library you&#039;re using.<BR><BR>For an OracleCommand, the adjusted syntax would read:<BR><BR>"INSERT INTO SuggestPosts(PName, PTitle) VALUES(:Poster, :PostTitle)"<BR><BR>For generic OLEDB or ODBC:<BR><BR>"INSERT INTO SuggestPosts(PName, PTitle) VALUES(?, ?)"<BR><BR>I don&#039;t know about SQL Server.<BR><BR><BR>Furthermore, you need to programmatically define each parameter and assign it&#039;s value (sorry, my examples are in C#, it should be enough to get the idea across):<BR><BR>cmdIN.Parameters.Add("Poster", OracleType.Varchar, 10, null);<BR>cmdIN.Parameters.Add("PostTitle", OracleType.Varchar, 10, null);<BR><BR>The first argument is the parameter name from your SQL statement. Finally, assign the values:<BR><BR>cmdIN.Parameters["Poster"].Value = SuggestName.Text;<BR>cmdIN.Parameters["PostTitle"].Value = SuggestTitle.Text;<BR><BR>Then you can do the insert:<BR><BR>cmdIN.ExecuteNonQuery();<BR><BR>Goo d Luck,<BR><BR>Mike<BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts