Solved

Can't see the error

Posted on 2006-10-25
14
602 Views
Last Modified: 2008-02-01
Hi,
My code behind gathers the input parameters and calls an SP.
This SP contains an insert statement.

When I run my aspx page, fill in the form and hit submit I get an error:
System.Data.SqlClient.SqlException was unhandled by user code
  Class=15
  ErrorCode=-2146232060
  LineNumber=1
  Message="Line 1: Incorrect syntax near 'InsertNewVApplication'."
  Number=170
  Procedure=""
  Server="devBoxDVMAIN"
  Source=".Net SqlClient Data Provider"
  State=1

judging by the error it looks like it is being returned from SQL.
I ran a trace and captured the code the .net page is passing. I then ran this code in query analyser and get the same error Incorrect syntax near 'InsertNewVApplication'.

The first part of the code is.....
exec sp_executesql N'InsertNewVApplication', N'@strSurname nvarchar(8),@strFirstName nvarchar(3),@strPrefName nvarchar(4000),@strOtherName nvarchar(4000),@strResAddress nvarchar(4000) etc etc

Anything obvious there?
The aspx code to call the SP is...
Dim Cmd As New Data.SqlClient.SqlCommand("InsertNewVApplication", Conn)
then we define the insert parameters
Conn.Open()
        Cmd.ExecuteNonQuery()
        Conn.Close()
        Cmd.Dispose()
0
Comment
Question by:QPR
  • 6
  • 3
  • 2
  • +3
14 Comments
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 17808464
hey no need for a " ("  i mean before starting parametre?
exec sp_executesql ('InsertNewVApplication',@strSurname nvarchar(8),@strFirstName nvarchar(3),@strPrefName nvarchar(4000),@strOtherName nvarchar(4000),@strResAddress nvarchar(4000) etc etc)
0
 
LVL 29

Author Comment

by:QPR
ID: 17808641
I don't put any "("s in there. e.g. nvarchar(8)
That is generated by VS/ADO/ODBC/I dont know  :)
0
 
LVL 29

Author Comment

by:QPR
ID: 17808643
like this

Dim Conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnString").ConnectionString)
        Dim Cmd As New Data.SqlClient.SqlCommand("InsertNewVApplication", Conn)
        With Cmd
            .Parameters.AddWithValue("@strSurname", strSurname)
            .Parameters.AddWithValue("@strFirstName", strFirstNames)
            .Parameters.AddWithValue("@strPrefName", strPrefName)
            etc etc
        End With

        Conn.Open()
        Cmd.ExecuteNonQuery()
        Conn.Close()
        Cmd.Dispose()
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Expert Comment

by:AaronAbend
ID: 17808932
try SqlCommand("exec InsertNewVApplication"
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17808939
Run the SQL server profiler first, and then your .Net Page.
Watch what is coming on the Profiler window.
Now copy it and run it on Query analyzer

Also remove the "  N'  " for nvarchar params

exec sp_executesql N'InsertNewVApplication', @strSurname nvarchar(8),@strFirstName nvarchar(3),@strPrefName nvarchar(4000),@strOtherName nvarchar(4000),@strResAddress nvarchar(4000)
0
 
LVL 29

Author Comment

by:QPR
ID: 17808942
Thatr is exactly what I did.
Extract from my original post....

I ran a trace and captured the code the .net page is passing. I then ran this code in query analyser and get the same error Incorrect syntax near 'InsertNewVApplication'.
0
 
LVL 29

Author Comment

by:QPR
ID: 17808945
try SqlCommand("exec InsertNewVApplication"

which line does this replace?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17808986

this will work in qa
exec sp_executesql InsertNewVApplication @strSurname nvarchar(8),@strFirstName nvarchar(3),@strPrefName nvarchar(4000),@strOtherName nvarchar(4000),@strResAddress nvarchar(4000)
0
 
LVL 29

Author Comment

by:QPR
ID: 17809055
So the problem is the N' before the SP name?
If so then how do I stop the aspx from generating this uneccesary char?
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17809205
put this at the bottom of the sproc and run the sproc from sql management with the params asp.net is passing in to see if sql is throwing error.


IF @@ERROR <> 0
  BEGIN
    select @@ERROR ;
  END
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 17809303
You need to set your command type to a stored procedure.  It's trying to evaluate it as a sql statement (Text).

Dim Cmd As New Data.SqlClient.SqlCommand("InsertNewVApplication", Conn)
cmd.CommandType = System.Data.CommandType.StoredProcedure

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17809304
>So the problem is the N' before the SP name?

I think there is a comma after that sp name also
0
 
LVL 29

Author Comment

by:QPR
ID: 17809587
Dean.... as I said in the original question. I have run the sql (that .NET is producing) in QA and yes it is SQL that is throwing the error.

Aneesh, the N' and , are both produced by .NET based on the above code

Clockwatcher, I'll give this a go when I'm back at work tomorrow.
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17824790
thats why you need to add error handling to sproc

IF @@ERROR <> 0
  BEGIN
    select @@ERROR ;
  END

it is hard to catch sql errors using cmd eg like a primary key violation especially if you are using executenonquery, maybe what you can do is do the insert, check amount of rows return and if -1 or 0 I think then run another sproc to get the last error thrown...

good luck
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calling web service and its methods dynamically 2 57
Reset asp.net login password 4 42
Code works but it's slow 28 70
Question about JQuery and asp.net 3 23
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now