“将nvarchar值'department_id'转换为数据类型int时转换失败。” [英] "Conversion failed when converting the nvarchar value 'department_id' to data type int."

查看:74
本文介绍了“将nvarchar值'department_id'转换为数据类型int时转换失败。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim da As New SqlDataAdapter("SELECT Subjects.subject_name FROM Subjects " _
     & "INNER JOIN Departments_Subjects_junction ON Subjects.subject_id = Departments_Subjects_junction.subject_id " _
     & "INNER JOIN Semesters_Subjects_junction ON Subjects.subject_id = Semesters_Subjects_junction.subject_id " _
     & "WHERE Departments_Subjects_junction.department_id = @DepartmentID AND Semesters_Subjects_junction.semester_id = @SemesterID ", con)
       da.SelectCommand.Parameters.AddWithValue("@DepartmentID", departmentCb.ValueMember)
       da.SelectCommand.Parameters.AddWithValue("@SemesterID", semesterCb.ValueMember)
       da.Fill(dt)
       con.Close()
       If dt.Rows.Count = 0 Then
           MessageBox.Show("No record exists, try again", "SORRY!", MessageBoxButtons.OK)
       End If

推荐答案

ValueMember [ ^ ](对于组合框)返回提供值的属性的名称 - 而不是实际值本身。



我怀疑你想要有departmentCB。 SelectedValue [ ^ ]?
"ValueMember"[^] (for a combo box) returns the name of the property that provides the value - not the actual value itself.

I suspect that you meant to have departmentCB.SelectedValue [^]?


难怪;没有什么可以保证你的一些随机字符串值总是保持整数值的正确表示。

你的数据库模式中的整个想法是错误的。您需要在 department_id 中创建一个字符串,该字符串在所需范围内唯一标识您的部门,而不是将其限制为整数,或使用表示数值的正确数据类型。无论如何,你永远不应该使用需要数字数据的字符串类型;它既低效又不可靠。



例如,参见 https://msdn.microsoft.com/en-us/library/ms187745.aspx [ ^ ]。



-SA
No wonder; nothing guarantees you that some random string value always holds correct representation of an integer value.
The whole idea put in your database schema is wrong. You either need to make your department_id a string uniquely identifying your department in required scope, not limiting it to integers, or use proper data type representing a numeric value. Anyway, you should never use string types where numeric data is required; it's both inefficient and unreliable.

See, for example, https://msdn.microsoft.com/en-us/library/ms187745.aspx[^].

—SA


da.SelectCommand.Parameters.AddWithValue("@DepartmentID", departmentCb.SelectedValue)
       da.SelectCommand.Parameters.AddWithValue("@SemesterID", semesterCb.SelectedValue)


这篇关于“将nvarchar值'department_id'转换为数据类型int时转换失败。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆