vb.net 值不能为空,参数名称:dataTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32523842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:27:03  来源:igfitidea点击:

Value Cannot be null, Parameter name: dataTable

.netsql-servervb.net

提问by Simon Price

I am using SQL Database and am calling a stored proc from VB.Net, however when I try and will my DataTable I am getting the error as per the title.

我正在使用 SQL 数据库并从 VB.Net 调用存储过程,但是当我尝试使用我的 DataTable 时,我收到了标题中的错误。

My code is:

我的代码是:

Dim dt As DataTable
Using sqlConn As New SqlConnection(_connstr)
    Dim sqlcmd As New SqlCommand()
    Dim dateParam = sqlcmd.Parameters.Add("@reportDate", SqlDbType.Date)
    Dim dateParam2 = sqlcmd.Parameters.Add("@reportDateEndRange", SqlDbType.Date)
    'Dim reportType = sqlcmd.Parameters.Add("@reportType", SqlDbType.VarChar)
    Dim companyId = sqlcmd.Parameters.Add("@companyID", SqlDbType.Int)
    dateParam.Value = DateTime.Now.AddDays(-1)
    dateParam2.Value = DateTime.Today
    'reportType.Value = "'IRF', 'TRF'"
    companyId.Value = 1

    sqlcmd.Connection = sqlConn
    sqlcmd.CommandType = CommandType.StoredProcedure
    sqlcmd.CommandText = "dbo.uspSearchRequest"

    Using sqlda As New SqlDataAdapter(sqlcmd)
        sqlda.Fill(dt)
    End Using
End Using

But when i get to the line sqlda.Fill(dt)I get the error as per the tile.

但是当我到达这条线时,sqlda.Fill(dt)我收到了瓷砖上的错误。

I have tested these parameters in the stored proc and I get results.

我已经在存储过程中测试了这些参数并得到了结果。

Any ideas on how to stop this?

关于如何阻止这种情况的任何想法?

=========================== edit 1

============================ 编辑 1

ALTER procedure [dbo].[uspSearchRequest]
@reportDate date,
@reportDateEndRange date,
@companyID int

as
begin

select 
null as [Report Manager],
null as [Report Detail],
null as [Form],
null as [Attachment],
req.OverallStatus as [Result],
req.ReportNumber as [Report Number],
req.ReportDate as [Report Date],
req.FormNumber  as [Form Number],
req.SubmittedTimestamp as [Submit Date],
req.ApplicantContactPerson  as [Applicant] ,
req.Brand  as [Brand],
req.Department  as [Department]
from ias.dbo.request req
where req.ReportDate  between @reportDate and @reportDateEndRange 
and req.RequestCompanyID = @companyID
end 
go

回答by Munavvar Husein

You have to initialize the DataTable first:

您必须先初始化 DataTable:

Dim dt As DataTable = New DataTable()