不能将类型'int'隐式转换为'string'? c#4

不能将类型'int'隐式转换为'string'? c#4

问题描述:

在运行下面给出的代码,得到一个错误。我必须通过gridview执行删除操作。

while running the given below code, getting an error. I have to perform delete operation through gridview.

代码:

protected void gvEdit_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string Emp_ID = gvEdit.DataKeys[e.RowIndex].Value.ToString();

        int EId = Convert.ToInt32(Emp_ID); // error popup here
        ShadingAnalysisDataSetTableAdapters.tbl_EmployeeToTeamTableAdapter tm;
        tm = new ShadingAnalysisDataSetTableAdapters.tbl_EmployeeToTeamTableAdapter();
        DataTable dt = new DataTable();
        tm.GetDelete(EId);
        BindData();
    }

DB:

SQL: p>

SQL:

DELETE FROM tbl_EmployeeToTeam WHERE (Emp_ID = @Emp_ID)


我假设 GetDelete 期望一个字符串,但你传递一个 int 。我认为是因为它似乎是 Emp_ID 的参数,它是一个 varchar 。所以你可以简单地传递你已经拥有的字符串

I assume that GetDelete expects a string but you're passing an int. I think so because it seems to be the parameter for Emp_ID which is a varchar. So you could simply pass that string which you already had in:

gvEdit.DataKeys[e.RowIndex].Value.ToString();

但是,如果它应该是一个 int ,为什么不在数据库中使用正确的数据类型?

But if it's supposed to be an int, why don't you use the correct data type in the database?

不良习惯踢:选择错误的数据类型

更新


我的员工ID正在从EMP001,EMP002。 。等等。那么如何使用
整数数据类型

My employee Id is strating from EMP001, EMP002..etc. So how can I use integer data type

为什么你总是保存 EMP 在开始?您也可以省略并使用 int 作为更有效和故障安全的类型。那么你可以在 之前添加 EMP

Why do you store always EMP at the beginning? You could also omit that and use int as type which would be more efficient and fail-safe. Then you could prepend EMP where you need to display it.

但是,如果您不想更改,而$ Emp_ID 只包含ID,而不包含 EMP 可以使用字符串连接: p>

However, if you don't want to change that and Emp_ID contains only the ID without EMP you can use string concatenation:

tm.GetDelete("EMP" + Emp_ID);