2

我正在使用 MVC3 和 SQL 2005。我收到错误:“将数据类型 varchar 转换为数字时出错。” 我相信这是因为我在 QTYORDER 中使用“1”而不是“1.00000”而生成的。对 QTYORDER 使用“1.00000”我现在看到错误:“无法将参数值从字符串转换为 Int32。”

我试过使用 Convert.ToInt32(Variable) ,如下所示:

cmd.Parameters.Add("@QTYSOLD", SqlDbType.Int).Value = Convert.ToInt32(QTYORDER);

它返回:“输入字符串的格式不正确。” Int.Parse(QTYORDER) 做同样的事情......

SQL 存储过程变量:

Procedure [dbo].[SVC_AddPart_To_Service_Call]      
    (@SRVRECTYPE smallint,
     @CALLNBR char(11),
     @TECHID char(11),
     @LINITMTYP char(3),
     @ITEMNMBR char(31),
     @QTY numeric(19,5) = 0,
     @QTYSOLD numeric(19,5) = 0,
     @STARTDATE datetime = '01/01/1900 00:00:00',   
     @STARTTIME datetime = '01/01/1900 00:00:00',
     @ENDDATE datetime = '01/01/1900 00:00:00',
     @ENDTIME datetime = '01/01/1900 00:00:00',
     @Status int output) As ...

MVC C# 代码:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CPLU"].ConnectionString);
var cmd = new SqlCommand("SVC_AddPart_To_Service_Call", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@SRVRECTYPE", SqlDbType.SmallInt).Value = "2";
cmd.Parameters.Add("@CALLNBR", SqlDbType.Char).Value = CALLNBR;
cmd.Parameters.Add("@TECHID", SqlDbType.Char).Value = Session["TechID"];
cmd.Parameters.Add("@LINITMTYP", SqlDbType.Char).Value = "P";
cmd.Parameters.Add("@ITEMNMBR", SqlDbType.Char).Value = ITEMNMBR;
cmd.Parameters.Add("@QTY", SqlDbType.Int).Value = QTYORDER;
cmd.Parameters.Add("@QTYSOLD", SqlDbType.Int).Value = QTYORDER;
cmd.Parameters.Add("@STARTDATE", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
cmd.Parameters.Add("@STARTTIME", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
cmd.Parameters.Add("@ENDDATE", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
cmd.Parameters.Add("@ENDTIME", SqlDbType.DateTime).Value = "1900-01-01 00:00:00.000";
cmd.Parameters.Add("@STATUS", SqlDbType.Int);
cmd.Parameters["@STATUS"].Direction = ParameterDirection.Output;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

MVC 剃刀代码:

@using (Html.BeginForm()) 
{
    @Html.ValidationSummary(true)
    <fieldset>

            @Html.LabelFor(model => model.CALLNBR, "Call Number")
            @Html.Label("CALLNBR", (string)ViewBag.CALLNBR)
            @Html.ValidationMessageFor(model => model.ITEMDESC)
        <br />
            @Html.LabelFor(model => model.ITEMNMBR, "Item Number")
            @Html.DropDownList("ITEMNMBR", (SelectList) ViewBag.Items, "Please Select a Part #")
            @Html.ValidationMessageFor(model => model.ITEMNMBR)
        <br />
            @Html.LabelFor(model => model.ITEMDESC, "Description")
            @Html.EditorFor(model => model.ITEMDESC)
            @Html.ValidationMessageFor(model => model.ITEMDESC)
        <br />
            @Html.LabelFor(model => model.QTYORDER, "Quantity")
            @Html.EditorFor(model => model.QTYORDER, new { required = "required"})
            @Html.ValidationMessageFor(model => model.QTYORDER)
        <br />
            <input type="submit" class="submit" value="Add Part" />

    </fieldset>
}

我试图通过表格传递的值:

CALLNBR = 0000198210 
ITEMNMBR = RF5-1885 (22 trailing spaces)
QTYORDER = 1

从 SQL 表中的一行获取的值:

CALLNBR = 0000198638  
ITEMNMBR = RM1-0043 (22 trailing spaces)
QTYORDER = 1.00000

问题:

1) 由于 SP 具有为 STARTDATE、STARTTIME、ENDDATE 和 ENDTIME 编码的默认值...在 MVC 应用程序上添加参数时是否需要提供这些值?

2) 我将如何在此处使用 Try-Catch,这会给我一个更好的错误消息吗?我实际上可以跟进的事情?

3) 有没有办法在 C# 中将 QTYORDER 值转换为小数点后 5 位?这将确保它以 #.00000 格式提交...

注意事项:

我正在使用实体框架,但我无法将存储过程映射到该表的选择插入函数,因为该 SP 用于添加零件,而另一个 SP 用于添加劳动力。

4

2 回答 2

3

要回答您的几个问题,您不需要将可选参数传递给存储过程——它们将使用它们的默认值。

关于 try-catch,只需在线快速搜索并使用您的断点——我会将它放在您上面提供的整个代码块周围。应该能够更好地了解引发的异常。

需要考虑的一些额外评论:

不要尝试 int.parse,而是使用 int.TryParse:

int orderQty = 0;
int.TryParse(QTYORDER, out orderQty);
if (orderQty == 0)
    //You potentially have a problem with binding 

此外,您的代码将 QTYORDER 作为整数传递,但存储过程接受小数。改变这个:

cmd.Parameters.Add("@QTY", SqlDbType.Int).Value = QTYORDER;

cmd.Parameters.Add("@QTY", SqlDbType.Decimal).Value = QTYORDER;
于 2013-01-07T18:01:53.180 回答
1

Are you sure QTYORDER is not null? MVC may have lost the model binding here. Do a quick check to see if its null

put this before you add params and add a breakpoint on the bool x line to see if it hits. If it is null then there is something wrong with the model binding, check your model code

if(QTYORDER == null){

     bool x = true;

}

Try trimming the string to ensure there is no spaces?

Convert.ToInt32(QTYORDER.Trim());

If you want to convert this to 5 decimal places

Math.Round(Convert.ToDecimal(QTYORDER.Trim()), 5);
于 2013-01-07T17:07:17.267 回答