简体   繁体   English

在查询中将数据类型nvarchar转换为数值时出错

[英]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.

相关问题 “将数据类型 nvarchar 转换为数字时出错。” - “Error converting data type nvarchar to numeric.” 获取错误:“在SQL中将数据类型nvarchar转换为数字时出错” - Getting Error: “Error converting data type nvarchar to numeric” in SQL CSharp ado.net将数据类型nvarchar转换为数值时出错 - CSharp ado.net Error converting data type nvarchar to numeric 错误:System.Data.SqlClient.SqlException(0x80131904):将数据类型nvarchar转换为数值时出错 - Error:System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to numeric 错误System.Data.SqlClient.SqlException:'将数据类型nvarchar转换为数字时出错。 在c#中 - Error System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in c# 当我的表上没有任何nvarchar时,如何解决“将nvarchar数据类型转换为数值错误”的问题? - How can i solve “error converting data type nvarchar to numeric” when i don't have any nvarchar on my table? 将Excel导入Sql Server数据库时将数据类型nvarchar转换为数值时出错 - Error converting data type nvarchar to numeric when Import Excel to Sql Server Database ASP.NET SQL插入错误将数据类型nvarchar转换为数值 - ASP.NET Sql insert Error converting data type nvarchar to numeric 将数据类型NVarchar转换为int时出错 - Error converting data type NVarchar into int 将nvarchar转换为数据类型int时出错 - Error converting nvarchar to data type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM