[英]Error converting data type nvarchar to numeric in query
I want to call the ID
of the row because I want to delete the row. 我想调用该行的ID
,因为我想删除该行。
Here is my code:- 这是我的代码:
private void DeleteAttachment(string ID)
{
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
connection.Open();
string sql = "DELETE FROM EMP_ATTACHED_DOCUMENTS WHERE mkey= @ID";
using (SqlCommand cmd = new SqlCommand(sql, connection))
{
cmd.Parameters.AddWithValue("@ID", ID);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
}
protected void GrdTraining_DeleteCommand(object sender, Obout.Grid.GridRecordEventArgs e)
{
if (Session["dt10"] != null)
{
dt10 = (DataTable)Session["dt10"];
}
else
{
BindDatatable();
}
DeleteAttachment(ID);
DataRow[] grdTrain = dt10.Select("SR_NO=" + Convert.ToString(e.Record["SR_NO"]));
dt10.Rows.Remove(grdTrain[0]);
AddToViewState("GrdTraining");
}
but I am unable to delete the row. 但我无法删除该行。 When I tried to debug it, at ID
, I was getting _Page
. 当我尝试以ID
调试它时,我正在获取_Page
。
The error message: 错误信息:
Error converting data type nvarchar to numeric 将数据类型nvarchar转换为数值时出错
Please, suggest a way to call a dynamic ID. 请提出一种调用动态ID的方法。
Based on your comments I think you need to do something like the following 根据您的评论,我认为您需要执行以下操作
private void DeleteAttachment(string ID)
{
decimal numericId;
if(!decimal.TryParse(ID, out numericId))
{
// The ID is not a valid decimal determine what to do in that case.
}
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
connection.Open();
string sql = "DELETE FROM EMP_ATTACHED_DOCUMENTS WHERE mkey= @ID";
using (SqlCommand cmd = new SqlCommand(sql, connection))
{
cmd.Parameters.Add("@ID", SqlDbType.Decimal).Value = numericId;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
}
Alternatively you could change the ID
argument to the DeleteAttachment
method to be a decimal
and do the parsing outside the method. 或者,您可以将DeleteAttachment
方法的ID
参数更改为decimal
然后在该方法之外进行解析。
within the DeleteAttachment I would simply put: 在DeleteAttachment中,我只需输入:
decimal temp;
if (!Decimal.TryParse(ID, out temp)) { return; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.