Results 1 to 2 of 2

Thread: Populate combobox using linq to sql selected items

  1. #1
    florble is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Feb 2010
    Posts
    1
    Reputation
    0

    Angry Populate combobox using linq to sql selected items

    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:li ne 73
    at System.Windows.Forms.Control.OnTextChanged(EventAr gs e)
    at System.Windows.Forms.ComboBox.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.SendMessa ge(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.ReflectMessageInterna l(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.W ndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
    at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& 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.W ndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
    at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& m)
    at System.Windows.Forms.ComboBox.DefChildWndProc(Mess age& m)
    at System.Windows.Forms.ComboBox.ChildWndProc(Message & m)
    at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
    at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.OnRun()
    at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.DoApplicationModel()
    at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.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.Create Instance(ActivationContext activationContext, String[] activationCustomData)
    at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssemblyDebugInZone()
    at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:


    Many thanks in advance for any help or comments!

    will

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,209
    Reputation
    2369
    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking