Wrox Programmer Forums
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 26th, 2006, 04:03 PM
Authorized User
Join Date: Dec 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Functions

I have spent a week and a half trying to figure this out. Can someone help me? Here is the error

Incorrect syntax near 'fn_salesrep_lookup_custzip'.
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 'fn_salesrep_lookup_custzip'.

[SqlException (0x80131904): Incorrect syntax near 'fn_salesrep_lookup_custzip'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) +857626
   System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) +735238
   System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) +188
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
   System.Data.SqlClient.SqlDataReader.ConsumeMetaDat a() +31
   System.Data.SqlClient.SqlDataReader.get_MetaData() +62
   System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
   System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +886
   System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
   System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior behavior, String method) +122
   System.Data.SqlClient.SqlCommand.ExecuteReader() +84
   ASP.findrep_aspx.fn_salesrep_lookup_custzip(Object strZipCode) +146
   ASP.findrep_aspx.FindRep_Click(Object s, ImageClickEventArgs e) +34
   System.Web.UI.WebControls.ImageButton.OnClick(Imag eClickEventArgs e) +105
   System.Web.UI.WebControls.ImageButton.RaisePostBac kEvent(String eventArgument) +115
   System.Web.UI.WebControls.ImageButton.System.Web.U I.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

Here is my code

Sub FindRep_Click(s As Object, e As ImageClickEventArgs)


End Sub

Function fn_salesrep_lookup_custzip(strZipCode)

    Dim strConString as String
    Dim conZip as SqlConnection
    Dim cmdFindRep as SqlCommand
    Dim parmName As SqlParameter
    Dim dtrRep as SqlDataReader

    strConString = ConfigurationSettings.AppSettings("constring")
    conZip = New SqlConnection(strConString)

    cmdFindRep = New SqlCommand("fn_salesrep_lookup_custzip", conZip)
    cmdFindRep.Parameters.Add("@CustomerZip", strZipCode)
    dtrRep = cmdFindRep.ExecuteReader()
    dlstRep.DataSource = dtrRep


End Function

   runat="server" />
   runat="server" />

                       <td align="right">Name:</td>
                       <td><%# Container.DataItem("Name")%></td>
               <td align="right">Address:</td>
               <td><%# Container.DataItem("Address")%></td>
               <td align="right">City:</td>
               <td><%# Container.DataItem("City")%></td>
                   <td align="right">State:</td>
                   <td><%# Container.DataItem("State")%></td>
               <td align="right">Zip Code:</td>
               <td><%# Container.DataItem("Zip")%></td>
               <td align="right">Phone Number:</td>
               <td><%# Container.DataItem("Phone")%></td>
               <td align="right">Email:</td>
               <td><%# Container.DataItem("email")%></td>

This is a function that someone else put together

CREATE FUNCTION dbo.fn_salesrep_lookup_custzip(@CustomerZip CHAR(6))
    RETURNS @retSalesRep TABLE
        -- Columns returned by the function
        Name nvarchar(50) PRIMARY KEY NOT NULL
        ,Address nvarchar(50) NULL
        ,City nvarchar(50) NULL
        ,State nvarchar(50) NULL
        ,Zip nvarchar(50) NULL
        ,Phone nvarchar(50) NULL
        ,email nvarchar(50) NULL
           @Name nvarchar(50)
          ,@Address nvarchar(50)
          ,@City nvarchar(50)
          ,@State nvarchar(50)
          ,@Zip nvarchar(50)
          ,@Phone nvarchar(50)
          ,@email nvarchar(50);

  -- Get contact information
           @Name =LTRIM(RTRIM(T1.[SM-NAME]))
          ,@Address =LTRIM(RTRIM(T1.[SM-ADDRESS1]))+
                             WHEN ISNULL(T1.[SM-ADDRESS2],'')='' THEN ''
                             ELSE ', '+LTRIM(RTRIM(T1.[SM-ADDRESS2]))
          ,@City =LTRIM(RTRIM(T1.[SM-CITY]))
          ,@State =LTRIM(RTRIM(T1.[SM-STATE]))
          ,@Zip =CASE
                        WHEN LEN(LTRIM(RTRIM(T1.[SM-ZIP])))=6 AND LEFT(LTRIM(T1.[SM-ZIP]),1)='0' THEN RIGHT(RTRIM(T1.[SM-ZIP]),5)
                        ELSE LTRIM(RTRIM(T1.[SM-ZIP]))
          ,@Phone =LTRIM(RTRIM(T1.[SM-PHONE]))
          ,@email ='email not yet available'
        FROM dbo.SalesRep T1
           INNER JOIN dbo.SalesRepCustomerZip T2 ON T1.[SM-NBR]=T2.[SM-NBR]
                ELSE LTRIM(RTRIM(T2.ZIP))

        -- Return the information to the user
        IF @CustomerZip IS NOT NULL AND @Name IS NOT NULL
           INSERT @retSalesRep
           SELECT @Name, @Address, @City, @State, @Zip, @Phone, @email;

Old December 26th, 2006, 04:19 PM
Authorized User
Join Date: Dec 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts

I forgot to add that if I change these lines

cmdFindRep = New SqlCommand("fn_salesrep_lookup_custzip", conZip)
cmdFindRep.Parameters.Add("@CustomerZip", strZipCode)

to this

cmdFindRep = New SqlCommand("SELECT Name, Address, City, State, Zip, Phone, email FROM fn_salesrep_lookup_custzip(63119)", conZip)

Then everything works and the correct sales rep shows up for zip 63119

Old December 26th, 2006, 05:30 PM
Authorized User
Join Date: Dec 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts

Never mind. I finally got it

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Functions michael.boucher SQL Server 2005 2 May 9th, 2008 03:17 AM
SQL date functions frankb522 Other Programming Languages 2 June 24th, 2006 09:04 AM
SQL Max() and Min() functions help shana Oracle ASP 1 April 25th, 2005 08:34 PM
SQL Max() and Min() Functions Help need... shana Access 1 September 7th, 2004 09:22 AM
sql server functions example mateenmohd Classic ASP Databases 3 June 12th, 2003 05:54 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.