Edit Commands do not show up when edit button pressed

I have a .aspx page I have created, and I am having problems.  I have an editable datagrid created and also a filtering drop down menu as well.  The datagrid shows up fine first, shows all the columns and the entries from the database, and has the edit button next to the entry.  When I press the edit button, the datagrid disappears and there is nothing on the page.  The page is completely blank.  If I reload the page, the data comes back, and when I hit the edit button again, it disappears again.  Any help anyone can give me would be appreciated.  Here is my code for the program.  Also, the datagrid edit tag has been setup with the OnEditCommand as well.

Sub Page_Load(Sender As Object, E As EventArgs)

    If Not Page.IsPostBack Then

        Dim ConnectionString As String = "server='labweb\labweb';database=ASPExamples;trusted_connection=true"
        Dim CommandText As String = "select distinct Name from Customers"

        Dim myConnection As New SqlConnection(ConnectionString)
        Dim myCommand As New SqlCommand(CommandText, myConnection)

        DropDownList1.DataTextField = "Name"

        myConnection.Open()

        DropDownList1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        DropDownList1.DataBind()

        DropDownList1.Items.Insert(0, "-- All People --")

        dgCustomers.DataSource = GetCustomers()
        dgCustomers.DataBind()

    End If

End Sub

Function GetCustomers() As System.Data.DataSet
        Dim connectionString As String = "server='labweb\labweb'; trusted_connection=true; database='ASPExamples'"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "SELECT [Customers].* FROM [Customers]"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
End Function

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='labweb\labweb'; trusted_connection=true; database='ASPExamples'"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "UPDATE [Customers] SET [Name]=@Name, [Phone]=@Phone, [ZipCode]=@ZipCode WHERE ([C"& _
"ustomers].[CustomerID] = @CustomerID)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_customerID As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_customerID.ParameterName = "@CustomerID"
        dbParam_customerID.Value = customerID
        dbParam_customerID.DbType = System.Data.DbType.Int32
        dbCommand.Parameters.Add(dbParam_customerID)
        Dim dbParam_name As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_name.ParameterName = "@Name"
        dbParam_name.Value = name
        dbParam_name.DbType = System.Data.DbType.String
        dbCommand.Parameters.Add(dbParam_name)
        Dim dbParam_phone As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_phone.ParameterName = "@Phone"
        dbParam_phone.Value = phone
        dbParam_phone.DbType = System.Data.DbType.String
        dbCommand.Parameters.Add(dbParam_phone)
        Dim dbParam_zipCode As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_zipCode.ParameterName = "@ZipCode"
        dbParam_zipCode.Value = zipCode
        dbParam_zipCode.DbType = System.Data.DbType.String
        dbCommand.Parameters.Add(dbParam_zipCode)

        Dim rowsAffected As Integer = 0
        dbConnection.Open
        Try
            rowsAffected = dbCommand.ExecuteNonQuery
        Finally
            dbConnection.Close
        End Try

        Return rowsAffected
End Function

Sub ApplyFilter_Click(Sender As Object, E As EventArgs)

    Dim ConnectionString As String = "server='labweb\labweb';database=ASPExamples;trusted_connection=true"
    Dim CommandText As String

    Dim filterValue As String = DropDownList1.SelectedItem.Text.Replace("'", "''")

    If filterValue = "-- All People --" Then
        CommandText = "select CustomerID as CustomerID, Name as Name, Phone as Phone, ZipCode as ZipCode, DateBecameCustomer as DateBecameCustomer from Customers"
    Else
        CommandText = "select CustomerID as CustomerID, Name as Name, Phone as Phone, ZipCode as ZipCode, DateBecameCustomer as DateBecameCustomer from Customers where Name = '" & filterValue & "'"
    End If

    Dim myConnection As New SqlConnection(ConnectionString)
    Dim myCommand As New SqlCommand(CommandText, myConnection)

    myConnection.Open()

    dgCustomers.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgCustomers.DataBind()

End Sub

Sub dgCustomers_Edit(sender as Object, e as DataGridCommandEventArgs)
    dgCustomers.EditItemIndex = e.Item.ItemIndex
    dgCustomers.DataSource = GetCustomers()
    dgCustomers.DataBind()
End Sub

Sub dgCustomers_Update(sender as Object, e as DataGridCommandEventArgs)
    Dim customerID as Integer = e.Item.Cells(1).Text
    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 = GetCustomers()
    dgCustomers.DataBind()
End Sub

Sub dgCustomers_Cancel(sender as Object, e as DataGridCommandEventArgs)
    dgCustomers.EditItemIndex = -1
    dgCustomers.DataSource = GetCustomers()
    dgCustomers.DataBind()
End Sub

0
jberthia
4/24/2006 5:02:42 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
708 Views

Similar Articles

[PageSpeed] 25
Get it on Google Play
Get it on Apple App Store

is viewstate enabled on the grid?
0
wysiwyg
4/25/2006 2:09:30 AM
Yeah what he said ^^^... could also be that viewstate is disabled at the page level. Anything that says EnableViewState="false" would be a prime suspect.


End the confusion.
Infinities Loop: TRULY Understanding ViewState
.NET from a new perspective.

This posting is provided "AS IS".
0
InfinitiesLoop
4/25/2006 4:03:17 AM
Reply: