February 19th, 2013, 01:51 PM
Help with connecting to DB and searching row.
I am new with asp.net.
I have read and tried a lot but with no result.
I have a table MS SQL :
id start end distance
1 madrid paris 300
2 moscow pitsburg 1000
3 NY california 2000
I also have 2 textboxes. so if first txtbox = moscow and second txtbox = pitsburg , I want to print 1000.
please help me.
February 19th, 2013, 04:01 PM
Dim connectionString As String
Dim sql As String
Dim pwd As String
Dim user As String
Dim sqlserv As String
Dim sqldb As String
sqlserv = localhost\sqlexpress 'your server
sqldb = mydbs 'the database
user = sa 'user who has access to the db
pwd = admin 'user's password
'The connection string below
connectionString = "Data Source=" & sqlserv & ";Initial Catalog=" & sqldb & ";User ID=" & user & ";Password=" & pwd
Dim sqlSelect As String
'SQL select below dbo.Table is your table which contains the information, also you can order if you want
sqlSelect = "SELECT Id, Start, End, Distance FROM dbo.Table WHERE (Start = '" & TextBox1.Text & "') AND (End = '" & TextBox2.Text & "' ORDER BY Distance "
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
sqlCnn = New SqlConnection(connectionString)
Dim dataadapter As New SqlDataAdapter(sqlSelect, connectionString)
Dim distance As string
sqlCmd = New SqlCommand(sql, sqlCnn)
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
Catch ex As Exception
MessageBox.Show("Houston we have a problem...", "Error")
distance = sqlReader.Item("distance")
February 19th, 2013, 05:46 PM
Originally Posted by garik111