Samar
Samar

Reputation: 11

Object reference not set to an instance of an object

hello can any one help me :)

whats the problem in this code ??

protected void LinkButton1_Click(object sender, EventArgs e)
{
    //object o = new object();
    //Control co = new Control();
    //co = GridView1.FindControl("EmpFileUpload");
    FileUpload f = new FileUpload();
    (System.Web.UI.WebControls.FileUpload)f = (System.Web.UI.WebControls.FileUpload)(GridView1.FindControl("EmpFileUpload"));

    if (f.HasFile)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolsConnectionString"].ConnectionString);
        conn.Open();
        SqlCommand insertCommand = new SqlCommand("insert_Empimg", conn);

        insertCommand.Parameters.Add("Emp_imgPath", SqlDbType.NVarChar, 0).Value =f.FileName;

        insertCommand.CommandType = CommandType.StoredProcedure;
        insertCommand.ExecuteNonQuery();

        conn.Close();
    }



}

Upvotes: 1

Views: 1219

Answers (3)

Anyname Donotcare
Anyname Donotcare

Reputation: 11423

FileUpload f = (System.Web.UI.WebControls.FileUpload)(GridView1.FindControl("EmpFileUpload"));

And finding the control by GridView1.FindControl("EmpFileUpload") depends on where you put your file upload in the grid view . please put your aspx shot to identify clearly where you put your control.to access it in the correct way.

Upvotes: 0

Richard Friend
Richard Friend

Reputation: 16018

It's possible that your FindControl is not finding the upload control.

Lets tidy this code up a bit..

//You dont need the `new` as you are assigning to the result of `FindControl`
FileUpload f = GridView1.FindControl("EmpFileUpload") as FileUpload;

//Check for null here, this is probably your problem
if (f !=null && f.HasFile)     
{       
    //Using statement takes care of closing our connection and disposing our objects.
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolsConnectionString"].ConnectionString))
    {
         conn.Open();         
         using (SqlCommand insertCommand = new SqlCommand("insert_Empimg", conn))
         {          
             insertCommand.Parameters.Add("Emp_imgPath", SqlDbType.NVarChar, 0).Value =f.FileName;          
             insertCommand.CommandType = CommandType.StoredProcedure;         
             insertCommand.ExecuteNonQuery();
         }
    }
}

Then we can have a look at why it is not being found, could you paste your markup that declares your EmpFileUpload

Upvotes: 1

Antony Woods
Antony Woods

Reputation: 4472

Firstly

FileUpload f = new FileUpload();
(System.Web.UI.WebControls.FileUpload)f = (System.Web.UI.WebControls.FileUpload(GridView1.FindControl("EmpFileUpload"));

You don't need to 'new' f if you're reassigning it on the next line.

FileUpload f = (System.Web.UI.WebControls.FileUpload(GridView1.FindControl("EmpFileUpload"));

Secondly, you need to check f isn't null by the sounds of it.

if(f != null && f.HasFile)

Upvotes: 1

Related Questions