Showing posts with label textboxes. Show all posts
Showing posts with label textboxes. Show all posts

Tuesday, March 27, 2012

binding textbox text to a database field

hello

is it possible to to bind a single field from a database table to a textbox.

I am trying to open a web form with five textboxes on it. When it loads is it possible to have the textboxes filled with a different field from a row in a database, selected with a sql statment.

Thanks in advance NubNub

Wrong Forum.|||

Yes, that is quite possible.

But you will get better assistance if you post your question in one of the .NET ASP related forums.

sql

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

Bind a variable to a SQL data source for labels, textboxes, etc.

I am trying to set a variable to be passed to my asp page but the value is not carrying over. I don't even know if this is supposed to work or not below:

<tdcolspan="3"style="font-size: 10pt; vertical-align: top; color:<%=Div7fontcolor%>; background-color:<%=Div7bgcolor%>; text-align: center; width: 0px;"id="TopBox"runat="server">

The above Div7fontcolor and Div7bgcolor are variables that I set in the VB file as shown below:

Dim obConnectionAs SqlConnection =New SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=OrgBoard;Integrated Security=True")Dim obCommandAs SqlCommand =New SqlCommand("SELECT Divisions.*, Dept19.*, Dept20.*, Dept21.*, ConfigDisplay.* FROM Divisions CROSS JOIN Dept19 CROSS JOIN Dept20 CROSS JOIN Dept21 CROSS JOIN ConfigDisplay", obConnection)

obConnection.Open()

Dim drAs SqlDataReader = obCommand.ExecuteReader()

dr.Read()

Dim Div7bgcolorAsString = dr("Div7color").ToString().Trim()

Dim Div7fontcolorAsString = dr("Div7textcolor").ToString().Trim()

dr.Close()

obConnection.Close()

The web page runs fine with no errors but the value does not carry over and change the property correctly.

I am able to set the bgColor value of the TopBox within the VB file if I use "TopBox.BgColor = Div7bgcolor" but I can't set other values on my web page like the font color. If I can do this from the VB file then please correct me. Using variables within the page will allow me to set almost any value but I don't even know if what I want is possible. Any help is greatly apprectiated.

Thank you,

Kris

The easiest thing for this is to use Literal controls:

<tdcolspan="3"style="font-size: 10pt; vertical-align: top; color:<asp:Literal ID="Div7fontcolor" runat="server" />; background-color:<asp:Literal ID="Div7bgcolor" runat="server" />; text-align: center; width: 0px;"id="TopBox"runat="server">

....

dr.Read()

Div7bgcolor.Text = dr("Div7color").ToString().Trim()

Div7fontcolor.Text = dr("Div7textcolor").ToString().Trim()

dr.Close()