我收到此运行时错误,但我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;
这是NumberStyles
和CultureInfo
定义。通常,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] 删除。
我来说两句