我的ID不为null,为什么会收到运行时错误?

莫斯塔法·布扎里(Mostafa Bouzari)

我收到此运行时错误,但我id不是null,我在代码中对其进行了检查(您将看到该代码),并且我正在使用telerikGridView

这是运行时错误

参数化查询'(@@ fName nvarchar(4),@ _ lName nvarchar(2),@ _ phone nvarchar(6),@ _ a'需要未提供的参数'@_id'。

我正在尝试id从另一种形式获取该内容,这就是为什么我这样写的原因

这是我在第一层的代码

private void btnEdt_Click(object sender, EventArgs e)
{
        Ref_View_Model = new View_model._View_Model();
        Ref_C = new Customers();

        foreach (var RowInfo in Ref_C.radGridView1.SelectedRows)
        {
            FireCell = RowInfo.Cells[0].Value.ToString();

            if (FireCell==null)
            {
                MessageBox.Show("null");
            }
        }

        //Ref_C.radGridView1.CurrentRow.Delete();
        Ref_C.customersTableAdapter.Update(Ref_C.sales_and_Inventory_SystemDataSet);
        Ref_C.customersTableAdapter.Fill(Ref_C.sales_and_Inventory_SystemDataSet.Customers);

        Ref_View_Model.GetEditCustomers(FireCell, txtFName.Text, txtLName.Text, txtPhn.Text, txtDdrss.Text);
} 

这是我在最后一层的代码

public void EditCustomres( string _id,string _fName, string _lName,string _phone, string _address)
{

        Connection_String = @"Data Source=MOSTAFA-PC;Initial Catalog=" + "Sales and Inventory System" + ";Integrated Security=TrueData Source=MOSTAFA-PC;Initial Catalog=" + "Sales and Inventory System" + ";Integrated Security=True;";
        Query = "update Customers " +
                                "set FName=@_fName  ,LName=@_lName  ,Phone=@_phone ,[Address]=@_address" +" "+
                                  "where Id like @_id";
        using (Con=new SqlConnection(Connection_String))
        using (Cmd=new SqlCommand(Query,Con))
        {
            Cmd.Parameters.Add("@_fName", SqlDbType.NVarChar);
            Cmd.Parameters.Add("@_lName", SqlDbType.NVarChar);
            Cmd.Parameters.Add("@_phone", SqlDbType.NVarChar);
            Cmd.Parameters.Add("@_address", SqlDbType.NVarChar);
            Cmd.Parameters.Add("@_id", SqlDbType.Int);

            Con = Cmd.Connection;
            Con.Open();

            Cmd.Parameters["@_fName"].Value = _fName;
            Cmd.Parameters["@_lName"].Value = _lName;
            Cmd.Parameters["@_phone"].Value = _phone;
            Cmd.Parameters["@_address"].Value = _address;
            Cmd.Parameters["@_id"].Value = _id;

            Cmd.ExecuteNonQuery();
            Cmd.Dispose();
            }
} 
杰西霍温

在这种情况下,您确实不希望使用LIKE,除非您有非常特定的需求来更新看起来像要传递的ID的东西,但事实并非如此...

您正在传递@_id作为SqlDbType.Int,但实际上为变量类型的string,这将导致一个转换引擎盖下发生的。如果您传入的是空字符串,则该值将转换为null,这将导致您在帖子中提到的错误。检查导致调用的代码,EditCustomres以确保它实际上传递了正确的值。像我添加的一样,添加参数检查将帮助您更早地在调用堆栈中跟踪此类问题。

第一步是摆脱您的LIKE声明,因此请更新查询以使用:

where id = @_id

然后更新代码,因为您希望_id变量为int,所以不能只给它分配一个字符串,这可能就是为什么将其重置为的原因null更新方法以接受int参数而不是string id,或者int在添加参数之前解析方法中的:

int idValue = -1;
if (!int.TryParse(_id, NumberStyles.Integer, CultureInfo.InvariantCulture, out idValue))
{
    throw new ArgumentException("id", "Id must be a string which contains an integer value, the value of 'id' was: '" + _id + "'"); 
}

Cmd.Parameters.Add("@_id", SqlDbType.Int).Value = idValue;

您可能需要在文件中添加using语句:

using System.Globalization;

这是NumberStylesCultureInfo定义。通常,Visual Studio将建议在这里你可以找到这些物品crtl+.会弹出一个屏幕,它会自动添加此语句。


最终结果将是:

using System.Globalization;

....
....

public void EditCustomers( string _id,string _fName, string _lName,string _phone, string _address)
{
    int idValue = -1;
    if (!int.TryParse(_id, NumberStyles.Integer, CultureInfo.InvariantCulture, out idValue))
    {
       string message = "Id must be a string which contains an integer value, the value of 'id' was: '" + _id + "'";
       throw new ArgumentException("_id", message); 
    }

    Connection_String = @"Data Source=MOSTAFA-PC;Initial Catalog=" + "Sales and Inventory System" + ";Integrated Security=TrueData Source=MOSTAFA-PC;Initial Catalog=" + "Sales and Inventory System" + ";Integrated Security=True;";
    Query = @"update Customers 
                  set FName=@_fName, LName=@_lName, Phone=@_phone,[Address]=@_address
                  where Id = @_id";

    using (Con=new SqlConnection(Connection_String))
    using (Cmd=new SqlCommand(Query, con))
    {
        Cmd.Parameters.AddWithValue("@_fName", _fName);
        Cmd.Parameters.AddWithValue("@_lName", _lName);
        Cmd.Parameters.AddWithValue("@_phone", _phone);
        Cmd.Parameters.AddWithValue("@_address", _address);
        Cmd.Parameters.AddWithValue("@_id", idValue);

        Con.Open();
        Cmd.ExecuteNonQuery();
    }
}   

我也简化了代码。在您的SQL语句中使用逐字字符串,删除对的调用Dispose(您已经使用using(cmd)并删除了Con=Cmd.Connection,因为它实际上没有任何其他值。

.AddWithValue与单独创建和设置值相比,使用该方法也更容易。


确实需要解决方案,这些解决方案要求您将字符串连接起来以在SQL Server端上形成SQL查询,这可能会导致值注入不需要的SQL语句,从而使您的数据暴露于外界或使外界破坏您的数据。

也要注意与LIKE更新值匹配的语句。除非您对代码路径调入这一说法,在通过非常强的参数验证id="?"id="%%"或简单地id=string.Empty给你的函数可能会更新所有的记录,因此将覆盖所有的人都拥有相同的价值观。%1%会匹配1、10、21、31、11、100003 ...这不太可能实现所需的行为。

在delete和update语句中使用=overlike几乎总是所需的解决方案。如果您遇到错误,则=可能是混合数据类型(例如string,在哪里int)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我会从此代码中收到VBA运行时错误“ 424”对象必需错误?

我收到运行时错误NZEC请告诉我是什么问题

为什么即使我有#[tokio :: main],也会收到错误消息“没有反应堆在运行,必须从Tokio运行时的上下文中调用”?

我为什么会收到此运行时错误:类型为“ Solution :: node”的空指针内的成员访问(solution.cpp)

收到运行时错误1004,我不明白为什么

为什么我会收到此错误

我不明白为什么会收到以下错误

为什么我会收到价值错误

我的VBA代码有什么问题(我收到运行时错误9)

为什么我会收到JavaScript参考错误?

为什么我的代码出现运行时错误?

为什么会收到此运行时异常?

我无法确定为什么在通过io运行时收到java.lang.Nullpointerexception错误消息

JAVA:为什么我会不断收到这些运行时错误?

为什么我的代码给出运行时错误?

为什么我的签名 URL 会收到错误 403?

为什么我会收到 Unexpected Token ,错误?

为什么我会收到 fsck 错误?

分段错误 - 为什么我会收到此错误?

为什么我会收到安装 imposm 的错误?

为什么我在 Excel VB 中收到运行时错误 429?

为什么我会收到此错误:ActiveRecord::AssociationTypeMismatch?

为什么我在 Xcode 上收到以下代码的 (lldb) 运行时错误?

为什么我会收到主题行的分段错误

为什么我会收到“运行时错误 '13':类型不匹配”错误消息?

为什么我在安装 Negbio 后会收到 ModuleNotFoundError 错误?

为什么我会收到分段错误错误?

为什么我一运行我的应用程序就会在运行时收到空指针异常错误,它会崩溃?

为什么我会收到“圆形视图路径错误”?