PDA

View Full Version : select From table and print result to screen like php ..How?


maxvee8
03-25-2008, 09:09 PM
do a simple select from a database and print the result to the screen how is this done without using controls???

like you would with php echo $row[whateava]

Freon22
03-25-2008, 10:03 PM
Well doing that is not dot net, vbscript is closest thing to what you are asking for.

You can use the Response.Write() in dot net or vbscript. Guess am not sure what you mean by "without controls". If you mean you don't want to write it to a label, gridview, or something then Response.Write() will just print out whatever onto the screen.

Response.Write("Hello World!")
You can ever put html in it if you want.
Response.Write("<font color='Blue'>Hello World!</font>")

The simplest query I guess would be SELECT * FROM myTable that will select all the data from that table. You will still have to open up a connection to the database. Write some code and when you get stuck ask. :-)

Take Care

maxvee8
03-25-2008, 11:43 PM
how would i make a select all from a database and then out put the data in to labels say just one line where it equaled a certain value

SELECT * FROM table WHERE ID=ID

then out put them in to labels

<label>Row[item1]</label> <label>Row [Item2]</label> <label>Row[Item3]</label>

____________

i have no clue where to really start with this im so used to php i want to just connect t oa database loop the row where it equals a value

Freon22
03-26-2008, 02:04 PM
I don't have much time, so here is a fast one. I have the database connection string in the web.config file. It makes it easyer putting it there then having to type it out each time. I am using the dataReader to read because it is fast and easy to use.

Imports System.Data
Imports System.Data.SqlClient 'Make sure you import this.
Imports System.Web.Configuration 'Make sure you import this.


'***** Calling a database Using a DataReader ********

Dim connectionString As String = _
WebConfigurationManager.ConnectionStrings("myDatabase").ConnectionString 'My connection string is in my web.config file.

Dim selectSQL As String = "SELECT * FROM myTable WHERE ID = 5 ;"

Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(selectSQL, con)
Dim reader As SqlDataReader

Try
con.Open()
reader = cmd.ExecuteReader()
reader.Read()
If reader.HasRows Then
Label1.Text = reader("firstField")
Label2.Text = reader("secondField")
Label3.Text = reader("andSo..on")
reader.Close()
con.Close()
End If
Catch ex As Exception
'ToDo: Do something with this exception, like send them to an error page or something.
Finally
reader.Close()
con.Close()
End Try