Populate combobox using linq to sql selected items

florble

New member
Joined
Feb 12, 2010
Messages
1
Programming Experience
Beginner
Hi,

New to vb so please go easy on me :)

Im trying to populate the combobox with items that are returned from a search of the database with linq from the combobox when the text changes(in the combo box)

here is some of my code:

Dim db As New ProdLinqDataContext

Dim searchstring = ComboBox1.Text
If searchstring.Length > 2 Then
Dim prods = From p In db.Products _
Where p.SKU.Contains(ComboBox1.Text) _
Select p _
Take 25

ComboBox1.AutoCompleteSource = prods

'ComboBox1.Items.Add(prods)
Else
'Not sufficiently filtered. Keep the suggestion list blank.
'searchstring.AutoCompleteSource = New String() {}
End If
End Sub


I have tried loads of ways to do this and i get the same error:

System.InvalidCastException was unhandled
Message="Conversion from type 'DataQuery(Of String)' to type 'Integer' is not valid."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value)
at XMLTools.EditProduct.ComboBox1_TextChanged(Object sender, EventArgs e) in C:\Users\Will\Desktop\Product Upload VB Files\XMLTools\Settings\Products\EditProduct.vb:line 73
at System.Windows.Forms.Control.OnTextChanged(EventArgs e)
at System.Windows.Forms.ComboBox.WndProc(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.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(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.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.ComboBox.DefChildWndProc(Message& m)
at System.Windows.Forms.ComboBox.ChildWndProc(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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at XMLTools.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


Many thanks in advance for any help or comments!

will
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,327
Location
Norway
Programming Experience
10+
TextBox.AutoCompleteSource Property (System.Windows.Forms) the value option you choose here is AutoCompleteSource.CustomSource. Then you can set TextBox.AutoCompleteCustomSource Property (System.Windows.Forms), create a new AutoCompleteStringCollection and AddRange the string array containing your autocomplete strings. Also set TextBox.AutoCompleteMode Property (System.Windows.Forms), it defaults to value AutoCompleteMode.None.

I'm not sure how type 'DataQuery(Of String)' comes into play here, isn't that from some DLINQ preview? As I see it "From p In db.Products" should return IQueryable(Of Product), but it would be natural to for example to '... Select p.Name' to return results as IQueryable(Of String) then do ToArray to get a String array.
 
Top Bottom