Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 对象引用未设置为对象的实例_Asp.net - Fatal编程技术网

Asp.net 对象引用未设置为对象的实例

Asp.net 对象引用未设置为对象的实例,asp.net,Asp.net,我一直收到以下错误,我不知道如何修复它。请帮忙 异常详细信息:用户未处理NullReferenceException代码:对象引用未设置为对象的实例 protected void LbUpload_Click(object sender, EventArgs e) { ERROR: if(FileUpload.PostedFile.FileName == string.Empty) { LabelMsg.Visible =

我一直收到以下错误,我不知道如何修复它。请帮忙

异常详细信息:用户未处理NullReferenceException代码:对象引用未设置为对象的实例

protected void LbUpload_Click(object sender, EventArgs e)
    {     
      ERROR: if(FileUpload.PostedFile.FileName == string.Empty)
        {

            LabelMsg.Visible = true;
            return;
        }
        else
    {
        string[] FileExt = FileUpload.FileName.Split('.');
        string FileEx = FileExt[FileExt.Length - 1];
        if (FileEx.ToLower() == "csv")
        {
            FileUpload.SaveAs(Server.MapPath("CSVLoad//" + FileUpload.FileName));
        }
        else
        {
            LabelMsg.Visible = true;
            return;
        }
    }
    CSVReader reader = new CSVReader(FileUpload.PostedFile.InputStream);
    string[] headers = reader.GetCSVLine();
    DataTable dt = new DataTable();
    foreach (string strHeader in headers)
    dt.Columns.Add(strHeader);
    string[] data;
    while ((data = reader.GetCSVLine()) != null)
    dt.Rows.Add(data);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    if (FileUpload.HasFile)
        try
        {
            FileUpload.SaveAs(Server.MapPath("confirm//") +
            FileUpload.FileName);
            LabelGrid.Text = "File name: " +
            FileUpload.PostedFile.FileName + "<br>" +
            FileUpload.PostedFile.ContentLength + " kb<br>" +
            "Content type: " +
            FileUpload.PostedFile.ContentType + "<br><b>Uploaded Successfully";
        }
        catch (Exception ex)
        {
            LabelGrid.Text = "ERROR: " + ex.Message.ToString();

        }
    else
    {
        LabelGrid.Text = "You have not specified a file.";
    }
    File.Delete(Server.MapPath("confirm//" + FileUpload.FileName));
}
protectedvoid LbUpload\u单击(对象发送方,事件参数e)
{     
错误:if(FileUpload.PostedFile.FileName==string.Empty)
{
LabelMsg.Visible=true;
返回;
}
其他的
{
字符串[]FileExt=FileUpload.FileName.Split('.');
字符串FileEx=FileExt[FileExt.Length-1];
如果(FileEx.ToLower()=“csv”)
{
FileUpload.SaveAs(Server.MapPath(“CSVLoad/”+FileUpload.FileName));
}
其他的
{
LabelMsg.Visible=true;
返回;
}
}
CSVReader reader=新的CSVReader(FileUpload.PostedFile.InputStream);
string[]headers=reader.GetCSVLine();
DataTable dt=新的DataTable();
foreach(标题中的字符串streader)
dt.Columns.Add(strHeader);
字符串[]数据;
而((data=reader.GetCSVLine())!=null)
dt.Rows.Add(数据);
GridView1.DataSource=dt;
GridView1.DataBind();
if(FileUpload.HasFile)
尝试
{
FileUpload.SaveAs(Server.MapPath(“confirm/”)+
FileUpload.FileName);
LabelGrid.Text=“文件名:”+
FileUpload.PostedFile.FileName+“
”+ FileUpload.PostedFile.ContentLength+“kb
”+ “内容类型:”+ FileUpload.PostedFile.ContentType+“
上传成功”; } 捕获(例外情况除外) { LabelGrid.Text=“错误:”+ex.Message.ToString(); } 其他的 { LabelGrid.Text=“您尚未指定文件。”; } Delete(Server.MapPath(“confirm/”+FileUpload.FileName)); }
您确定FileUpload和FileUpload.PostedFile不为空吗?

无论是
FileUpload
还是它的
PostedFile
属性都必须为空。

您正在检查
FileName
是否为
string.Empty
,听起来像是要检测用户在没有选择文件的情况下单击按钮

如果发生这种情况,实际的
PostedFile
属性将为
null
(请记住,用户没有发布文件),您应该使用该属性:

protected void LbUpload_Click(object sender, EventArgs e)
{     
    if(FileUpload.HasFile)
    {

        LabelMsg.Visible = true;
        return;
    } 
    // ...
}
但我建议您也添加一个

有关验证的更多信息: