Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi,
 
I have a gridview CompanyGv1.i need to get the selected rows value
 

protected void btncontact_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/Company/Contact.aspx?CompanyName=" + CompanyGv1.SelectedRow.Cells[1].Text);
    
    }
 
getting error "Object reference not set to an instance of an object".
please help.
 
Thanks.
Posted 20-May-12 18:17pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
VJ Reddy at 22-May-12 6:24am
   
Good reference. 5!
sravani.v at 22-May-12 6:34am
   
Thank you VJ
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

add your code at RowDataBound event. Then you get cell value.
 
In RowDataBound Event add following code.
 
declare a variable for store Row value.
 
RowVal;
protected void CompanyGv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
         RowVal = e.Row.Cells[1].Text.ToUpper();
    }
}
 
And then pass this variable to your code.
 
protected void btncontact_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Admin/Company/Contact.aspx?CompanyName=" + RowVal );
}
  Permalink  
Comments
sreeCoderMan at 14-Dec-12 2:31am
   
what is RowVal ? is it a string variable
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

hey
 
rowcommand event will help u, give commandargument to your control like select button and get the current row thats it just fetch the value of the current row.
 
if you have different case please specify.
 
all the best..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

You don't need to use any server events for this. Use the ItemTemplate to create a button, or link, that will navigate as you wish
 
<asp:TemplateField>
<ItemTemplate>
  <a href='DataBinder.Eval(Container.DataItem, "column name", "~/Admin/Company/Contact.aspx?CompanyName={0}")'>Select<a/>
<ItemTemplate/>
</asp:TemplateField>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is just a guess, but maybe you want :
 
Response.Redirect("~/Admin/Company/Contact.aspx?CompanyName=" + CompanyGv1.SelectedRow.Cells[0].Text);
 
However, I would check to see which part is giving you the null exception. During debugging, put a breakpoint on this line. YOu can hover over the part, or you can type in immediate window.
  Permalink  
Comments
Arokia Mary at 21-May-12 0:01am
   
Hi Thanks for yr reply.Cells[1] i have the value what i need.
Every row has a button on button click i should get Cells[1] value.
when i chk with break point SelectedRow=null.
please correct.
Unareshraju at 21-May-12 0:27am
   
http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/a9d3642e-f8f8-4ef6-a2a8-3ae8add98761
Clifford Nelson at 21-May-12 11:37am
   
That tells me that a row has not been selected (clicking the button obviously does not cause the row to be selected). You may be able to cast the sender. and get parent. Not sure since do not have code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

you can use javascript in place of CS
 
add template field in grid view and then add button on that template field
add js function go(this) on that button
 
and add javascript in your page
with
 
function go(th)
{
 alert("pagepath.aspx?"+th.parentNode.parentNode.chieldNode[1].innerText)
 window.location.href="pagepath.aspx?"+th.parentNode.parentNode.chieldNode[1].innerText
}
 

 
this work fast in place of code behiend
  Permalink  
Comments
Mark Nischalke at 21-May-12 15:10pm
   
Partial correct. There is no need for any JavaScript
Technoses at 22-May-12 14:26pm
   
plz explain your comment
Mark Nischalke at 22-May-12 14:42pm
   
Your answer was partially correct in using a template field, but it is not necessary to use any JavaScript to get the results desired by the OP.

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 22 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100