Issue With DataAdapter.Fill

Jul 26 2010 1:27 PM

I have the following code which works in another part of my application.
 

MainListView.Clear();
//Clear List View.
MainDataSet =
new DataSet(); //Instansiate Data Set.
if (CBSearchBy.Text == "Registration")
{
sql =
"SELECT * From MainTable WHERE VehicleReg = " + TBValue.Text; //Create Query.
}
else if (CBSearchBy.Text == "Date Taken")
{
sql =
"SELECT * From MainTable WHERE HandlerDate = " + TBValue.Text; //Create Query.
}
MainDataAdapter =
new System.Data.OleDb.OleDbDataAdapter(sql, MainMenuInst.con); //Instantiate Data Adapter.
OleDbCommandBuilder cb = new OleDbCommandBuilder(MainDataAdapter); //Open Connection.
MainDataAdapter.Fill(MainDataSet,
"SearchResults"); //Fill Data Set.
 

However, I am using it on another form and I get an exception on the last line which reads below:
 

System.Data.OleDb.OleDbException was unhandled
  Message="No value given for one or more required parameters."
  Source="Microsoft JET Database Engine"
  ErrorCode=-2147217904
  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.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
       at EDoc.FrmSearch.BtnSearch_Click(Object sender, EventArgs e) in C:\Users\Matt\Documents\EDoc\EDoc\FindForm.cs:line 85
       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(Int32 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 System.Windows.Forms.Application.Run(Form mainForm)
       at EDoc.Program.Main() in C:\Users\Matt\Documents\EDoc\EDoc\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
 

Can anyone advise where I may be going wrong?
Many Thanks

Answers (1)