When I click the "Commit Changes" button, I want it to save the changes to the record in the database.
I have some of the code, but am confused about how to go from here.
Any help would be great!!
Thank you:::
The code for the method that is called when the button is clicked::::
Protected Sub gridEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridEdit.SelectedIndexChanged
Dim myCommitConnection As Data.SqlClient.SqlConnection
Dim myCommitCommand As Data.SqlClient.SqlDataAdapter
Dim commitNow As New Data.SqlClient.SqlCommand
commitNow.CommandText = "UPDATE rate_Details SET depositType = depositType2, minToOpen = minToOpen2, maturity = maturity2, minBalanceToEarn = minBalanceToEarn2, interestRate = interestRate2, APY = apy, compFreq = compFreq2 WHERE rateID = rateID2"
myCommitConnection = New Data.SqlClient.SqlConnection("server=SNSQLDEV01;" & "database=StifelBank;Trusted_Connection=Yes")
myCommitCommand = New Data.SqlClient.SqlDataAdapter("UPDATE rate_Details SET depositType = depositType2, minToOpen = minToOpen2, maturity = maturity2, minBalanceToEarn = minBalanceToEarn2, interestRate = interestRate2, APY = apy, compFreq = compFreq2 WHERE rateID = rateID2", myCommitConnection)
myCommitConnection.Open()
commitNow.ExecuteNonQuery()
ContextUtil.SetComplete()
myCommitConnection.Close()
End Sub
Ribeyed
05-10-2007, 02:20 PM
hi again :)
whats not working or what part are you stuck on?
macker
05-10-2007, 02:25 PM
I really don't know what part is not working.....
It is just not saving the changes in the database.
Do I need to add parameters to that code, or something?
Thanks
Ribeyed
05-10-2007, 02:36 PM
yes you'll need to pass the information from your gridview to the SQLStatement
Is doesn't look like your passing in anything to this statement:
i knew you where going to ask that! Should have gave you some code lol.
UPDATE rate_Details SET depositType = @depositType2, minToOpen = @minToOpen2, maturity = @maturity2, minBalanceToEarn = @minBalanceToEarn2, interestRate = @interestRate2, APY = @apy, compFreq = @compFreq2 WHERE rateID = @rateID2"
That will make them parameters.
Then add this line for each paramater:
It is crashing on the line "commitNow.ExecuteNonScalar()"
i have tried ".ExecuteNonQuery()", and ".ExecuteScalar():
I get the same error with all of these......
"connection property has not been initialized"
I thought I did that, see what you think, i guess?????
Protected Sub gridEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridEdit.SelectedIndexChanged
Dim myCommitConnection As Data.SqlClient.SqlConnection
Dim myCommitCommand As Data.SqlClient.SqlDataAdapter
Dim commitNow As New Data.SqlClient.SqlCommand
commitNow.ExecuteNonScalar()
ContextUtil.SetComplete()
myCommitConnection.Close()
End Sub
Ribeyed
05-10-2007, 03:24 PM
I normally use stored procedures never did sql statements from within my code however you still have to open the sqlconnection first:
myCommitConnection.Open()
Then execute the nonquery on your adapter:
commitNow.ExecuteNonQuery()
Then close your connection
myCommitConnection.Close()
macker
05-10-2007, 03:30 PM
I have that already, and it does not seem to work, still crashing on the same line.
Protected Sub gridEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridEdit.SelectedIndexChanged
Dim myCommitConnection As Data.SqlClient.SqlConnection
Dim myCommitCommand As Data.SqlClient.SqlDataAdapter
Dim commitNow As New Data.SqlClient.SqlCommand
needs to be:
commitNow.Parameters("@depositType2").SourceColumn = theTextbox.Text
macker
05-11-2007, 08:36 AM
I put my procedure into sql. I use sql Management Studio 2005.
Now it is just not picking up the parameters, it says the expected parameter was not supplied.
Protected Sub gridEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridEdit.SelectedIndexChanged
Dim myCommitConnection As Data.SqlClient.SqlConnection
Dim myCommitCommand As Data.SqlClient.SqlDataAdapter
Dim commitNow As New Data.SqlClient.SqlCommand
I changed it to this, and it does the same thing.
It is saying that the procedure is not getting any parameters sent to it.
Any ideas on that? I have moved the proc to sql management studio.
This code is different form the first post from today. They both, however, do the same thing, don't work.. lol.
Thanks
Protected Sub gridEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridEdit.SelectedIndexChanged
' Try
Dim myCommitConnection As Data.SqlClient.SqlConnection
Dim myCommitCommand As Data.SqlClient.SqlDataAdapter
Dim cmd As New Data.SqlClient.SqlCommand
'Catch ex As Exception
' 'lblerror.text = "Error Processing Save: " ex
' Me.lblError.Text = GetExceptionMessage(ex)
' lblError.ForeColor = System.Drawing.Color.Red
'End Try
End Sub
macker
05-11-2007, 09:21 AM
I can not use the text property of the textboxes, because they are in a datagrid, therefore, they all have the same name, and it would not be able to pull the text properly. It would not know which box to get, would it?
Ribeyed
05-11-2007, 01:55 PM
right your getting there but your still missing a few things here.
Dim myCommitConnection As Data.SqlClient.SqlConnection
Dim myCommitCommand As Data.SqlClient.SqlDataAdapter
Dim sqlProcedure As String
sqlProcedure = "StoredProcedureName"
myCommitConnection = New Data.SqlClient.SqlConnection("server=DBWEBWORKSTATIO;" & "database=StifelBank;Trusted_Connection=Yes")
Dim cmd As New Data.SqlClient.SqlCommand(sqlProcedure, myCommitConnection)
cmd.CommandType = System.Data.CommandType.StoredProcedure
cmd.Parameters.Add("@depositType2", SqlDbType.VarChar)
cmd.Parameters("@depositType2").SourceColumn = GridViewItem
cmd.Parameters.Add("@minToOpen2", SqlDbType.VarChar)
' etc......
'then open your connection.....
myCommitConnection.Open()
cmd.ExecuteNonQuery()
myCommitConnection.Close()
Now you have to put all this in a loop. You need to loop through your GridView and run the above code for each row in your GridView.
You need to substiute the parameter variables with the items in your gridview.
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.