Article ID: 316364 - Last Review: September 3, 2003 - Revision: 2.1

HOW TO: Quote a Member of the Connection String in ADO.NET By Using Visual Basic .NET

This article was previously published under Q316364

On This Page

Expand all | Collapse all

SUMMARY

This article describes how to format a string if there is a quotation mark in a member of the connection string.

Description of the Technique

The sample code demonstrates how to format a password that includes a quotation mark. Without the proper formatting, the compiler generates build errors. The errors that you may receive include:
Comma, ')' , or a valid expression continuation expected.

Invalid Delimiter

Name 'connectionobject' is not declared.
If you are using a single quotation mark in your password, you have to use two double quotation marks to enclose the string:
password=a'b       'Not properly formatted
				
If you are using a double quotation mark in your password, you have to use two double quotation marks instead of one, and then you have to enclose the string with single quotation marks:
password=a"b       'Not properly formatted
				

Requirements

The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need to perform the procedure:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
  • Microsoft Visual Studio .NET
  • Microsoft SQL Server 7.0 or later
The following items describe what you need to understand before you use the information that is provided in this article:
  • Visual Studio .NET
  • ADO.NET fundamentals and syntax

Create the Project and Add the Code

  1. Start Visual Studio .NET.
  2. Create a new Windows Application in Visual Basic .NET.
  3. Make sure that your project contains a reference to the System.Data namespace, and add a reference if it does not.
  4. Place a CommandButton on Form1. Change the Name property of the button to btnTest, and then change the Text property to Test.
  5. Use the IMPORTS statement on the namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the General Declarations section of Form1:
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    					
  6. Copy and paste the following code in the Click event of btnTest:
            Dim da As SqlDataAdapter
            Dim ds As New DataSet()
            Dim cn As New SqlConnection("server=myServer;user id=myUID;password=a"b;database=northwind")
            da = New SqlDataAdapter("select * from customers", cn)
            da.Fill(ds, "customers")
            MsgBox("connected")
    					
  7. Modify the connection strings appropriately for your environment. Notice that the password has a quotation mark in it.
  8. Save your project. On the Debug menu, click Start, and then run your project. Notice the build errors that you may encounter.
  9. Modify the connection string to:
    ("server=myServer;user id=myUID;password='a""b';database=northwind")
    					
  10. Save your project, and then run it.

APPLIES TO
  • Microsoft ADO.NET 1.1
  • Microsoft ADO.NET 1.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbsqlclient kbsystemdata KB316364
 

Article Translations