Results 1 to 9 of 9

Thread: VBNET2008 SQLSERVER 2000 SQL string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    207

    VBNET2008 SQLSERVER 2000 SQL string

    Hullo Good Guys,
    I need your help. Please help me.

    I am using VBNET2008 and SQL SERVER 2000.
    I tried to create this SQL string to retrieve CustomerID and a CustomerID and Name together but it's not working

    Here are the SQL String that is not working.
    Dim strsql As String = ""
    strsql &= "Select CustomerID, "
    strsql &= " ( CustomerID & " - " & CompanyName ) as [Companyname]"
    strsql &= " From TestCustomers Order by CompanyName"

    This is the error message
    Conversion from string "Select CustomerID, CustomerID & " to type 'Double' is not valid.

    Cause by this SQL String coding:
    strsql &= " CustomerID & " - " & CompanyName as [Companyname]"

    I am using the NORTHWIND DatacBase in SQL SERVER 2000 and the table is Customers
    Last edited by Lennie; Jan 11th, 2011 at 06:17 PM. Reason: type error

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VBNET2008 SQLSERVER 2000 SQL string

    Look at this part:
    Code:
    " ( CustomerID & " - " & CompanyName ) as [Companyname]"
    Does that look like a valid String to you?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VBNET2008 SQLSERVER 2000 SQL string

    that's because after your ampersand, you drop out of the string and then have a - ... which means subtract... so it tries to convert your string to a double to perform the arithmetic operation... but since the string can't be converted to a double, the error results.

    Going out on a limb, I'm going to guess that what you're trying to do is concatenate the ID with the company name with a - in the middle, right?

    Try this:
    Code:
    strsql &= " ( CustomerID & ' - ' & CompanyName ) as [Companyname]"
    changed the " to ' around the -

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VBNET2008 SQLSERVER 2000 SQL string

    Quote Originally Posted by techgnome View Post
    that's because after your ampersand, you drop out of the string and then have a - ... which means subtract... so it tries to convert your string to a double to perform the arithmetic operation... but since the string can't be converted to a double, the error results.

    Going out on a limb, I'm going to guess that what you're trying to do is concatenate the ID with the company name with a - in the middle, right?

    Try this:
    Code:
    strsql &= " ( CustomerID & ' - ' & CompanyName ) as [Companyname]"
    changed the " to ' around the -

    -tg
    Also, SQL Server doesn't use & to concatenate strings. That's a VB thing. T-SQL uses the more usual +. & is a bitwise AND.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    207

    Re: VBNET2008 SQLSERVER 2000 SQL string

    Jmcilhinney,
    You suggestion of sample code: strsql &= " ( CustomerID & ' - ' & CompanyName ) as [Companyname]"
    is not the way that I requested as per my posting earlier.

    This is what I wanted as per Business Analyst specficatino and requirements:
    strsql &= "CustomerID,"
    strsql &= " ( CustomerID & ' - ' & CompanyName ) as [Companyname]"

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VBNET2008 SQLSERVER 2000 SQL string

    Hang on... time out... you're saying the "wrong" solution that you don't want... is the same as the requirement... did I miss something?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    207

    Re: VBNET2008 SQLSERVER 2000 SQL string

    techgnome
    Your suggestion is similar to one from Jmcilhinney without complete SQL String that I posted.
    Because of that I could not understand it. Please try to be complete.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VBNET2008 SQLSERVER 2000 SQL string

    That's because that's the only line that's the problem... just simply fix the line you have...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: VBNET2008 SQLSERVER 2000 SQL string

    The line
    strsql &= " ( CustomerID & " - " & CompanyName ) as [Companyname]"

    should be this:
    strsql &= " ( CustomerID + ' - ' + CompanyName ) as [Companyname]"

    Is CustomerID as numeric or text field?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

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



Click Here to Expand Forum to Full Width