c# asp.net 输入字符串格式不正确

来源:百度知道 编辑:UC知道 时间:2024/06/28 15:16:02
public SqlDataReader GetCategoriesInDepartment(string departmentId)//用于返回在指定departmentId下的category的列表
{
SqlConnection connection=new SqlConnection(connectionString);
SqlCommand command=new SqlCommand("GetCategoriesInDepartment",connection);
command.CommandType=CommandType.StoredProcedure;
command.Parameters.Add("@DepartmentID",SqlDbType.Int,4);
command.Parameters["@DepartmentID"].Value=Convert.ToInt32(departmentId);
connection.Open();
return command.ExecuteReader(CommandBehavior.CloseConnection);

}

其中存储过程GetCategoriesInDepartment的输入参数DepartmentID是int类型,错误提示是指向这句command.Parameters["@DepartmentID"].Value=Convert.ToInt32(departmentId);输入字符串格式不正确,为什么呢?不是已经转成int了么?函数输入参数departmentId在主调用函数已经判断过非空了啊

Convert.ToInt32(departmentId);中参数“departmentId”值有非数字字符所以转换int类型失败。 设置断点跟踪一下 看看传过来的“departmentId”值是不是都是数字就知道了。

你的 @DepartmentID 是输出参数是吗,你没有设置它的direction为output

光执行这个存储过程有没有什么问题啊?

格式不正确,不是数据类型不正确