1

I have a SQL table with three columns "From","To" and "Equivalent Value". Each value is shown below:

From        To           Equivalent Value
1,001.00    2,000.00     200.00
2,001.00    3,000.00     300.00

Now if the user enters the value "1,200.00" in textbox1 it will display the result value to textbox2 which is "200.00" because that is the corresponding value of between "From" and "To.

Another condition, if the user enters the value "2,500.00" in textbox1 it will display the value "300.00".

So far, I have tried this code but no luck:

Dim conn As SqlConnection = SQLConn()
Dim da As New SqlDataAdapter
Dim dt As New DataTable

conn.Open()

Dim cmd As New SqlCommand("", conn)
Dim result As String

cmd.CommandText = "SELECT [Equivalent Value] FROM tblSSS"
result = IIf(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar)
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)

If result <> "" Then
    If TextBox1.Text >= dt.Rows(0)(1).ToString() And TextBox1.Text <= dt.Rows(0)(2).ToString() Then
        TextBox2.Text = dt.Rows(0)(3).ToString()

    End If
End If
Bugs
  • 4,491
  • 9
  • 32
  • 41
Jaron2016
  • 29
  • 1
  • 6
  • And what have you tried so far? – A Friend Jan 31 '17 at 08:11
  • Can you explain a bit more? – Dieter Meemken Jan 31 '17 at 08:13
  • You need to use an event with your TextBox1. I'm not sure which one is the best in your case. Check the [MSDN](https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events(v=vs.110).aspx) to find the one you need. – nbadaud Jan 31 '17 at 08:21
  • 2
    No need to select the entire content of the table, when you can use SQL's `WHERE` clause to return only the results you want. What if your table had a million rows? Learn about Parameters and how to use them in SQL. – Zohar Peled Jan 31 '17 at 08:58
  • What equivalent value do you want if the value is 2000.50? – Andrew Morton Jan 31 '17 at 19:05

2 Answers2

2

If I have got this right I think there are a couple of things I would change which may help you:

  1. Use Using. This will dispose of the SQL objects once finished with.
  2. Use SqlParameters. This will help with filtering your data.
  3. Remove the use of SqlDataAdapter. In this case I don't feel it's needed.
  4. The use of IIf. I will be using If which has replaced IIf.

With these in mind I would look at something like this:

Dim fromValue As Decimal = 0D
Dim toValue As Decimal = 0D

If Decimal.TryParse(TextBox1.Text, fromValue) AndAlso Decimal.TryParse(TextBox1.Text, toValue) Then

    Dim dt As New DataTable

    Using conn As SqlConnection = SQLConn,
          cmd As New SqlCommand("SELECT [Equivalent Value] FROM tblSSS WHERE [From] >= @From AND [To] <= @To", conn)

        cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@From", .SqlDbType = SqlDbType.Decimal, .Value = fromValue})
        cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@To", .SqlDbType = SqlDbType.Decimal, .Value = toValue})

        conn.Open()

        dt.Load(cmd.ExecuteReader)

    End Using

    If dt.Rows.Count = 1 Then
        TextBox2.Text = If(IsDBNull(dt.Rows(0).Item("Equivalent Value")), "0", dt.Rows(0).Item("Equivalent Value").ToString)
    End If

End If

Note the use of Decimal.TryParse:

Converts the string representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed.

This is an assumption that the From and To fields in your database are Decimal.

Now to explain the difference between IIf and If. IIf executes each portion of the statement even if it's true whilst If executes only one portion. I won't go into detail as many others on here have done that already. Have a look at this answer.

As per Andrew Morton's comment and more in line with what the OP attempted here is a solution that uses ExecuteScaler.

ExecuteScaler executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

With this in mind:

'I reset the value of TextBox2.Text. You may not want to.
TextBox2.Text = ""

Dim fromValue As Decimal = 0D
Dim toValue As Decimal = 0D

If Decimal.TryParse(TextBox1.Text, fromValue) AndAlso Decimal.TryParse(TextBox1.Text, toValue) Then

    Using conn As SqlConnection = SQLConn,
          cmd As New SqlCommand("SELECT [Equivalent Value] FROM tblSSS WHERE [From] >= @From AND [To] <= @To", conn)

        cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@From", .SqlDbType = SqlDbType.Decimal, .Value = fromValue})
        cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@To", .SqlDbType = SqlDbType.Decimal, .Value = toValue})

        conn.Open()

        Try
            TextBox2.Text = cmd.ExecuteScalar().ToString()
        Catch ex As Exception

        End Try

    End Using

End If

I have used the example on the ExecuteScaler MSDN documentation. You might want to look into handling the exception on the Try Catch a little better and not letting it go to waste.

You may want to place this code on the TextBox1.Leave method or maybe on a Button.Click method. That's totally up to you.

There may a few changes you may need to make however I think this will give you a few ideas on how to move ahead with your code.

Community
  • 1
  • 1
Bugs
  • 4,491
  • 9
  • 32
  • 41
-3

Hope it Helps...

Dim connetionString As String
Dim cnn As SqlConnection
Dim cmd As SqlCommand
Dim sql As String

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "SELECT [Equivalent Value] FROM tblSSS WHERE [FROM]<=" & Val(TextBox1.Text) & " AND [TO]>= " & Val(TextBox1.Text)

cnn = New SqlConnection(connetionString)
Try
    cnn.Open()
    cmd = New SqlCommand(sql, cnn)
    Dim count As Int32 = Convert.ToInt32(cmd.ExecuteScalar())
    cmd.Dispose()
    cnn.Close()

Catch ex As Exception
    MsgBox("Can not open connection ! ")
End Try
Bugs
  • 4,491
  • 9
  • 32
  • 41
  • Please, add more detail. – GiamPy Jan 31 '17 at 11:41
  • 1
    Sorry, buggy and wide open to SQL injection code deserves downvote. – Arvo Jan 31 '17 at 13:26
  • Have a look at [this](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work?noredirect=1&lq=1) Kunal. Parameters should always be suggested when providing an answer regarding SQL, even if it's to fix a slight syntax issue. – Bugs Feb 01 '17 at 13:46