Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ASP / Database Access / July 2007



Tip: Looking for answers? Try searching our database.

Updating record from DetailsView with Code-Behind

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stupid1 - 26 Jul 2007 12:30 GMT
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
Bob Barrows [MVP] - 26 Jul 2007 16:45 GMT
> I want to update my records in my DetailsView control using

There was no way for you to know it (except maybe by browsing through some
of the previous questions in this newsgroup before posting yours - always a
recommended practice) , but this is a classic asp newsgroup. ASP.Net bears
very little resemblance to classic ASP so, while you may be lucky enough to
find a dotnet-knowledgeable person here who can answer your question, you
can eliminate the luck factor by posting your question to a group where
those dotnet-knowledgeable people hang out. I suggest
microsoft.public.dotnet.framework.aspnet.

Signature

Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.