Anteriormente hice una pregunta donde no podia generar un update
, se solucionaron ciertos problemas pero no llegue a mi objetivo, en esta pregunta quiero saber cómo puedo hacer para que los TextBox
reemplacen los valores que ya se traen desde la base de datos que tengo con Access.
Un ejemplo es que la base de datos trae who = pedro
, payroll = 123456
, y así sucesivamente pero al momento de "cambiar" los valores y dar clic en update
estos se quedan igual.
Este es el codigo del Page_Load
:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Code display data
conexiones.usuario_conexion connect = new conexiones.usuario_conexion();
conexiones.usuario_conexion USBTable = new conexiones.usuario_conexion();
DataTable dvt = USBTable.connect();
if (dvt.Rows.Count > 0)
{
GridView1.DataSource = dvt;
GridView1.DataBind();
}
else
{
GridView1.DataBind();
}
}
}
y este es el código del GridView
:
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();
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int id = Convert.ToInt32(((TextBox)row.FindControl("txtid")).Text);
string who = ((TextBox)row.FindControl("txtwho2")).Text;
int payrol = Convert.ToInt32(((TextBox)row.FindControl("txtpay2")).Text);
string name = ((TextBox)row.FindControl("txtname2")).Text;
string ou = ((TextBox)row.FindControl("txtou2")).Text;
int pool = Convert.ToInt32(((TextBox)row.FindControl("txtpool2")).Text);
int team = Convert.ToInt32(((TextBox)row.FindControl("txtteam2")).Text);
int rol = Convert.ToInt32(((TextBox)row.FindControl("txtrol2")).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.Parameters.Add("@who", OleDbType.VarChar).Value = who;
command.Parameters.Add("@payrol", OleDbType.Integer).Value = payrol;
command.Parameters.Add("@name", OleDbType.VarChar).Value = name;
command.Parameters.Add("@ou", OleDbType.VarChar).Value = ou;
command.Parameters.Add("@pool", OleDbType.Integer).Value = pool;
command.Parameters.Add("@team", OleDbType.Integer).Value = team;
command.Parameters.Add("@rol", OleDbType.Integer).Value = rol;
command.Parameters.Add("@id", OleDbType.Integer).Value = id;
command.ExecuteNonQuery();
GridView1.EditIndex = -1;
connection.Close();
GridView1.DataBind();
Response.Redirect("WebForm2.aspx");
}
}
}
Y este es mi 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" DataKeyNames="id" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Id" Visible="false">
<ItemTemplate>
<asp:TextBox ID="txtid" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Who">
<EditItemTemplate>
<asp:TextBox ID="txtwho2" type="String" runat="server" Text='<%# Bind("who") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtwho" runat="server" Text='<%# Bind("who") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payroll Number">
<EditItemTemplate>
<asp:TextBox ID="txtpay2" type="Int32" runat="server" Text='<%# Bind("payroll_number") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtpay" runat="server" Text='<%# Bind("payroll_number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtname2" type="String" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtname" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OU">
<EditItemTemplate>
<asp:TextBox ID="txtou2" type="String" runat="server" Text='<%# Bind("ou") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtou" runat="server" Text='<%# Bind("ou") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pool">
<EditItemTemplate>
<asp:TextBox ID="txtpool2" type="Int32" runat="server" Text='<%# Bind("pool") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtpool" runat="server" Text='<%# Bind("pool") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Team">
<EditItemTemplate>
<asp:TextBox ID="txtteam2" type="Int32" runat="server" Text='<%# Bind("team") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtteam" runat="server" Text='<%# Bind("team") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rol">
<EditItemTemplate>
<asp:TextBox ID="txtrol2" type="Int32" runat="server" Text='<%# Bind("rol_id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtrol" 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>