I am trying to perform an update with the function of RowUpdating
of the same Gridview
by giving it in update
, this throws an error and does not continue the execution, I have tried everything, and I searched for the solution in many forums and just do not want to work ... I'm using a database in Access
This is my code:
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Code display data
string myConnectionString = @"C:\Users\gutiece\Desktop\database\" + "Database1.accdb";
using (OleDbConnection connection = new OleDbConnection())
{
using (OleDbCommand command = new OleDbCommand())
{
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
command.Connection = connection;
connection.Open();
string id = ((TextBox)GridView1.Rows[e.RowIndex].Controls[0]).Text;
string who = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string payrol = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string ou = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string pool = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
string team = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
string rol = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
command.CommandText = "UPDATE users SET who = '" + who + "', payroll_number = '" + payrol + "',name = '" + name + "', ou = '" + ou + "', pool = '" + pool + "', team = '" + team + "', rol_id = '" + rol + "' WHERE id = '" + id + "'";
command.ExecuteNonQuery();
GridView1.EditIndex = -1;
connection.Close();
GridView1.DataBind();
Response.Redirect("WebForm2.aspx");
}
}
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
and that's how I got the GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True"
onrowcancelingedit="GridView2_RowCancelingEdit" onrowediting="GridView2_RowEditing"
onrowupdating="GridView2_RowUpdating" OnPageIndexChanging="GridView2_PageIndexChanging" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Id" Visible="false">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Who">
<EditItemTemplate>
<asp:TextBox ID="Tgv_who" type="String" runat="server" Text='<%# Bind("who") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("who") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payroll Number">
<EditItemTemplate>
<asp:TextBox ID="Tgv_pay" type="Int32" runat="server" Text='<%# Bind("payroll_number") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("payroll_number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="Tgv_name" type="String" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OU">
<EditItemTemplate>
<asp:TextBox ID="Tgv_ou" type="String" runat="server" Text='<%# Bind("ou") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("ou") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pool">
<EditItemTemplate>
<asp:TextBox ID="Tgv_pool" type="Int32" runat="server" Text='<%# Bind("pool") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("pool") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Team">
<EditItemTemplate>
<asp:TextBox ID="Tgv_team" type="Int32" runat="server" Text='<%# Bind("team") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("team") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rol">
<EditItemTemplate>
<asp:TextBox ID="Tgv_rol" type="Int32" runat="server" Text='<%# Bind("rol_id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("rol_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField></asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
and this is the error that pulls on the first line where the id = ((TextBox...))
Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.Web.UI.WebControls.TextBox'.