Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SqlConnection Not Defined 1

Status
Not open for further replies.

jalbao

Programmer
Nov 27, 2000
413
US
In an attempt to learn how to add a record to an existing table in my sql server database, I have created a form that contains nothing but a button. When the button is clicked I want to add a record to my table (tblController).

The code that I am using is adapted from an msdn doc.

Here's the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConnectionString As String = _
"Data Source=(local);Initial Catalog=Trackster;user id=foo; password=bar"
Dim myConnection As New SqlConnection(myConnectionString)
Dim myInsertQuery As String = "INSERT INTO tblController (sName) Values('this is a test')"
Dim myCommand As New SqlCommand(myInsertQuery)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()

End Sub


Here's the problem:

When I attempt to compile the code, I receive a couple of error messages:
1. Type 'SqlConnection' is not defined
2. Type 'SqlCommand' is not defined

Can anyone fill me in on why I am receiving this error messages and how to fix the problem. If you don't know what's causing the error, but have a working sample of how to add a record to a table, that would be great too!

I am running:
Visual Studio .NET 2003
SQLServer 2000 Enterprise
Windows 2000

btw: I am an absolute beginner to VB / Studio.

Thanks
 
I discovered a fix for my problem.

In the myConnection and myCommand declarations I have to include the entire path to the SqlConnection and SqlCommand methods.

So the updated declarations look like this:
Dim myConnection As New _
System.data.sqlclient.SqlConnection(myConnectionString)

Dim myCommand As New _
System.Data.SqlClient.SqlCommand(myInsertQuery)

Can anyone tell me why this fixes the problem. I am confused because ALL the samples that I have read do not include the entire path - I'm thinking that the chances of ALL the samples being typo's are very slim. There must be something that I am missing or simply do not understand as to when a method is or is not defined.
 
You have to realize that the namespaces are in a sort of heirchy. The books don't list the entire qualifying namespace because at the top of the code section, before the class declaration, they have:

Code:
Imports System.Data.SqlClient
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top