如果我的帖子不遵循編碼論壇的常見做法/規則,我深表歉意,因為這是我的第二個問題。
我有一個按鈕,一旦單擊,它會根據組合框中的選定值在 MS Access 資料庫中搜索專案,并將這些專案添加到資料網格中。它為每個值創建一個引數,并將相關的組合框項添加到該引數。
CreateSQLStr() 函式根據組合框的選定值形成 SQL 命令文本(即,如果組合框沒有選定值,則它不會為表中的該欄位指定條件,但如果有它會將 AND (TableField = @Parameter) 添加到 SQL 命令文本中)。
我嘗試將“ACTIVE”更改為預定義的字串和相同的錯誤,但我想不出其他任何東西。
我的錯誤是 `System.Data.OleDb.OleDbException: 'Parameter @Status has no default value。'
細節:
System.Data.OleDb.OleDbException
HResult=0x80040E10
Message=Parameter @Status has no default value.
Source=System.Data
StackTrace:
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteReader()
at WindowsApplication1.frmEmployee.vehiclesearch_Click(Object sender, EventArgs e) in C:\Users\aawad\OneDrive\Documents\QHP\QHP\QHP\frmEmployee.vb:line 79
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in :line 81
This exception was originally thrown at this call stack:
[External Code]
WindowsApplication1.frmEmployee.vehiclesearch_Click(Object, System.EventArgs) in frmEmployee.vb
[External Code]
`
Private Sub vehiclesearch_Click(sender As Object, e As EventArgs) Handles VehicleSearch.Click
If DbConnect() Then 'This fucntions returns boolean for whether or not database is connecyed
Dim SQLcmd As New OleDbCommand
With SQLcmd
.Connection = cn
.CommandText = CreateSQLStr() 'Creates command text based on selected combo box items, see below
.Parameters.AddWithValue("@Make", MakeCBox.SelectedItem)
.Parameters.AddWithValue("@Model", ModelCBox.SelectedItem)
.Parameters.AddWithValue("@ClassV", ClassCBox.SelectedItem)
.Parameters.AddWithValue("@VSeats", SeatsCBox.SelectedItem)
.Parameters.AddWithValue("@GrBox", GrboxCBox.SelectedItem)
.Parameters.AddWithValue("@Branch", BranchCBox.SelectedItem)
.Parameters.AddWithValue("@Status", "ACTIVE")
Dim rs As OleDbDataReader = .ExecuteReader 'Error occurs at this point
While rs.Read 'Adding data to data grid
Dim make, model, year, fuel, vclass, body As String
make = rs("VMake")
model = rs("VModel")
year = rs("VRegYear")
fuel = rs("VFuel")
vclass = rs("VClass")
body = rs("VBody")
VehDGrid.Rows.Add(make, model, year, fuel, vclass, body)
End While
rs.Close()
cn.Close()
End With
Else 'Error message when no vehicle found
MessageBox.Show("Could not find any vehicles with the selected details. ", "Find vehicle", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
Function CreateSQLStr() As String
Dim SQLStr As String
SQLStr = "Select * FROM Vehicles WHERE (VStatus = @Status)"
If MakeCBox.SelectedItem IsNot Nothing Then
SQLStr = SQLStr " AND (VMake = @Make)"
End If
If ModelCBox.SelectedItem IsNot Nothing Then
SQLStr = SQLStr & " AND (VModel = @Model)"
End If
If ClassCBox.SelectedItem IsNot Nothing Then
SQLStr = SQLStr & " AND (VClass = @ClassV)"
End If
If SeatsCBox.SelectedItem IsNot Nothing Then
SQLStr = SQLStr & " AND (VSeats = @Seats)"
End If
If GrboxCBox.SelectedItem IsNot Nothing Then
SQLStr = SQLStr & " AND (VGearbox = @GrBox)"
End If
If BranchCBox.SelectedItem IsNot Nothing Then
SQLStr = SQLStr & " AND (BranchID = @Branch)"
End If
Return SQLStr
End Function
編輯:回復評論
Dim rs As OleDbDataReader = SQLcmd.ExecuteReader
While rs.Read
VehDGrid.Rows.Add(rs("VehicleID"), rs("VMake"), rs("VModel"), rs("VRegYear"),
rs("VFuel"), rs("VClass"), rs("VBody"), rs("VSeats"),
rs("VGearbox"), rs("VReg"), rs("PPDay"), rs("VColour"))
End While
uj5u.com熱心網友回復:
對于 MS Access,引數的順序很重要,而不是名稱。?使用引數時,我在我的 SQL 命令中使用占位符。我還指定了資料型別,因此請考慮使用
So, in above, you can click on the "..." button, and that launches the connection builder. And once you save above, then the setting can be used from My.Settings."my setting name here".
So, for things like company name, perhaps a title, or whatever? You can build up and create your own settings. Once done, then these handy settings can be used in code.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426205.html
上一篇:如何用2個不同的鍵決議Json?
