Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

 
Share this answer
 
Comments
VJ Reddy 22-May-12 6:24am    
Good reference. 5!
sravani.v 22-May-12 6:34am    
Thank you VJ
add your code at RowDataBound event. Then you get cell value.

In RowDataBound Event add following code.

declare a variable for store Row value.

<datatype> RowVal;
C#
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.

C#
protected void btncontact_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Admin/Company/Contact.aspx?CompanyName=" + RowVal );
}
 
Share this answer
 
Comments
sreeCoderMan 14-Dec-12 2:31am    
what is RowVal ? is it a string variable
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..
 
Share this answer
 
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.NET
<asp:TemplateField>
<ItemTemplate>
  <a href='DataBinder.Eval(Container.DataItem, "column name", "~/Admin/Company/Contact.aspx?CompanyName={0}")'>Select<a/>
<ItemTemplate/>
</asp:TemplateField>
 
Share this answer
 
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.
 
Share this answer
 
Comments
Lancy.net 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 21-May-12 0:27am    
http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/a9d3642e-f8f8-4ef6-a2a8-3ae8add98761
Clifford Nelson 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
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

JavaScript
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
 
Share this answer
 
Comments
[no name] 21-May-12 15:10pm    
Partial correct. There is no need for any JavaScript
Technoses 22-May-12 14:26pm    
plz explain your comment
[no name] 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)



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