Updating a database with vb.net from a gridview
Hello,
This is my first attempt at updating an Oracle database from code (vb.net 2005), (Oracle 9i). In the past i have only updated access databases. I got through the connection and read processes to fill gridviews, and I see that Oracle is really no different then access, only object names have changed.
I'm not sure if this is the case with the updates. Below is a sample of an Access update. Can you post a sample of an oracle update?
Are parameters handled differently? (And below this is my incorrect attempt at the Oracle update)
******a working Access update *****
Protected Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
Dim NewConnection As New OleDbConnection
NewConnection.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
If e.CommandName = "Update" Then
Try
Dim strDepartName = CType(GridView3.Rows(CInt(e.CommandArgument)).FindControl("txtDepartName"), TextBox).Text
Dim txtDepartNum = CType(GridView3.Rows(CInt(e.CommandArgument)).FindControl("lblDepartNum"), Label).Text
NewConnection.Open()
Dim trans As OleDbTransaction = NewConnection.BeginTransaction(IsolationLevel.ReadCommitted)
Dim cmd As New OleDbCommand("Update tblDepart set DepartName=? " & _
"Where DepartNum=?", NewConnection, trans)
cmd.Parameters.Add(New OleDb.OleDbParameter("DepartName", strDepartName))
cmd.Parameters.Add(New OleDb.OleDbParameter("DepartNum", txtDepartNum))
cmd.ExecuteNonQuery()
trans.Commit()
GridView3.EditIndex = -1
NewConnection.Close()
BindDepartments()
Me.lblInfo.Text = "Update Complete"
Catch ex As Exception
Throw ex
End Try
End If
End Sub
***************I know this is wrong on many levels, can you post one that works? *************
If e.CommandName = "Update" Then
Try
Dim NewOracleConnection As New OracleConnection()
NewOracleConnection.ConnectionString = ConfigurationManager.ConnectionStrings("MyOracleConnection").ConnectionString
Dim strGoalAmount = CType(GridView3.Rows(CInt(e.CommandArgument)).FindControl("txtGoalAmount"), TextBox).Text
Dim trans As OracleTransaction = NewOracleConnection.BeginTransaction(IsolationLevel.ReadCommitted)
Dim cmd As New OracleCommand
NewOracleConnection.Open()
cmd.CommandText = "Update zsalespersongoals set GoalAmount=:I_GoalAmount " & _
"Where SalesPerson_Id=:I_Salesperson_ID"
Dim parmGoalAmount = New OracleParameter("I_GoalAmount", OracleDbType.BinaryFloat)
Dim parmSalesPerson = New OracleParameter("I_Salesperson_Id", OracleDbType.Varchar2)
parmSalesPerson = 52
parmGoalAmount = strGoalAmount
cmd.ExecuteNonQuery()
trans.Commit()
GridView3.EditIndex = -1
NewOracleConnection.Close()
Catch ex As Exception
Throw ex
End Try
End If