Click here to Skip to main content
15,505,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in the first page five records will be displayed and in the second page another five records will be displayed.

suppose in the first page i check two rows and in the second page i check another two rows means that selected check box rows to be update in the [transact].[transaction_item] in the table.



C#
protected void Page_Load(object sender, EventArgs e)
        {
           if (!IsPostBack)
                {
                     BindData();
                }
       }


 protected void BindData()
        {
             String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                SqlCommand cmd = new SqlCommand("select * from [transact].[transaction_item] where status = 'new'", con);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                da.Fill(ds);
                grdRpt.DataSource = ds;
                grdRpt.DataBind();
        }


 protected void grdRpt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            SaveCheckedValues();
            grdRpt.PageIndex = e.NewPageIndex;
            BindData();
            PopulateCheckedValues();
        }


private void PopulateCheckedValues()
        {
            System.Collections.ArrayList userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
            if (userdetails != null && userdetails.Count > 0)
            {
                foreach (GridViewRow gvrow in grdRpt.Rows)
                {
                    int index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
                    if (userdetails.Contains(index))
                    {
                        CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkselecdata");
                        myCheckBox.Checked = true;
                    }
                }
            }
        }

   private void SaveCheckedValues()
        {
            System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in grdRpt.Rows)
            {
                index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;
                bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

               
                if (Session["CHECKED_ITEMS"] != null)
                    userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!userdetails.Contains(index))
                        userdetails.Add(index);
                }
                else
                    userdetails.Remove(index);
            }
            if (userdetails != null && userdetails.Count > 0)
                Session["CHECKED_ITEMS"] = userdetails;
        }


when i run the above code shows error as follows
specified cast is not valid

The above error shows in below line as follows
index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;

how to fix this error.

What I have tried:

Refer to above...
Posted
Updated 17-Jun-18 20:52pm
v2

try int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value)
 
Share this answer
 
Comments
nar86 18-Jun-18 3:43am    
i try this but error shows in below line as follows

private void SaveCheckedValues()
{
System.Collections.ArrayList userdetails = new System.Collections.ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdRpt.Rows)
{
int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value);

bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!userdetails.Contains(index))
userdetails.Add(index);
}
else
userdetails.Remove(index);
}
if (userdetails != null && userdetails.Count > 0)
Session["CHECKED_ITEMS"] = userdetails;
}


when i run the above code shows error as follows

'Unable to cast object of type 'System.DBNull' to type 'System.String

The error shows in below line as follows

int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value);

how to solve this error please help me.
Manish K. Agarwal 19-Jun-18 6:33am    
please refer https://stackoverflow.com/questions/21277969/getting-datakey-value-for-each-row-in-grid-view
Quote:
specified cast is not valid

The above error shows in below line as follows
C#
index = (int)grdRpt.DataKeys[gvrow.RowIndex].Value;

You can't, or at least you can't directly.
The error message is pretty clear: You cannot cast the data in that cell to an integer. Which means that the data isn't an integer number - we don't have any access to your data, so we cannot tell exactly what you are retrieving. Start by "breaking" the line:
C#
object o = grdRpt.DataKeys[gvrow.RowIndex].Value;
index = (int) o;
and use the debugger to look at exactly what you get from the grid. That should start to give you a clue as to where the problem is: data, code, grid, ...

But we can't do that for you - we have no access to your data or to your code while it is running!
 
Share this answer
 

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

  Print Answers RSS


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