Beginner's Question--accessing database server

G

gary

Hello I am trying to access my data using some of the examples on the VB Resource CD to learn about VB.net. I have used the following code:

Public Sub InsertRow(ByVal myConnectionString As String)
MyConnectionString = "database=Northwind;Server=localhost"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myConnection As New SqlConnection
Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')"
Dim myCommand As New SqlCommand(myInsertQuery)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
End Sub 'SelectSqlClientSrvRows


When I click f5 and run the app, I get no compile errors. When I click on the button, however and attempt to access the server, I get the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: The ConnectionString property has not been initialized.

Additional information is as follows:
1. I have Visual Studio.net installed with MSDE on my computer. In my server explorer I have the following items showing.
SQL Servers ---GARYLAP\INSTANCE--------

2. Under the GARYLAP\INSTANCE I have the following databases:
Master
Model
MSDB
Northwind

I have modified the connection string until I finally gave up. I tried server =garylap and garylap\instance but to no avail. Any help would be greatly appreciated.

Thanks,

Gary
 
H

Hussein Abuthuraya[MSFT]

Gary,

In your code, you didn't set the connectionstring for the connection object
unless you didn't include it all. You have InsertRow function that takes
one parameter but it does nothing. Propably what you need to do is:

- Remove InsertRow function since it is redundant.
- Modify Button2 click event code to:

Dim ConStr as String =
"Server=yourMSDEinstancename;database=Northwind;Integrated security=SSPI"
Dim myConnection As New SqlConnection(ConStr)
Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID,
CompanyName) Values('NWIND', 'Northwind Traders')"
Dim myCommand As New SqlCommand(myInsertQuery)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
End Sub 'SelectSqlClientSrvRows

I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2002 Microsoft Corporation. All rights
reserved

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
G

Guest

You may also need to change the server name from "localhost" to "(local)" or to the IP address "127.0.0.1". Localhost may not work because of name resolution issues unrelated to your code issues. I had that problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top