Can someone point out what I did wrong? I'm using a search form to 
bring up the record to be edited. Yet once I edit the record, the 
next time I go search for it, it doesn't display it and shows it as a 
nonexisting record in the datagrid. In addition, once I edit the 
record, I should see the updated changes right in the datagrid, but I 
don't. The record disappears. Here's my code:

==================================================================
<%@ Page Language="VB" %>
<script runat="server">

    Sub dgCoordinator_Edit(sender as Object, e as 
DataGridCommandEventArgs)
            dgCoordinator.EditItemIndex = e.Item.ItemIndex
    
            dgCoordinator.DataSource = GetCoordinator(lastname.Text, 
firstname.Text)
            dgCoordinator.DataBind()
        End Sub
    
    
        Sub dgCoordinator_Update(sender as Object, e as 
DataGridCommandEventArgs)
            'update database here
            'determind the value of the customer id column
            Dim userID as Integer = e.Item.Cells(1).Text
    
            'reference each textbox
            Dim lname as TextBox = e.Item.Cells(2).Controls(0)
            Dim fname as TextBox = e.Item.Cells(3).Controls(0)
            Dim email as TextBox = e.Item.Cells(4).Controls(0)
            Dim phone as TextBox = e.Item.Cells(5).Controls(0)
            Dim regcode as TextBox = e.Item.Cells(6).Controls(0)
            Dim uname as TextBox = e.Item.Cells(7).Controls(0)
            Dim pword as TextBox = e.Item.Cells(8).Controls(0)
            Dim systemsecuid as TextBox = e.Item.Cells(9).Controls(0)
            Dim regsecuid as TextBox = e.Item.Cells(10).Controls(0)
    
            UpdateCoordinator(userID, lname.Text, fname.Text, 
email.Text, phone.Text, regcode.Text, uname.Text, pword.Text, 
systemsecuid.Text, regsecuid.Text)
    
    
            dgCoordinator.EditItemIndex = -1
    
            dgCoordinator.DataSource = GetCoordinator(lastname.Text, 
firstname.Text)
            dgCoordinator.DataBind()
        End Sub
    
        Sub dgCoordinator_Cancel(sender as Object, e as 
DataGridCommandEventArgs)
            dgCoordinator.EditItemIndex = -1
    
            dgCoordinator.DataSource = GetCoordinator(lastname.text, 
firstname.Text)
            dgCoordinator.DataBind()
        End Sub
    
    
    
    
        Function UpdateCoordinator(ByVal userUID As Integer, ByVal 
username As String, ByVal password As String, ByVal lastName As 
String, ByVal firstName As String, ByVal email As String, ByVal phone 
As String, ByVal [region] As String, ByVal systemSecUID As Byte, 
ByVal regSecUID As Byte) As Integer
            Dim connectionString As String = "server='localhost'; 
user id='sa'; password='pooh2'; Database='OTSODITMCallingCard"& _
    "db'"
            Dim sqlConnection As System.Data.SqlClient.SqlConnection 
= New System.Data.SqlClient.SqlConnection(connectionString)
    
            Dim queryString As String = "UPDATE [Coordinator] SET 
[EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED], "& _
    "[EMAIL PROTECTED], [EMAIL PROTECTED], [LastName]
[EMAIL PROTECTED], [EMAIL PROTECTED]"& _
    "ne, [EMAIL PROTECTED], [EMAIL PROTECTED] WHERE 
([Coordinator].[U"& _
    "serUID] = @UserUID)"
            Dim sqlCommand As System.Data.SqlClient.SqlCommand = New 
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
            sqlCommand.Parameters.Add("@UserUID", 
System.Data.SqlDbType.Int).Value = userUID
            sqlCommand.Parameters.Add("@Username", 
System.Data.SqlDbType.VarChar).Value = username
            sqlCommand.Parameters.Add("@Password", 
System.Data.SqlDbType.VarChar).Value = password
            sqlCommand.Parameters.Add("@LastName", 
System.Data.SqlDbType.Char).Value = lastName
            sqlCommand.Parameters.Add("@FirstName", 
System.Data.SqlDbType.Char).Value = firstName
            sqlCommand.Parameters.Add("@Email", 
System.Data.SqlDbType.VarChar).Value = email
            sqlCommand.Parameters.Add("@Phone", 
System.Data.SqlDbType.Char).Value = phone
            sqlCommand.Parameters.Add("@Region", 
System.Data.SqlDbType.Char).Value = [region]
            sqlCommand.Parameters.Add("@SystemSecUID", 
System.Data.SqlDbType.TinyInt).Value = systemSecUID
            sqlCommand.Parameters.Add("@RegSecUID", 
System.Data.SqlDbType.TinyInt).Value = regSecUID
    
            Dim rowsAffected As Integer = 0
            sqlConnection.Open
            Try
                rowsAffected = sqlCommand.ExecuteNonQuery
            Finally
                sqlConnection.Close
            End Try
    
            Return rowsAffected
        End Function
    
    
        Function GetCoordinator(ByVal lastName As String, ByVal 
firstName As String) As System.Data.SqlClient.SqlDataReader
            Dim connectionString As String = "server='localhost'; 
user id='sa'; password='pooh2'; Database='OTSODITMCallingCard"& _
    "db'"
            Dim sqlConnection As System.Data.SqlClient.SqlConnection 
= New System.Data.SqlClient.SqlConnection(connectionString)
    
            Dim queryString As String = "SELECT [Coordinator].* FROM 
[Coordinator] WHERE (([Coordinator].[LastName] = @Las"& _
    "tName) AND ([Coordinator].[FirstName] = @FirstName))"
            Dim sqlCommand As System.Data.SqlClient.SqlCommand = New 
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
            sqlCommand.Parameters.Add("@LastName", 
System.Data.SqlDbType.Char).Value = lastName
            sqlCommand.Parameters.Add("@FirstName", 
System.Data.SqlDbType.Char).Value = firstName
    
            sqlConnection.Open
            Dim dataReader As System.Data.SqlClient.SqlDataReader = 
sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
            Return dataReader
        End Function
    
    
    
        'Sub Page_Load(sender as Object, e as EventArgs)
    
            'If Not Page.IsPostBack then
                'dgCustomers.DataSource = GetCustomers()
                'dgCustomers.DataBind()
            'End If
        'End Sub
    
        Sub Button1_Click(sender As Object, e As EventArgs)
    
            dgCoordinator.DataSource = GetCoordinator(lastname.Text, 
firstname.Text)
            dgCoordinator.DataBind()
    
        End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            EDIT COORDINATOR 
        </p>
        <p>
            Last Name: 
            <asp:TextBox id="lastname" runat="server"></asp:TextBox>
            &nbsp;&nbsp;&nbsp;&nbsp; First Name: 
            <asp:TextBox id="firstname" runat="server"></asp:TextBox>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button 
id="Button1" onclick="Button1_Click" runat="server" 
Text="Search"></asp:Button>
        </p>
        <p>
        </p>
        <p>
            <asp:DataGrid id="dgCoordinator" runat="server" 
AutoGenerateColumns="False" OnCancelCommand="dgCoordinator_Cancel" 
OnUpdateCommand="dgCoordinator_Update" 
OnEditCommand="dgCoordinator_Edit">
                <HeaderStyle horizontalalign="Center"></HeaderStyle>
                <Columns>
                    <asp:EditCommandColumn ButtonType="LinkButton" 
UpdateText="Update" CancelText="Cancel" 
EditText="Edit"></asp:EditCommandColumn>
                    <asp:BoundColumn Visible="False" 
DataField="UserUID" ReadOnly="True" 
HeaderText="UserUID"></asp:BoundColumn>
                    <asp:BoundColumn DataField="LastName" 
HeaderText="Last Name"></asp:BoundColumn>
                    <asp:BoundColumn DataField="FirstName" 
HeaderText="First Name"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Email" 
HeaderText="Email"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Phone" 
HeaderText="Phone"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Region" 
HeaderText="Region"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Username" 
HeaderText="Username"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Password" 
HeaderText="Password"></asp:BoundColumn>
                    <asp:BoundColumn DataField="SystemSecUID" 
HeaderText="SystemSecUID"></asp:BoundColumn>
                    <asp:BoundColumn DataField="RegSecUID" 
HeaderText="RegSecUID"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>
            <!-- Insert content here -->
        </p>
    </form>
</body>
</html>
===================================================================

This is the exact code from a book, which I altered, but it works:

===================================================================
<%@ Page Language="VB" %>
<script runat="server">

    Sub dgCustomers_Edit(sender as Object, e as 
DataGridCommandEventArgs)
            dgCustomers.EditItemIndex = e.Item.ItemIndex
    
            dgCustomers.DataSource = GetCustomer2(name.text)
            dgCustomers.DataBind()
        End Sub
    
    
        Sub dgCustomers_Update(sender as Object, e as 
DataGridCommandEventArgs)
            'update database here
            'determind the value of the customer id column
            Dim customerID as Integer = e.Item.Cells(1).Text
    
            'reference each textbox
            Dim nameTextBox as TextBox = e.Item.Cells(2).Controls(0)
            Dim phoneTextBox as TextBox = e.Item.Cells(3).Controls(0)
            Dim zipTextBox as TextBox = e.Item.Cells(4).Controls(0)
    
            UpdateCustomer(customerID, nameTextBox.Text, 
phoneTextBox.Text, zipTextBox.Text)
    
    
            dgCustomers.EditItemIndex = -1
    
            dgCustomers.DataSource = GetCustomer2(name.Text)
            dgCustomers.DataBind()
        End Sub
    
        Sub dgCustomers_Cancel(sender as Object, e as 
DataGridCommandEventArgs)
            dgCustomers.EditItemIndex = -1
    
            dgCustomers.DataSource = GetCustomer2(name.text)
            dgCustomers.DataBind()
        End Sub
    
    
    
        Function UpdateCustomer(ByVal customerID As Integer, ByVal 
name As String, ByVal phone As String, ByVal zipCode As String) As 
Integer
            Dim connectionString As String = "server='localhost'; 
user id='sa'; password='pooh2'; Database='OTSODITMCallingCard"& _
    "db'"
            Dim sqlConnection As System.Data.SqlClient.SqlConnection 
= New System.Data.SqlClient.SqlConnection(connectionString)
    
            Dim queryString As String = "UPDATE [Customers] SET [Name]
[EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED] WHERE ([C"& _
    "ustomers].[CustomerID] = @CustomerID)"
            Dim sqlCommand As System.Data.SqlClient.SqlCommand = New 
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
            sqlCommand.Parameters.Add("@CustomerID", 
System.Data.SqlDbType.Int).Value = customerID
            sqlCommand.Parameters.Add("@Name", 
System.Data.SqlDbType.VarChar).Value = name
            sqlCommand.Parameters.Add("@Phone", 
System.Data.SqlDbType.VarChar).Value = phone
            sqlCommand.Parameters.Add("@ZipCode", 
System.Data.SqlDbType.VarChar).Value = zipCode
    
            Dim rowsAffected As Integer = 0
            sqlConnection.Open
            Try
                rowsAffected = sqlCommand.ExecuteNonQuery
            Finally
                sqlConnection.Close
            End Try
    
            Return rowsAffected
        End Function
    
    
        Function GetCustomer2(ByVal name As String) As 
System.Data.SqlClient.SqlDataReader
            Dim connectionString As String = "server='localhost'; 
user id='sa'; password='pooh2'; Database='OTSODITMCallingCard"& _
    "db'"
            Dim sqlConnection As System.Data.SqlClient.SqlConnection 
= New System.Data.SqlClient.SqlConnection(connectionString)
    
            Dim queryString As String = "SELECT [Customers].* FROM 
[Customers] WHERE ([Customers].[Name] = @Name)"
            Dim sqlCommand As System.Data.SqlClient.SqlCommand = New 
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
            sqlCommand.Parameters.Add("@Name", 
System.Data.SqlDbType.VarChar).Value = name
    
            sqlConnection.Open
            Dim dataReader As System.Data.SqlClient.SqlDataReader = 
sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
            Return dataReader
        End Function
    
    
        'Sub Page_Load(sender as Object, e as EventArgs)
    
            'If Not Page.IsPostBack then
                'dgCustomers.DataSource = GetCustomers()
                'dgCustomers.DataBind()
            'End If
        'End Sub
    
        Sub Button1_Click(sender As Object, e As EventArgs)
            dgCustomers.DataSource = GetCustomer2(name.Text)
            dgCustomers.DataBind()
    End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            name: 
            <asp:TextBox id="name" runat="server"></asp:TextBox>
            &nbsp;<asp:Button id="Button1" onclick="Button1_Click" 
runat="server" Text="search"></asp:Button>
        </p>
        <p>
        </p>
        <p>
            <asp:DataGrid id="dgCustomers" runat="server" 
AutoGenerateColumns="False" OnCancelCommand="dgCustomers_Cancel" 
OnUpdateCommand="dgCustomers_Update" OnEditCommand="dgCustomers_Edit">
                <Columns>
                    <asp:EditCommandColumn ButtonType="LinkButton" 
UpdateText="Update" CancelText="Cancel" 
EditText="Edit"></asp:EditCommandColumn>
                    <asp:BoundColumn DataField="CustomerID" 
ReadOnly="True" HeaderText="CustomerID"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Name" 
HeaderText="Name"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Phone" 
HeaderText="Phone"></asp:BoundColumn>
                    <asp:BoundColumn DataField="ZipCode" 
HeaderText="ZipCode"></asp:BoundColumn>
                    <asp:BoundColumn DataField="DataBecameCustomer" 
ReadOnly="True" HeaderText="DateBecameCustomer" 
DataFormatString="{0:d}"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>
            <!-- Insert content here -->
        </p>
    </form>
</body>
</html>
===================================================================



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

Reply via email to