#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Help with connecting to DB and searching row.


    Hi
    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.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Hello,

    in VB.NET:


    Code:
    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
    
            Try
                sqlCnn.Open()
                sqlCmd = New SqlCommand(sql, sqlCnn)
                Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
    While sqlReader.Read() distance = sqlReader.Item("distance") End While
    sqlReader.Close() sqlCmd.Dispose() sqlCnn.Close() MessageBox.Show(distance, "Result") Catch ex As Exception MessageBox.Show("Houston we have a problem...", "Error") End Try
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    C#?


    Originally Posted by garik111
    Hi
    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.
    Maybe C#??

IMN logo majestic logo threadwatch logo seochat tools logo