data binding gone bad...

ckeezer

Well-known member
Joined
Jan 16, 2006
Messages
100
Programming Experience
1-3
Can someone please let me know why the code below is not updating to the db? I will post my asp code first then the code-behind in a follow-on post...

HTML:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="details.aspx.vb" Inherits="details" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server">
<title>Update an Employee</title>
</head> 
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="dvDetails" runat="server" AutoGenerateRows="False" Height="50px"
Width="125px" DefaultMode="Edit">
<Fields>
<asp:BoundField DataField="EmployeeID" Visible="false" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
<asp:BoundField DataField="HireDate" HeaderText="HireDate" SortExpression="HireDate" />
</Fields>
</asp:DetailsView>
</div>
<asp:Button ID="update" runat="server" Text="Update" />
<asp:Button ID="cancel" runat="server" Text="Cancel" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
</form> </body> 
</html>
 
Last edited by a moderator:
follon on

VB.NET:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Partial Class detailsInherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim connStr As String = ConfigurationManager.ConnectionStrings("NWConnectionString").ToString()
            ' Create DB Connection
            Dim conn As New SqlConnection(connStr)
            'Create DS
            Dim ds As New DataSet
            'Read EmployeeID from Default.aspx
            Dim sEmployeeId As String = Request.QueryString("EmployeeID")
            'Create DA
            Dim da As New SqlDataAdapter("SELECT * FROM Employees WHERE [EmployeeID] = " & sEmployeeId, conn)
            'Open Conn to DB
            conn.Open()
            'Fill DA with tbl
            da.Fill(ds, "Employees")
            'Set DataSource to DataSet
            dvDetails.DataSource = ds
            'Bind DV to ds
            dvDetails.DataBind()
            'Close DB Connection
            conn.Close()
        End If
    End Sub


    Protected Sub cancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cancel.Click
        Response.Redirect("~/Default.aspx")
    End Sub


    Protected Sub update_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles update.Click
        Dim connStr As String = ConfigurationManager.ConnectionStrings("NWConnectionString").ToString()
        ' Create DB Connection
        Dim conn As New SqlConnection(connStr)
        'Create Select Statement
        Dim strSql As String = "Select * From Employees"
        'Create Sql Update Statement
        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"


        Try
            'Create DataAdapter
            Dim da As New SqlDataAdapter
            da.SelectCommand = New SqlCommand(strSql, conn)
            'Create and Fill DataAdapter
            Dim ds As New DataSet
            da.Fill(ds, "Employees")
            'Get the DataTable
            Dim dt As DataTable = ds.Tables("Employees")
            'Modify the Rows
            'dt.Rows(0)("LastName") = "@LastName"
            'dt.Rows(0)("FirstName") = "@FirstName"
            'dt.Rows(0)("Title") = "@Title"
            'dt.Rows(0)("Address") = "@Address"
            'dt.Rows(0)("City") = "@City"
            'dt.Rows(0)("Region") = "@Region"
            'dt.Rows(0)("PostalCode") = "@PostalCode"
            'dt.Rows(0)("HireDate") = "@HireDate"
            'Update Employees Table
            'Create Command
            Dim upCmd As New SqlCommand(upSql, conn)
            'Map Parameters
            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")
            'Map EmployeeID Parameter
            Dim idParam As SqlParameter = upCmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 4, "EmployeeID")
            idParam.SourceVersion = DataRowVersion.Original
            'Open the connection
            conn.Open()
            'Update Employees Table
            da.UpdateCommand = upCmd
            ds.AcceptChanges()
            da.Update(ds, "Employees")


        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
End Class
 
Last edited by a moderator:
Please use CODE tags, and please post ASP.NET questions in the ASP.NET section (This is VB.NET)

Edit your post to include code tags. If the indentation is lost, paste the code again.Dont just make a post that looks like this:

VB.NET:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Partial Class detailsInherits System.Web.UI.Page 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
If Not IsPostBack Then
Dim connStr As String = ConfigurationManager.ConnectionStrings("NWConnecti onString").ToString() 
' Create DB Connection
Dim conn As New SqlConnection(connStr) 
'Create DS
Dim ds As New DataSet 
'Read EmployeeID from


Contact a moderator (Neal, JohnH, JuggaloBrotha) and ask them to move your post to ASP.NET
 
Back
Top