1

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();
    }



}
2
  • It is difficult to know just by looking at the code. You should look at the exception's stack trace, and find out which line is exactly throwing the exception. Commented May 10, 2011 at 11:41
  • 3
    Welcome to Stackoverflow! Let me give you some hints that might help you get good answers to your question: (a) If you get an error message, tell us in which line of your code this error occurs (this usually shown by Visual Studio). (b) Please format your code. This has been done by Richard for your right now; next time, please do it yourself. Commented May 10, 2011 at 11:41

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

And you may need to find the row of the control you are looking for and do a find control there. Gridviews can be picky about it. There could but several controls in a gridview with that id. But only one in any given row.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.