Microsoft Knowledge Base Email Alertz

KBAlertz.com: (310368) - When you create a parameterized command against Microsoft SQL Server 7.0, if you call the Prepare method before you add parameters to the command, you receive the following error message: An unhandled exception of type...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 310368 - Last Review: May 13, 2007 - Revision: 2.7

Error when you try to call the Prepare method before you add parameters: "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll"

This article was previously published under Q310368
For a Microsoft Visual C# .NET version of this article, see 311348  (http://kbalertz.com/Feedback.aspx?kbNumber=311348/EN-US/ ) .

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

On This Page

SYMPTOMS

When you create a parameterized command against Microsoft SQL Server 7.0, if you call the Prepare method before you add parameters to the command, you receive the following error message:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll.
Additional information: System error.
This problem does not occur in SQL Server 2000.

CAUSE

This problem occurs in SQL Server 7.0 because, by design, you cannot run the Prepare method before you add parameters. This applies to most database systems.

SQL Server 2000 does not generate the above-mentioned exception because it does not run Prepare until the first command is executed. This optimization prevents the overhead of Prepare if no commands are subsequently executed.

RESOLUTION

To resolve this problem, do not call the Prepare method until after you add the parameters.

MORE INFORMATION

Steps to Reproduce the Behavior

The sample code to follow uses the Region table of the Northwind sample database.
  1. Start Microsoft Visual Studio .NET.
  2. Create a new Visual Basic Windows Application project. Form1 is added to the project by default.
  3. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
  4. Place a Button control on Form1. Change the Name property of the button to btnTest, and change the Text property to Test.
  5. Use the Imports statement on the System, System.Data.OleDb, and System.Data.SqlClient 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.OleDb
    Imports System.Data.SqlClient
    					
  6. Add the following code to the Code window after the "Windows Form Designer generated code" region.

    Note You must change User ID <username> and password =<strong password> to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database.
        Private Sub btnTest_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnTest.Click
            Dim myConnString As String = _
                "User ID=<username>;password=<strong password>;Initial Catalog=Northwind;Data Source=myServer"
            Dim id As Integer = 25
            Dim desc As String = "myFirstRegion"
            Dim rConn As SqlConnection = New SqlConnection(myConnString)
            rConn.Open()
            Dim command As SqlCommand = New SqlCommand("", rConn)
            command.CommandText = "insert into Region (RegionID, RegionDescription)" & _
                                  "values (@id, @desc)"
    
            'SQL Server 7.0 throws an exception here.
            'Comment the following line to resolve this problem against SQL Server 7.0.
            command.Prepare()
    
            command.Parameters.Add("@id", SqlDbType.Int, 4)
            command.Parameters.Add("@desc", SqlDbType.Char, 50)
            'You can call Prepare after you set up CommandText and parameters.
            command.Prepare() 
    
            command.Parameters(0).Value = id
            command.Parameters(1).Value = desc
            command.ExecuteNonQuery()
            MessageBox.Show("Updated Successfully")
        End Sub
    					
  7. Modify the connection string (myConnString) as appropriate for your environment.
  8. Save your project. On the Debug menu, click Start to run your project.
  9. Click Test. If you are connected to a SQL Server 7.0 database, the code generates the above-mentioned exception.

    If you are connected to a SQL Server 2000 database, the code runs properly, and the "Updated Successfully" message box appears.
  10. To resolve this problem against SQL Server 7.0, comment out the call to command.Prepare that precedes the code to add the parameters, and then run the project again.

REFERENCES

For more information on ADO.NET objects and syntax, refer to the following Microsoft .NET Framework Software Development Kit (SDK) documentation:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)

APPLIES TO
  • Microsoft ADO.NET 2.0
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
  • Microsoft SQL Server 7.0 Standard Edition
Keywords: 
kbtshoot kberrmsg kbnofix kbprb kbsqlclient kbsystemdata KB310368
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please