错误的无效参数,无法从"ref int"转换为"ref int?" [英] Error invalid arguments, cannot convert from 'ref int' to 'ref int?'
问题描述
你好
此存储过程有效,并在sql server中返回"14"值.
现在,我想通过linq在asp.net中使用它.因此,我在网络表单中使用了1个标签和1个按钮.现在,我喜欢按按钮时它执行存储过程,并将值返回到"lable1".
我执行以下操作,但出现错误:
错误1"WebApplication4.DataClasses1DataContext.ap_InventoryByMakeModel_Count(string,string,ref int?)"的最佳重载方法匹配具有一些无效的参数
错误2参数"3":无法从"ref int"转换为"ref int?"
最佳致谢
-------------------------------------------------- ------------------
存储过程:
ALTER proc [dbo].[ap_InventoryByMakeModel_Count]
@ Make varchar ( 50 )= 空,- 条件
@模型 varchar ( 50 )= 空,- 条件
@ Count int 输出
/* 测试:
声明@count int
exec ap_InventoryByMakeModel_Count'Compaq','LN40定影单元,110V',@count输出
选择@count count
*/
为
选择 @ Count = count(*)
来自 dbo.InventorySum
其中使类似 @ Make
和模型 like @ Model
返回
------------------------------- -----------------------
Default.aspx.cs
受保护的 无效 Button1_Click(对象发件人,EventArgs e)
{
DataClasses1DataContext资产= 新 DataClasses1DataContext();
int count = 0 ;
int totalrows = asset.ap_InventoryByMakeModel_Count(" span>," , ref 计数);
Label1.Text = Convert.ToString(总计);
}
--------------------------------------------- ------------------------
Default.aspx
< body > ;
< 表单 =" form1" =" 服务器" < div >
< asp:Label ID =" runat 服务器" 文本 标签" < /asp:Label >
< asp:Button ID =" runat 服务器" onclick Button1_Click" 文本 按钮" / >
< /div >
< /form >
< /body >
< /html > 上一个> ------------------------------------------------ ----------------------------------- 解决方案
这里的问题是您的存储过程可能返回DBNUll.Value
,这将被映射到VB中的Nothing
或C#中的null
.但是,您传入的是int
,它不能具有值null
(null
将转换为0,而这并不是存储过程返回的值).而是将您的count
变量设置为int?
.
附加问号将创建 Nullable< int> [ 受保护的 无效 Button1_Click(对象发件人,EventArgs e)
{
DataClasses1DataContext资产= 新 DataClasses1DataContext();
int ? count = 0 ; // 注意问号!
int totalrows = asset.ap_InventoryByMakeModel_Count(" span>," , ref 计数);
Label1.Text = Convert.ToString(总计);
}
希望能帮助到你! :)
另外,如果我没记错的话,您希望在count
变量中返回计数的行数.这与SP返回的结果不同,SP将返回受影响的行数.
在这种情况下,您可能需要这样:
Label1.Text = Convert.ToString(count); // 或count.Value(?)
请记住,尽管count
可以是null
(更精确的count.Value
可以是null
).我不确定Convert.ToString
是否可以成功转换null
值...我相信您会很快发现这一点的:)
Hello
This stored procedure works and returns "14" value in sql server.
Now I like to use that in asp.net by using linq. So I have used 1 lable and 1 button in my webform. Now I like when I push the button it execute the stored procedure and returns value to "lable1".
I do those like below and I have errors:
Error 1 The best overloaded method match for 'WebApplication4.DataClasses1DataContext.ap_InventoryByMakeModel_Count(string, string, ref int?)' has some invalid arguments
Error 2 Argument '3': cannot convert from 'ref int' to 'ref int?'
Best Regards
--------------------------------------------------------------------
Stored procedure:
ALTER proc [dbo].[ap_InventoryByMakeModel_Count]
@Make varchar(50) = null, -- criteria
@Model varchar(50) = null, -- criteria
@Count int output
/* test:
declare @count int
exec ap_InventoryByMakeModel_Count 'Compaq', 'LN40 Fuser Unit, 110V', @count output
select @count count
*/
as
select @Count = count(*)
from dbo.InventorySum
where Make like @Make
and Model like @Model
return
------------------------------------------------------
Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
DataClasses1DataContext asset = new DataClasses1DataContext();
int count = 0;
int totalrows = asset.ap_InventoryByMakeModel_Count("compaq", "LN40 Fuser Unit, 110V", ref count);
Label1.Text = Convert.ToString(totalrows);
}
---------------------------------------------------------------------
Default.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
-----------------------------------------------------------------------------------
The problem here is that your stored procedure could returnDBNUll.Value
, which would be mapped toNothing
in VB, ornull
in C#. You are passing in anint
though, which cannot have the valuenull
(null
will be converted to 0, and that''s just not what your stored procedure returns). Instead make yourcount
variable anint?
.
Appending the questionmark will create a Nullable<int>[^] which CAN hold anull
value.
So your code should be changed to:
protected void Button1_Click(object sender, EventArgs e) { DataClasses1DataContext asset = new DataClasses1DataContext(); int? count = 0; //Notice the questionmark! int totalrows = asset.ap_InventoryByMakeModel_Count("compaq", "LN40 Fuser Unit, 110V", ref count); Label1.Text = Convert.ToString(totalrows); }
Hope it helps! :)
Edit:
Also, if I''m not mistaken, you want the number of counted rows returned in thecount
variable. This is NOT the same as the result returned by the SP, which will return the number of affected rows.
In this case you might want this:
Label1.Text = Convert.ToString(count); // or count.Value (?)Remember though that
count
can benull
(more precisecount.Value
can benull
). I am not sure ifConvert.ToString
can successfully convertnull
values... I am sure you''ll figure that out soon enough though :)
这篇关于错误的无效参数,无法从"ref int"转换为"ref int?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!