[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to make a calculated field save into sql table?

Posted on 2005-05-15
1
Medium Priority
?
176 Views
Last Modified: 2010-04-07
Hi,
I have a form that has input,used and output,the output is a calculated field that i don't show in the form but i have it in my sql table,the user inserts output and used and i want to be able to calculate the output and save itdirectly to my sql table
i thought of doing a hidden field that reads from the input and used and then calculate it as follows:
output=input - used

but i don't know how to read from the fields ,below is my insert code:
*************************************************************************
Try
  Dim MySQL As String = "Insert into totals(input,used) values(@txttext1,@texttest2)"

 Dim myConn As SqlConnection = New SqlConnection("data source=dcmp;uid=sa;pwd=;database=bookshop")
            Dim Cmd As New SqlCommand(MySQL, myConn)
            Cmd.Parameters.Add(New SqlParameter("@txttext1", TextBox1.Text))
            Cmd.Parameters.Add(New SqlParameter("@texttest2", TextBox2.Text))


End Try
        BindData()
        'myConn.Close()

    End Sub

    Sub BindData()

        'Dim MySQL as string = "Select * from totals where [bookid] = @id"
        Dim myConn As SqlConnection = New SqlConnection("data source=dcmp;uid=sa;pwd=;database=bookshop")
        Dim MySQL As String
        MySQL = "Select * from totals order by dbookid "
        Dim Cmd1 As New SqlCommand(MySQL, myConn)
        'cmd1.Parameters.Add(New SqlParameter("@id", tmstm.text))

        Dim ds As DataSet = New DataSet
        Dim Cmd As New SqlDataAdapter(MySQL, myConn)
        Cmd.Fill(ds, "totals1")
        mydatagrid.DataSource = ds.Tables("totals1").DefaultView
        mydatagrid.DataBind()
***********************************************************************************

is it the right way to use hidden textbox or there is better way?
0
Comment
Question by:lion79
1 Comment
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 14007420
Aside from the fact that this is a really bad idea (this is why they invented Computed columns), this is how you would do it:

Try
  Dim MySQL As String = "Insert into totals(input,used, output) values(@txttext1,@texttest2, @outputvalue)"
 
 Dim myConn As SqlConnection = New SqlConnection("data source=dcmp;uid=sa;pwd=;database=bookshop")
            Dim Cmd As New SqlCommand(MySQL, myConn)
            Cmd.Parameters.Add(New SqlParameter("@txttext1", TextBox1.Text))
            Cmd.Parameters.Add(New SqlParameter("@texttest2", TextBox2.Text))
            Dim OutputValue As Integer = CType(TextBox1.Text, integer) - CType(TextBox2.Text, integer)
            Cmd.Parameters.Add(New SqlParameter("@outputvalue", OutputValue)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question