Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have grid view and i want to take cell value from one column and use that value to navigate to another page that shows complete profile of that student
instead of taking value from cell it is only taking zero as argument

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
    {
        string year = TextBox1.Text;
        studentDataContext db = new studentDataContext();
        var stud = from p in db.s_profs
                   where p.stdpyear == TextBox1.Text || p.stdname == TextBox2.Text
                   select new { p.stud_id, p.stitle, p.stdname, p.stdcurcity, p.stdorgnm, p.stdpyear, p.stdcourse };

        /* var q =
             from a in db.GetTable<stud_reg>()
             where a.uyear == TextBox1.Text
             select  a.category, a.uprefix,a a.uemail ;*/
        GridView1.DataSource = stud;
        GridView1.DataBind();
    }


protected void btnSubmit_Click(object sender, EventArgs e)
   {
       Response.Redirect("gridprof.aspx?stud_id =" + studu);
   }



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    studu = Convert.ToInt16(row.Cells[1].Text);

    Response.Write(studu);

}
Posted
Updated 4-May-18 3:28am
Comments
Maciej Los 3-May-18 16:52pm    
Try to debug your code to find out where you made mistake.

1 solution

There might be few reasons of your issue, for example row.Cells[1] does not contain student id or a value passed to the Convert.ToInt16() method exceeds Int16.MaxValue[^].

I'd try to get student id this way:
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = ContactsGridView.Rows[index];
     studu = Convert.ToInt32(row.Cells[1].Text);
}


More at MSDN: GridView.RowCommand Event (System.Web.UI.WebControls)[^]
 
Share this answer
 

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



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