Updating record from DetailsView with Code-Behind

Tech-Archive recommends: Speed Up your PC by fixing your registry



I want to update my records in my DetailsView control using code-behind,
vb.net, however I keep running into road blocks. I am having a hard time
posting the data back to the db.
My app is pull all recs from the Employees table of the NW DB into a
GridView control. I have a asp:HyperLink control in the last column, Edit,
that passes the Key value of the row to the DetailsView on my Details.aspx
page. I then want the user to be able to make what ever changes they want,
and save the record back to the db.
It is the posting back to the db that I cannot get correct. Any help is
appreciated. Below is the code that I have for the ItemUpdating method:

Protected Sub dvDetails_ItemUpdating(ByVal sender As Object, ByVal e As
DetailsViewUpdateEventArgs) Handles dvDetails.ItemUpdating
Dim connStr As String =
ConfigurationManager.ConnectionStrings("NWConnectionString").ToString()
Dim conn As New SqlConnection(connStr)
'Dim strSql As String = "SELECT * FROM Employees WHERE
EmployeeID=@EmployeeID"
Dim upSql As String = "UPDATE Employees SET LastName=@LastName,
FirstName=@FirstName, Title=@Title, Address=@Address," & _
"City=@City, Region=@Region, PostalCode=@PostalCode,
HireDate=@HireDate WHERE EmployeeID=@EmployeeID"
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim ds1 As New DataSet
Dim upCmd As New SqlCommand(upSql, conn)
Dim idParam As SqlParameter = upCmd.Parameters.Add("@EmployeeID",
SqlDbType.Int, 4, "EmployeeID")


Try
' da.SelectCommand = New SqlCommand(strSql, conn)
'da.Fill(ds, "Employees")

upCmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20,
"LastName")
upCmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10,
"FirstName")
upCmd.Parameters.Add("@Title", SqlDbType.VarChar, 30, "Title")
upCmd.Parameters.Add("@Address", SqlDbType.VarChar, 60, "Address")
upCmd.Parameters.Add("@City", SqlDbType.VarChar, 15, "City")
upCmd.Parameters.Add("@Region", SqlDbType.VarChar, 15, "Region")
upCmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10,
"PostalCode")
upCmd.Parameters.Add("@HireDate", SqlDbType.DateTime, 8,
"HireDate")

idParam.SourceVersion = DataRowVersion.Original

conn.Open()
'Update Employees Table

ds1 = ds.GetChanges(DataRowState.Modified)
da.Update(ds1)
ds.Merge(ds1, False, MissingSchemaAction.Add)
ds.AcceptChanges()
dvDetails.DataBind()

Catch ex As Exception
'Display Error
Console.WriteLine("Error: " & ex.ToString())

Finally
'Close Connection
conn.Close()
'Redirect to Home
Response.Redirect("~/Default.aspx")

End Try
End Sub
.



Relevant Pages

  • Re: IE sequencing and cookie notification popups
    ... ' 10Apr04: accept cookies from hotmail. ... Dim SetWindowLong ' as object ... Const sDlgCaption = " Kookie Kruncher, ... ' notifications associated with that type of control. ...
    (microsoft.public.scripting.vbscript)
  • FYI: TS client script
    ... for TS on the machine running the script, ... the web control class was not found; ... Dim m_Tracing, m_TraceStream ... Function ArgString(ByVal argName, ByVal defaultValue) ...
    (microsoft.public.win2000.termserv.clients)
  • FYI: TS client script
    ... for TS on the machine running the script, ... the web control class was not found; ... Dim m_Tracing, m_TraceStream ... Function ArgString(ByVal argName, ByVal defaultValue) ...
    (microsoft.public.windows.terminal_services)
  • Re: Transfer data from one Content Control to another Word 2007 VB
    ... unfortunately cleared the grey placeholder text and turned it black and I ... Dim cc1PtMedication As ContentControl ... you can just assign the text of one control directly to the text ... macro transfers the contents of room 1 to the controls for room 3, ...
    (microsoft.public.word.vba.beginners)
  • Re: Transfer data from one Content Control to another Word 2007 VB
    ... Dim cc1PtMedication As ContentControl ... incorporates a dropdown list. ... you can just assign the text of one control directly to the text ... macro transfers the contents of room 1 to the controls for room 3, ...
    (microsoft.public.word.vba.beginners)