Reputation: 4864
We would like to update data in a SQL Server 2012 database with a value obtained from
changing a value on an ASP.Net DetailsView
. I Would would like to update the database using
DataSetParentsDetails
ParentsDetailsTableAdapter
ParentsDetails
.These were created with the DataSet Designer.
This is the code from the code-behind file used to figure out the amount we want to update into the database:
Protected Sub DetailsViewDetails_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)
Dim dcmAmountToAdjust As Decimal
Dim StrSqlStatement As String
Select Case e.CommandName
Case "Add"
Case "Edit"
dcmOriginalRegistrationFee = GetValueFromLabelRegistrationFee()
Case "Delete"
Case "Update"
dcmNewRegistrationFee = GetValueFromTextBoxRegistrationFee()
dcmAmountToAdjust = dcmNewRegistrationFee - dcmOriginalRegistrationFee
' Update the tuition balance in the parent's data.
'-------------------------------------------------
StrSqlStatement =
"Update Students " & _
"Set RegistrationCode = RegistrationCode + @AmountToAdjust " & _
"Where StudentID = @ID"
' Code to update the database goes here.
'---------------------------------------
End Select
End Sub
I'm sure that this was asked many times before but I can't find a good example on how to use the query in: StrSqlStatement
to update the database through the strongly typed DataSet.
Upvotes: 1
Views: 1486
Reputation: 6372
First off you need a connection string, it's good practise to store your connection strings in the web.config
file:
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=putYourServerAndInstanceNameHere;Initial Catalog=putYourDatabaseNameHere;User ID=putYourSqlUsernameHere;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
This is a direct child of the root <configuration>
element. For more information about connection strings, visit http://www.connectionstrings.com.
Then you'll need some imports in your code-behind, and you'll need to add them as references to your project if you haven't already got them in there:
Import System.Data
Import System.Data.SqlClient
Then we connect to the database and run our command, we use parameters because they're more secure.
'build the connection object using the string from the web.config file
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
'build the command object specifying the command text and the connection to use, conn
Using cmd As New SqlCommand("UPDATE Students SET RegistrationCode = RegistrationCode + @AmountToAdjust WHERE StudentID = @ID", conn)
'add the parameters needed by the command
cmd.Parameters.AddWithValue("@AmountToAdjust", amountToAdjust)
cmd.Parameters.AddWithValue("@ID", studentID)
'try to open the connection and execute the statement
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
'handle the exception here
End Try
End Using
End Using
Note that there is no need to use conn.Close()
here as the Using
statement will take care of that for you (SqlConnection's Dispose method closes the connection if it is still open).
Upvotes: 5