Tuesday, March 20, 2012

Pulling 2 values from 2 textboxes displayed in a third

I have a problem trying to get a value from textbox 1 and textbox 2.
If those two values match the data in the database then it has to
return a third corresponding value to a third textbox (or label) after
a user clicks a submit button. Basically, I am trying to allow users
to enter a value of weight in pounds in textbox 1 and a zip code in
textbox 2 then pulling the shipping amount from a courier's database
and returning that dollar amount to a third textbox or label after
they click submit. How would I write this code in vb.NET?

Thanks,
TGHere's an example to get you started:

--schema
CREATE TABLE ShippingCharges
(
ShippingWeight int NOT NULL,
PostalCode varchar(5) NOT NULL,
ShippingCharge decimal(9, 2) NOT NULL
CONSTRAINT PK_ShippingCharges
PRIMARY KEY(ShippingWeight, PostalCode)
)

'VB.Net snippet
Try
Dim connection As New SqlConnection("Data
Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=SSPI")
connection.Open()
Dim command As New SqlCommand("SELECT @.ShippingCharge =
ShippingCharge FROM ShippingCharges WHERE PostalCode = @.PostalCode AND
ShippingWeight = @.ShippingWeight", connection)
command.Parameters.Add(New SqlParameter("@.ShippingCharge",
SqlDbType.Decimal))
command.Parameters("@.ShippingCharge").Direction =
ParameterDirection.Output
command.Parameters("@.ShippingCharge").Precision = 9
command.Parameters("@.ShippingCharge").Scale = 2
command.Parameters.Add(New SqlParameter("@.ShippingWeight",
Convert.ToInt32(Me.TextBox1.Text)))
command.Parameters.Add(New SqlParameter("@.PostalCode",
Me.TextBox2.Text))
command.ExecuteScalar()
If command.Parameters("@.ShippingCharge").Value Is DBNull.Value
Then
MessageBox.Show("No shipping charge found")
Else
Me.TextBox3.Text =
command.Parameters("@.ShippingCharge").Value.ToString()
End If
connection.Close()
Catch ex As SqlException
MessageBox.Show(ex.ToString)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

--
Hope this helps.

Dan Guzman
SQL Server MVP

"TG" <tjgraham4@.hotmail.com> wrote in message
news:4822c297.0409231326.4a2a892a@.posting.google.c om...
>I have a problem trying to get a value from textbox 1 and textbox 2.
> If those two values match the data in the database then it has to
> return a third corresponding value to a third textbox (or label) after
> a user clicks a submit button. Basically, I am trying to allow users
> to enter a value of weight in pounds in textbox 1 and a zip code in
> textbox 2 then pulling the shipping amount from a courier's database
> and returning that dollar amount to a third textbox or label after
> they click submit. How would I write this code in vb.NET?
> Thanks,
> TG

No comments:

Post a Comment