Click here to Skip to main content
15,887,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
All of my gridview features work (insert, delete) except for updating a customer. I can't figure out why my update statement isn't working. When I set a breakpoint on the method, and click the "update" button, nothing happens in the browser, and it doesn't hit my breakpoint either. Does anyone see anything that could be an issue in my code? I'll include my ASP.NET gridview code, and the methods that are used with the gridview.

ASP.NET
<ContentTemplate>
                <asp:GridView ID="GridView1" DataKeyNames="PersonID" runat="server" AutoGenerateColumns="False"
                    Font-Names="Arial" Font-Size="10pt" ShowFooter="true" OnRowEditing="EditCustomer"
                    OnRowUpdating="UpdateCustomer" OnRowCancelingEdit="CancelEdit" Height="95px">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <%# Container.DataItemIndex + 1 %>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="First Name">
                            <ItemTemplate>
                                <asp:Label ID="lblFirstName" runat="server" Height="20px" Text='<%# Eval("FirstName")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtFirstName" Width="60px" MaxLength="15" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Last Name">
                            <ItemTemplate>
                                <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtLastName" Width="60px" MaxLength="15" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Age">
                            <ItemTemplate>
                                <asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("Age")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtAge" Width="30px" MaxLength="2" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Activity Card">
                            <ItemTemplate>
                                <asp:Label ID="lblActivityCard" runat="server" wrap="true" Text='<%# Eval("ActivityCard")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtActivityCard" Width="50px" MaxLength="7" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cell Phone">
                            <ItemTemplate>
                                <asp:Label ID="lblCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtCell" Width="70px" MaxLength="10" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Emergency Phone">
                            <ItemTemplate>
                                <asp:Label ID="lblEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtEmergency" Width="70px" MaxLength="10" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Boat Type">
                            <ItemTemplate>
                                <asp:Label ID="lblBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtBoat" Width="80px" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Initials">
                            <ItemTemplate>
                                <asp:Label ID="lblInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtInitials" Width="30px" MaxLength="2" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Group #">
                            <ItemTemplate>
                                <asp:Label ID="lblGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtGroup" MaxLength="2" Width="20px" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("PersonID")%>'
                                    OnClientClick="return confirm('Are you sure you want to delete?')" Text="Delete"
                                    OnClick="DeleteCustomer"></asp:LinkButton>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Button ID="btnAdd" runat="server" Text="Submit" OnClick="AddNewCustomer" />
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                    </Columns>
                    <EmptyDataTemplate>
                        <span style="font-size: 13px; font-weight: bold; color: Blue;">No records are available
                            matching your selected filter. Please fill out the form below to add the first customer
                            .. </span>
                    </EmptyDataTemplate>
                </asp:GridView>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" />
            </Triggers>
        </asp:UpdatePanel>


C#:

C#
private void BindData()
    {
        string strQuery = "SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
                             "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
                             "FROM Person INNER JOIN " +
                             "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Datee and " +
                             "TripSchedule.Time = @Timee and TripSchedule.TripType = @Typee order by Person.Group#;";

        SqlCommand cmd = new SqlCommand(strQuery);

        cmd.Parameters.AddWithValue("@Datee", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Timee", ddlTripTime.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Typee", ddlTripType.SelectedItem.ToString());
        string trip = ddlTripType.SelectedItem.ToString();

        if (trip == "CANOE / KAYAK")
        {
            GridView1.HeaderStyle.BackColor = System.Drawing.Color.FromName("Yellow");
            GridView1.RowStyle.BackColor = System.Drawing.Color.FromArgb(255, 255, 0);
            GridView1.AlternatingRowStyle.BackColor = System.Drawing.Color.FromArgb(255, 250, 205);
        }
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(strConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;
        adapter.SelectCommand = cmd;
        adapter.Fill(dt);
        return dt;
    }
  protected void EditCustomer(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
    {
        string nFirstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
        string nLastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
        string nEmergency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmergency")).Text;
        string nCell = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCell")).Text;
        string nAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge")).Text;
        string nActivityCard = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtActivityCard")).Text;
        string nBoat = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBoat")).Text;
        string nInitials = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtInitials")).Text;
        string nGroup = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGroup")).Text;
        long personID = long.Parse(GridView1.DataKeys[e.RowIndex].Values["PersonID"].ToString());
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Person set FirstName=@FirstName, LastName=@LastName, " +
        "Emergency#=@Emergency, Cell#=@Cell, Age=@Age, ActivityCard=@ActivityCard, Initials=@Initials, CraftType=@Boat, Group#=@Group " +
        "where PersonID=@Pid;" +
        "SELECT Person.PersonID AS PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
        "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
        "FROM Person INNER JOIN " +
        "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Date and " +
        "TripSchedule.Time = @Time and TripSchedule.TripType = @Type order by Person.Group#;";
        cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = nFirstName;
        cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = nLastName;
        cmd.Parameters.Add("@Emergency", SqlDbType.NChar).Value = nEmergency;
        cmd.Parameters.Add("@Cell", SqlDbType.NChar).Value = nCell;
        cmd.Parameters.Add("@Age", SqlDbType.NChar).Value = nAge;
        cmd.Parameters.Add("@ActivityCard", SqlDbType.NChar).Value = nActivityCard;
        cmd.Parameters.Add("@Initials", SqlDbType.NChar).Value = nInitials;
        cmd.Parameters.Add("@Boat", SqlDbType.VarChar).Value = nBoat;
        cmd.Parameters.Add("@Group", SqlDbType.VarChar).Value = nGroup;
        cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Pid", personID);
        GridView1.EditIndex = -1;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();

    }
Posted

1 solution

Try to add Trigger for the UpdatePanel like...
XML
</ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="GridView1" EventName="RowUpdating" />
    </Triggers>
</asp:UpdatePanel>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900