Microsoft KB Archive/316364

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

Article ID: 316364

Article Last Modified on 9/3/2003

-

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

-



This article was previously published under Q316364



For a Microsoft Visual C# .NET version of this article, see 316367.



For a Microsoft Visual Basic 6.0 version of this article, see 178070.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Description of the Technique
 * Requirements
 * Create Project and Add Code



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

back to the top

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&quot;b      'Not properly formatted back to the top

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

back to the top

Create the Project and Add the Code
 Start Visual Studio .NET. Create a new Windows Application in Visual Basic .NET. Make sure that your project contains a reference to the System.Data namespace, and add a reference if it does not. Place a CommandButton on Form1. Change the Name property of the button to btnTest, and then change the Text property to Test .  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   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(&quot;server=myServer;user id=myUID;password=a&quot;b;database=northwind&quot;) da = New SqlDataAdapter(&quot;select * from customers&quot;, cn) da.Fill(ds, &quot;customers&quot;) MsgBox(&quot;connected&quot;) </li> Modify the connection strings appropriately for your environment. Notice that the password has a quotation mark in it.</li> Save your project. On the Debug menu, click Start, and then run your project. Notice the build errors that you may encounter.</li>  Modify the connection string to: (&quot;server=myServer;user id=myUID;password='a&quot;&quot;b';database=northwind&quot;) </li> Save your project, and then run it.</li></ol>

back to the top

Keywords: kbhowtomaster kbsqlclient kbsystemdata KB316364

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.