Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i m deleting row then showing error is Object reference not set to an instance of an object.


C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.IO;
namespace EDS
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        String strcon = ConfigurationManager.ConnectionStrings["con"].ToString();
        SqlConnection Scon;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridviewData();
            }
        }
  private void BindGridviewData()
{
    Scon = new SqlConnection(strcon);

Scon.Open();
SqlCommand cmd = new SqlCommand("select * from SaveData",Scon);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Scon.Close();
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
   protected void lnkDownload_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string filePath = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}

   protected void lnkdelete_Click(object sender, EventArgs e)
   {

       LinkButton lnkbtn = sender as LinkButton;
       //getting particular row linkbutton
       GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
       //getting userid of particular row
       string filePath = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
       
       //string username = gvrow.Cells[0].Text;
       Scon.Open();
       SqlCommand cmd = new SqlCommand("delete from SaveData where FilePath=" + filePath, Scon);
       int result = cmd.ExecuteNonQuery();
       Scon.Close();
       if (result == 1)
       {
           BindGridviewData();
           //Displaying alert message after successfully deletion of user
           ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + filePath + " details deleted successfully')", true);
       }
Posted
Updated 20-Sep-13 20:19pm
v3
Comments
phil.o 21-Sep-13 2:21am    
Which line throws the exception?
sp_suresh 21-Sep-13 2:23am    
Which line error occurs ?

1 solution

Without knowing exactly which line throws the exception (and the exception detail tells you that, it will even give you the line number) we can't be exact, but the first thing to do is look at the sender parameter - it will be the control that raised the event, but is it a LinkButton? If it isn't, or a control derived from LinkButton, then the as operator will return a null, which would cause the problem.

If it isn't that (and you have double checked that), then put a breakpoint on the first line of the method, and single step through, checking each part of each line for null values.

Sorry, but we can't help more without better info!
 
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