Tengo un problema con el evento RowUpdating de un GridView de ASP.Net. Cuando quiero saber el valor de una celda y por alguna razon, no me los trae. Me dan nulos.
Les dejo el codigo para que puedan ver.
Categories.aspx
<%@ Page Title="Category Administration" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Categories.aspx.cs" Inherits="Example.Client.GUI.forum.Administration.Categories" Debug="true" Trace="true" TraceMode="SortByTime" %>
<asp:Content ID="cntHead" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="cntBody" ContentPlaceHolderID="MainContent" runat="server">
<%-- hidden --%>
<asp:HiddenField ID="hidAlgo" runat="server" />
<%-- error --%>
<asp:Panel ID="pnlError" CssClass="alert alert-danger" role="alert" runat="server" Visible="false">
<asp:Literal ID="litError" runat="server" />
</asp:Panel>
<%-- grid --%>
<asp:Panel CssClass="panel panel-default" runat="server">
<asp:Panel CssClass="panel-heading" runat="server">
<asp:Literal Text="Categories" runat="server" />
</asp:Panel>
<asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="False" CssClass="table table-hover" EnableTheming="False" GridLines="None" OnRowCancelingEdit="gvCategories_RowCancelingEdit" OnRowEditing="gvCategories_RowEditing" OnRowUpdating="gvCategories_RowUpdating" OnRowDeleting="gvCategories_RowDeleting" AllowPaging="True" AllowSorting="True" DataKeyNames="category_id">
<Columns>
<asp:BoundField DataField="category_id" HeaderText="Identifier" ReadOnly="True" />
<asp:BoundField DataField="category_name" HeaderText="Name" />
<asp:BoundField DataField="date_added" HeaderText="Date added" ReadOnly="True" />
<asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</asp:Panel>
</asp:Content>
<asp:Content ID="cntScripts" ContentPlaceHolderID="ScriptSection" runat="server">
</asp:Content>
Categories.aspx.cs
using presenter;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using view;
namespace Example.Client.GUI.forum.Administration
{
public partial class Categories : Page, ICategoriesView
{
protected readonly CategoriesPresenter _presenter;
public Categories()
{
_presenter = new CategoriesPresenter(this);
}
public void FillCategoriesGrid(DataSet dataset)
{
gvCategories.DataSource = dataset;
gvCategories.DataBind();
}
public void ShowError(Exception ex)
{
pnlError.Visible = true;
litError.Text = ex.Message;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_presenter.LoadCategories();
}
}
protected void gvCategories_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCategories.EditIndex = e.NewEditIndex;
_presenter.LoadCategories();
}
protected void gvCategories_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCategories.EditIndex = -1;
_presenter.LoadCategories();
}
protected void gvCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = gvCategories.DataKeys[e.RowIndex].Value.ToString();
string categoryName = gvCategories.Rows[e.RowIndex].Cells[1].Text;
string dateadd = gvCategories.Rows[e.RowIndex].Cells[2].Text;
_presenter.UpdateCategory(id, categoryName, dateadd);
_presenter.LoadCategories();
}
protected void gvCategories_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = gvCategories.DataKeys[e.RowIndex].Value.ToString();
_presenter.DeleteCategory(id);
_presenter.LoadCategories();
}
}
}
CategoriesPresenter.cs
using mvp;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using view;
namespace presenter
{
public class CategoriesPresenter : Presenter<ICategoriesView>
{
protected readonly SqlConnection _conn;
public CategoriesPresenter(ICategoriesView view)
: base(view)
{
_conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString);
}
public void LoadCategories()
{
try
{
_conn.Open();
var command = _conn.CreateCommand();
command.CommandText = "SELECT category_id, category_name, date_added FROM forum_categories;";
var adapter = new SqlDataAdapter(command);
var dataset = new DataSet();
adapter.Fill(dataset);
View.FillCategoriesGrid(dataset);
}
catch (Exception ex)
{
View.ShowError(ex);
}
finally
{
if (_conn != null && _conn.State == ConnectionState.Open)
{
_conn.Close();
}
}
}
public void UpdateCategory(string id, string categoryName, string dateadd)
{
try
{
var command = _conn.CreateCommand();
command.CommandText = "UPDATE forum_categories SET category_name = @categoryName, date_added = @dateadd WHERE category_id = @id;";
command.CommandType = CommandType.Text;
command.Parameters.Add("@id", SqlDbType.Int).Value = id;
command.Parameters.Add("@categoryName", SqlDbType.VarChar, 100).Value = categoryName;
command.Parameters.Add("@dateadd", SqlDbType.DateTime).Value = Convert.ToDateTime(dateadd);
_conn.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
View.ShowError(ex);
}
finally
{
if (_conn != null && _conn.State == ConnectionState.Open)
{
_conn.Close();
}
}
}
public void DeleteCategory(string id)
{
try
{
var command = _conn.CreateCommand();
command.CommandText = "DELETE FROM forum_categories WHERE category_id = @id;";
command.CommandType = CommandType.Text;
command.Parameters.Add("@id", SqlDbType.Int).Value = id;
_conn.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
View.ShowError(ex);
}
finally
{
if (_conn != null && _conn.State == ConnectionState.Open)
{
_conn.Close();
}
}
}
}
}
Si necesitan mas informacion, no duden en comentarmelo que lo anexó a la pregunta.