Double click Default.aspx in Solution Explorer to open the Design view of the web page. A Button and a Textbox component should already be there. (We added them earlier.) Double click the Button and a code window for Default.aspx.vb should open with the subroutine event code already entered for you.
To make our code a little simpler, you can enter an Imports statement at the top:
Imports System.Data.SqlClient
We're going to code a "connected" access to make the code as simple as possible. To start, add these statements to the subroutine.
Dim myCommand As New SqlCommand()
Dim myConnection As New SqlConnection
Your system now has to be able to actually find the database. It does this (and other things) by using the "connection string" that is a "property" of the connection that the second statement creates. This is where a lot of the instruction you might find really breaks down because there are a lot of different ways to do this. That's because this is where much of the flexibility of SQL Server is built in. And where there are a lot of choices, there's a lot of confusion too. Fortunately, when you created the database, VWD created the connection string too. You just need to know where to find it.
Click myTestDB in Database Explorer and look for the Properties window. The connection string is right there. All you have to do is select it and copy it to use it in your code. (It's "greyed out" but you can select and copy it anyway.)
Enter the statement ...
myConnection.ConnectionString = "<paste from your computer>"
It will be a long statement. And the line of code will display the error: "Character constant must contain exactly one character." when you rest the mouse pointer on it. This has to be one of least helpful messages you will see. (WHY can't Microsoft do a better job on error messages? I don't know.) What they're trying to tell you is that you have too many quote marks. This is the "human" meaning of the connection string:
"part of the string "more of the string" the rest of the string"
VWD looks at it this way:
"String One" something that is not recognizable at all "String Two"
(Part of the skill of being a programmer is learning to think like the computer thinks.)
To fix this, just substitute single quotes for the inner double quotes. (They're around the AttachDbFilename parameter in the connection string.)
Now we're ready to execute some database code. Enter the statements:
myCommand.Connection = myConnection
myCommand.CommandText = "SELECT * FROM myTestTable"
myConnection.Open()
Dim myDataReader As SqlDataReader
myDataReader = myCommand.ExecuteReader()
myDataReader.Read()
TextBox1.Text = myDataReader("myDataColumn")
myConnection.Close()
That's it. You're ready to roll. If you've done everything just right and the moon is in the second house and Jupiter aligns with Mars (just kidding), you should be able to click the green arrow to run your form. (Reply "yes" to the two confirmation screens that might pop up.)
This will display an Untitled Page with a button and a text box. Click the button and the value you entered into the first record will be entered into the text box.
Hey! It's not going to win the Turing Award but it's a start.
In the next lesson, VWD in more depth, I cover some of the unique features of our free development tool, VWD, to give you the foundation you'll need for the remainder of the course. To make sure the lesson sinks in, I also create the version of web based system for sending email using VWD.

