你调用的对象是空的。错误显示使用gridview中的文件上载控件 [英] Object reference not set to an instance of an object. error show using file upload control in gridview

查看:65
本文介绍了你调用的对象是空的。错误显示使用gridview中的文件上载控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  Button1_Click(对象发​​件人,EventArgs e)
{

按钮Button1 =(按钮)发件人;
GridViewRow gvRow =(GridViewRow)Button1.NamingContainer;
FileUpload fuCOA =(FileUpload)gvRow.FindControl( FileUpload1);
Stream fs = fuCOA.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte [] bytes = br.ReadBytes(( Int32 )fs.Length);
标签ID =((标签)gvRow.FindControl( lblApplicantNo));
string sql_update = @ update ApplicantData set ApplicantFile = @ ApplicantFile;
sql_update + = 其中ApplicantNo = @ ApplicantNo;
SqlCommand cmdDist = new SqlCommand(sql_update,con);

cmdDist.Parameters.Add( @ ApplicantFile,SqlDbType.Binary ).Value = bytes;
cmdDist.Parameters.AddWithValue( @ ApplicantNo,ID.Text.ToString() );
con.Open();
int i = cmdDist.ExecuteNonQuery();
con.Close();
if (i > 0
{
Response.Redirect( ApplicantExamUpdate.aspx) ;
}

}

解决方案

使用调试器:拼写错误的机会可能是FileUpload1 错了,它在这一行找不到:

 FileUpload fuCOA =(FileUpload)gvRow.FindControl(   FileUpload1); 



如果不是这样,那很可能就是没有可用的文件 - 但你需要使用调试器来确切地确定你的语句的哪个部分是空的。

我们不能为你做那个 - 我们可以;运行你的代码!


protected void Button1_Click(object sender, EventArgs e)
        {
           
            Button Button1 = (Button)sender;
            GridViewRow gvRow = (GridViewRow)Button1.NamingContainer;
            FileUpload fuCOA = (FileUpload)gvRow.FindControl("FileUpload1");
            Stream fs = fuCOA.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            Label ID = ((Label)gvRow.FindControl("lblApplicantNo"));
            string sql_update = @" update ApplicantData set ApplicantFile=@ApplicantFile";
            sql_update += " where ApplicantNo=@ApplicantNo";
             SqlCommand cmdDist = new SqlCommand(sql_update, con);
            
             cmdDist.Parameters.Add("@ApplicantFile", SqlDbType.Binary).Value = bytes;
             cmdDist.Parameters.AddWithValue("@ApplicantNo", ID.Text.ToString());
                con.Open();
                int i = cmdDist.ExecuteNonQuery();
                con.Close();
                if (i > 0)
                {
                    Response.Redirect("ApplicantExamUpdate.aspx");
                }
           
        }

解决方案

Use the debugger: The chances are that you spelt "FileUpload1" wrong and it can't find it in this line:

FileUpload fuCOA = (FileUpload)gvRow.FindControl("FileUpload1");


If it isn't that, then it's likely that there is no file available - but you need to use the debugger to find out exactly which part of your statement is null to be sure.
We can't do that for you - we can;t run your code!


这篇关于你调用的对象是空的。错误显示使用gridview中的文件上载控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆