我该如何修复tbese错误? [英] How do I fixed tbese error?
本文介绍了我该如何修复tbese错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误:
Error:
"There is already an open DataReader associated with this Command which must be closed first"
如果数据库中已存在菜单名称,则显示错误信息,否则插入菜单名称。
我尝试了什么:
If the menu name is already exists in the database, display an error message else insert menu name.
What I have tried:
Public Sub addMenu()
Try
dbConnection()
search_query = "SELECT * FROM tblfood_menu WHERE menu_name = @menu_name;"
command = New SqlCommand
With command
.Connection = connection
.CommandText = search_query
.Parameters.Clear()
.Parameters.Add(New SqlParameter With {.ParameterName = "@menu_name", .SqlDbType = SqlDbType.VarChar, .Value = formFoodMenu.txtMenuName.Text})
dataReader = command.ExecuteReader()
If dataReader.HasRows Then
MsgBox("Menu name is already exists!", MsgBoxStyle.Exclamation, "Add Menu")
Else
insert_query = "INSERT INTO tblfood_menu(menu_name) VALUES(@menu_name);"
command = New SqlCommand
With command
.Connection = connection
.CommandText = insert_query
result = .ExecuteNonQuery()
If result = 0 Then
MsgBox("Error in adding menu!", MsgBoxStyle.Exclamation)
Else
MsgBox("Successfully added menu!", MsgBoxStyle.Information)
End If
End With
End If
End With
Catch ex As SqlException
MsgBox("Error: " + ex.Message)
Finally
connection.Close()
command.Dispose()
End Try
End Sub
推荐答案
With command
.Connection = connection
.CommandText = search_query
.Parameters.Clear()
.Parameters.Add(New SqlParameter With {.ParameterName = "@menu_name", .SqlDbType = SqlDbType.VarChar, .Value = formFoodMenu.txtMenuName.Text})
result = .ExecuteScalar()
If result > 0 Then
MsgBox("Menu name is already exists!", MsgBoxStyle.Exclamation, "Add Menu")
Else
insert_query = "INSERT INTO tblfood_menu(menu_name) VALUES(@menu_name);"
command = New SqlCommand
With command
.Connection = connection
.CommandText = insert_query
.Parameters.Clear()
.Parameters.Add(New SqlParameter With {.ParameterName = "@menu_name", .SqlDbType = SqlDbType.VarChar, .Value = formFoodMenu.txtMenuName.Text})
result = .ExecuteNonQuery()
If result = 0 Then
MsgBox("Error in adding menu!", MsgBoxStyle.Exclamation)
Else
MsgBox("Successfully added menu!", MsgBoxStyle.Information)
End If
End With
End If
End With
这篇关于我该如何修复tbese错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文