I am getting an error while i press update button in Gridview.
The Error is:-
Invalid postback or callback argument. Event validation
is enabled using in
configuration or <%@ Page EnableEventValidation="true" %> in a
page. For security purposes, this feature verifies that arguments to
postback or callback events originate from the server control that
originally rendered them. If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
I am getting the error when i press "tick ImageButton.Here it is:-
My code for the RowUpdating is:-
protected void dgvDivision_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int DivId = Convert.ToInt32(dgvDivision.DataKeys[e.RowIndex].Value.ToString());
string DivName = ((TextBox)dgvDivision.Rows[e.RowIndex].FindControl("txtDivisionName")).Text;
decimal ClassId = decimal.Parse(((TextBox)dgvDivision.Rows[e.RowIndex].FindControl("txtClassName")).Text);
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand sqlCmd = new SqlCommand("", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@divisionId", DivId);
sqlCmd.Parameters.AddWithValue("@divisionName", DivName);
sqlCmd.Parameters.AddWithValue("@classId", ClassId);
int Incount = sqlCmd.ExecuteNonQuery();
if (Incount > 0)
{
HttpContext.Current.Response.Write("");
}
dgvDivision.EditIndex = -1;
gridFill();
}
please Help me in this topic.

Sanjeeb LenkaPosted Aug 26, 2013, 5:35 AM
your parent table didn't contain the classid 393. thats why you are getting this error.
As you classid is dependent so if you are updating any classid which is not present in parent table you will get error. so try to give those classid which are present in your parent table on updating.
Bineesh ViswanathPosted Aug 26, 2013, 5:45 AM
Iftikar HussainPosted Aug 26, 2013, 5:34 AM
divisionViewAll
Bineesh ViswanathPosted Aug 26, 2013, 5:31 AM
See my tbl_class from where the classId is coming:-
And the data stored in tbl_Exam :-
Iftikar HussainPosted Aug 26, 2013, 5:22 AM
Regards,
Iftikar
Bineesh ViswanathPosted Aug 26, 2013, 5:21 AM
The breakpoint stuck the ExecuteNonQuery() line.
I know that this error is beacause of Pk and Fk relation.
how can avoid this error?
Iftikar HussainPosted Aug 26, 2013, 5:08 AM
Regards,
Iftikar
Bineesh ViswanathPosted Aug 26, 2013, 5:04 AM
1) protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
mvDivision.ActiveViewIndex = 0;
comboFill();
gridFill();
}
else
{
mvDivision.ActiveViewIndex = 1;
}
}
2) GridView1.EnableViewState="True"
A new problem been arised:-
Why this happening?
Sanjeeb LenkaPosted Aug 26, 2013, 4:59 AM
change your page load to below code and check:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
mvDivision.ActiveViewIndex = 0;
comboFill();
gridFill();
}
else
{
mvDivision.ActiveViewIndex = 1;
}
}
Iftikar HussainPosted Aug 26, 2013, 4:49 AM
Regards,
Iftikar
Bineesh ViswanathPosted Aug 26, 2013, 4:45 AM
public partial class Division : System.Web.UI.Page
{
protected SqlConnection sqlCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\BINEESH\ASP\SchoolERP\App_Data\DB_schoolERP.mdf;Integrated Security=True;Connect Timeout=200;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
mvDivision.ActiveViewIndex = 0;
comboFill();
}
else
{
mvDivision.ActiveViewIndex = 1;
gridFill();
}
}
public void comboFill()
{
SqlDataAdapter sqlDa = new SqlDataAdapter("select * from tbl_Class", sqlCon);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
cmbClassName.DataSource = dtbl;
cmbClassName.DataValueField = "classId";
cmbClassName.DataTextField = "className";
cmbClassName.DataBind();
}
protected void mvDivision_ActiveViewChanged(object sender, EventArgs e)
{
}
public void gridFill()
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand sqlCmd = new SqlCommand("divisionViewAll", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rdr = sqlCmd.ExecuteReader();
DataTable dtbl = new DataTable();
dtbl.Load(rdr);
dgvDivision.DataSource = dtbl;
dgvDivision.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
divisionSP spDiv = new divisionSP();
divisionInfo InfoDiv = new divisionInfo();
InfoDiv.divisionName = txtDivision.Text;
InfoDiv.classId = decimal.Parse(cmbClassName.SelectedValue.ToString());
spDiv.DivisionAdd(InfoDiv);
if (IsPostBack)
{
mvDivision.ActiveViewIndex = 1;
gridFill();
}
}
protected void dgvDivision_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
mvDivision.ActiveViewIndex = 0;
int inId = Convert.ToInt32(e.CommandArgument);
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlDataAdapter sqlDa = new SqlDataAdapter("select * from tbl_Division where divisionId='" + inId + "'", sqlCon);
DataSet DS = new DataSet();
sqlDa.Fill(DS);
txtDivision.Text = Convert.ToString(e.CommandArgument.ToString());
}
}
protected void dgvDivision_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int inId = Convert.ToInt32(dgvDivision.DataKeys[e.RowIndex].Value.ToString());
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand sqlCmd = new SqlCommand("delete from tbl_Division where divisionId='" + inId + "'", sqlCon);
sqlCmd.Parameters.Add("@divisionId", inId);
int inCount=sqlCmd.ExecuteNonQuery();
if (inCount > 0)
{
HttpContext.Current.Response.Write("");
}
mvDivision.ActiveViewIndex = 1;
gridFill();
}
protected void dgvDivision_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
dgvDivision.EditIndex = -1;
gridFill();
}
protected void dgvDivision_RowEditing(object sender, GridViewEditEventArgs e)
{
dgvDivision.EditIndex = e.NewEditIndex;
gridFill();
}
protected void dgvDivision_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int DivId = Convert.ToInt32(dgvDivision.DataKeys[e.RowIndex].Value.ToString());
string DivName = ((TextBox)dgvDivision.Rows[e.RowIndex].FindControl("txtDivisionName")).Text;
decimal ClassId = decimal.Parse(((TextBox)dgvDivision.Rows[e.RowIndex].FindControl("txtClassName")).Text);
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand sqlCmd = new SqlCommand("", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@divisionId", DivId);
sqlCmd.Parameters.AddWithValue("@divisionName", DivName);
sqlCmd.Parameters.AddWithValue("@classId", ClassId);
int Incount = sqlCmd.ExecuteNonQuery();
if (Incount > 0)
{
HttpContext.Current.Response.Write("");
}
dgvDivision.EditIndex = -1;
gridFill();
}
}
Sanjeeb LenkaPosted Aug 26, 2013, 4:29 AM
you tried my solution or not. better share your full .cs code.
Iftikar HussainPosted Aug 26, 2013, 4:08 AM
Regards,
Iftikar
Bineesh ViswanathPosted Aug 26, 2013, 4:05 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Division.aspx.cs" Inherits="Division" EnableEventValidation="false" %>
Sanjeeb LenkaPosted Aug 26, 2013, 3:15 AM
put your gridfil method in pageload within Is not equal to postback:
ex:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridFill();
}
}
Iftikar HussainPosted Aug 26, 2013, 3:13 AM
Set this in page directive
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" EnableEventValidation="false" %>
Regards,
Iftikar