Sunday, March 25, 2012

Bind computed textbox value to database field

Hi all,

I have three textboxes in a form view

1- Qty textbox

2- price textbox

3-TotalPrice textbox which value is the result of multiplying the previous two values

and then I want to Bind() the third value to the totalprice field in the database

how can I do that??

(without making the database field as a computed column)

thanks for any help

1. What database?
2. What language?
3. How are you retrieving the value of the third textbox currently?
4. How are you performing data access? SqlDataSource controls? ADO.NET code in your code-behind? Custom business classes? Typed DataSets?

You really should provide more information so that people can help you. The answer to your question is very simple. You simply run an Insert statement against the database. However, I suspect that no one has bothered to reply so far because how you run the insert and in what language isn't clear from your post.

If you can work out what to do from the "run an Insert statement" answer, that's great. But if you want some sample code offered, help us to help you.Big Smile

|||

Hi

Thanks for reply,

1- SQL Express database

2- langauge= VB

3- I don't know (someone has made a visual basic 6 application and I don't have the source code of it so I am converting it into ASP.net)

4- I am using SQL data source control to select,update,insert and delete the records from the database

I want to make the third text box value as a result of multiplying the first two values

And then bind that value to the database

Thank you

|||

Frankly, I'm not sure about the need for a 3rd textbox. Your InsertCommand for the SqlDataSource should be something like "Insert Into mytable (price, quantity, total) Values (@.price, @.quantity, @.total)". You need 3 InsertParameters. The first 2 will be <asp:ControlParameters>, and the ControlID will be the textboxes for price and quantity. The third will be a straightforward <asp:parameter> of type Int32.

Create a SqlDataSource_Inserting event by selecting the datasource control in design view, then hit F4 to bring up the porperties. Double click the lightning bolt to bring up the events and double click the Inserting event. That will create code for handling the event. The code is straightofrward, something along the lines of:

Dim price As Integer = Convert.ToInt32(TextBox1.Text)
Dim qty As Integer = Convert.ToInt32(TextBox2.Text)
Dim total As Integer = price * qty

Then just add code to set the value of the plain parameter to the calculation:

SqlDataSource.InsertParameters("total").DefaultValue = total

No comments:

Post a Comment