Reputation: 4103
I am trying to get particular cell value of a column of gridview but received a NullReferenceException Error. I don't know why because I am using all controls in a right manner and with a right code. Please suggest me a solution regarding the same.
Code:
protected void lnkedit_Click(object sender, EventArgs e)
{
// LinkButton btndetails = sender as LinkButton;
//GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
//lblID.Text = gvdetails.DataKeys[gvrow.RowIndex].Value.ToString();
//// lblCompanyName.Text = gvrow.Cells[1].Text;
//txt_ComName.Text = gvrow.Cells[1].Text;
//txt_ShortName.Text = gvrow.Cells[2].Text;
//txt_email.Text = gvrow.Cells[3].Text;
//txt_website.Text = gvrow.Cells[4].Text;
string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:/ATTt/App_Data/eTimeTrackLite1.mdb;Persist Security Info=False;");
OleDbConnection conn = new OleDbConnection(str);
conn.Open();
OleDbCommand cmd = new OleDbCommand();
GridViewRow row = gvdetails.SelectedRow;
string index = row.Cells[0].Text.ToString(); **/*Error occure on this line*/**
cmd.CommandText = "select CompanyId from Companies where CompanyFName = '"+index+"'";
// cmd.Parameters.AddWithValue("@CompanyId", Convert.ToInt32(lblID.Text));
cmd.Connection = conn;
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
CompanyId = dt.Rows[0]["CompanyId"].ToString();
// da.UpdateCommand = cmd;
// cmd.ExecuteNonQuery();
this.ModalPopupExtender1.Show();
}
My Grid View:
Thanks in advance.
Upvotes: 0
Views: 1164
Reputation: 3026
Is it possible to handle SelectedIndexChanged
event instead of a link click? If so - just follow the MSDN example here - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow.aspx
Otherwise you need to check row
for null as there is no guarantee that there is a selected row when your event occurs:
GridViewRow row = gvdetails.SelectedRow;
if (row == null)
{
// Probably display some text to a user asking to select a row
return;
}
Upvotes: 1