我正在VB.Net和SQL Server中创建LMS。书中添加表单包含文本框组合框和图片框。当我单击“保存”按钮时,它会显示以下错误消息
Conversion failed when converting the varchar value 'Saman' to data type int.
在表单加载上,组合框填充整个表中的数据。
表和关系
要插入的数据
数据类型为
的表格保存时出现错误信息
我尝试了以下编码
表单加载编码
Private Sub frmAddBook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'To load Author details
Using com As New SqlCommand("SELECT * FROM tblBookAuthor", con)
Dim dt As New DataTable()
dt.Load(com.ExecuteReader)
cmbAuthor.DataSource = dt
cmbAuthor.DisplayMember = "authorFirstName"
cmbAuthor.ValueMember = "authorFirstName"
' con.Close()
End Using
'To load Publisher details
Using com2 As New SqlCommand("SELECT * FROM tblPublisher", con)
Dim dt As New DataTable()
dt.Load(com2.ExecuteReader)
cmbPub.DataSource = dt
cmbPub.DisplayMember = "publisherName"
cmbPub.ValueMember = "publisherName"
' con.Close()
End Using
'To load Class details
Using com3 As New SqlCommand("SELECT * FROM tblBookClass", con)
Dim dt As New DataTable()
dt.Load(com3.ExecuteReader)
cmbClass.DataSource = dt
cmbClass.DisplayMember = "classDetail"
cmbClass.ValueMember = "classDetail"
' con.Close()
End Using
'To load bookshelf details
Using com4 As New SqlCommand("SELECT * FROM tblBookShelfDetail", con)
Dim dt As New DataTable()
dt.Load(com4.ExecuteReader)
cmbBookShelf.DataSource = dt
cmbBookShelf.DisplayMember = "shelfDetail"
cmbBookShelf.ValueMember = "shelfDetail"
con.Close()
End Using
End Sub
保存按钮编码
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
sql = "INSERT INTO tblBookDetail (accessionNo, authorId, title, pages, price, ISBN, noOfCopies, pubId, classId, source, bookShelfNo,bookEdition, bookCoverImg) VALUES (@accNo, @auID, @title, @pages, @price, @ISBN, @noOfCopies, @pubID, @classID, @src, @shlfNo, @edition, @img)"
Dim ms As New MemoryStream()
picPhoto.Image.Save(ms, picPhoto.Image.RawFormat)
con.Open()
cmd = New SqlCommand(sql, con)
cmd.Parameters.Add("@accNo", SqlDbType.Int).Value = txtBookID.Text
cmd.Parameters.Add("@auID", SqlDbType.VarChar).Value = cmbAuthor.Text
cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = txtEdition.Text
cmd.Parameters.Add("@pages", SqlDbType.Int).Value = txtPageNo.Text
cmd.Parameters.Add("@price", SqlDbType.Decimal).Value = txtPrice.Text
cmd.Parameters.Add("@ISBN", SqlDbType.Int).Value = txtISBN.Text
cmd.Parameters.Add("@noOfCopies", SqlDbType.Int).Value = txtNoOfCopies.Text
cmd.Parameters.Add("@pubID", SqlDbType.VarChar).Value = cmbPub.Text
cmd.Parameters.Add("@classID", SqlDbType.VarChar).Value = cmbClass.Text
cmd.Parameters.Add("@src", SqlDbType.VarChar).Value = cmbSrc.Text
cmd.Parameters.Add("@shlfNo", SqlDbType.VarChar).Value = cmbBookShelf.Text
cmd.Parameters.Add("@edition", SqlDbType.Int).Value = txtEdition.Text
cmd.Parameters.Add("@img", SqlDbType.Image).Value = ms.ToArray
cmd.ExecuteNonQuery()
MsgBox("SUCCESS")
con.Close()
End Sub
答案 0 :(得分:0)
程序错误:
将varchar值'Saman'转换为数据时转换失败 输入int。
在这里,你将列authorID作为数据类型整数,那你怎么能插入varchar,即将'Saman'命名为它!
触发此错误的代码行:
cmd.Parameters.Add("@auID", SqlDbType.VarChar).Value = cmbAuthor.Text
确保表格列不是整数或相应更改。
修改:
如果要插入id,则:
用组合框中的SelectedValue替换文本
希望你明白这一点。感谢