Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Go Back  Xtreme Visual Basic Talk > > > Rookie:Populate DatagridView from tables listed in combobox


Reply
 
Thread Tools Display Modes
  #1  
Old 01-18-2012, 10:07 AM
saberx saberx is offline
Newcomer
 
Join Date: Jan 2012
Posts: 2
Default Rookie:Populate DatagridView from tables listed in combobox


Hello. I`m new to this.I`m working in vb net 2008, x86, on Windows 7.
So i have a little project, a SQL CE database with the connection:
Code:
Sub setSql(ByVal SQL As String)
        Dim con As New SqlCeConnection
        Dim cmd As New SqlCeCommand
        con.ConnectionString = "datasource= d:\consum.sdf"
        cmd.Connection = con
        cmd.CommandText = SQL
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Sub
    Sub getSql(ByVal SQL As String)
        Dim con As New SqlCeConnection
        Dim cmd As New SqlCeCommand
        con.ConnectionString = "datasource= d:\consum.sdf"
        cmd.Connection = con
        cmd.CommandText = SQL
        Dim da As New SqlCeDataAdapter(cmd)
        Dim dt As New DataTable
        con.Open()
        da.Fill(dt)
        DataGridView1.DataSource = dt
        con.Close()
So i have this databases with more tables in it.
Say database name is CONSUM with tables dcv1 , dcv2, stress, det .
I have also a form with a combobox in it, some textboxes (for inserting records in database) , a DataGridView.
What i want to do is:
To select a TABLE using the comboox, and to populate into the DatagridView the table that is selected in combobox.
I`m using this code to populate the Combobox with the table names:
Code:
Sub cbobox(ByVal sql As String)

        Dim con As New SqlCeConnection
        Dim cmd As New SqlCeCommand
        con.ConnectionString = "datasource= d:\consum.sdf"
        cmd.Connection = con

        cmd.CommandText = sql
        Dim da As New SqlCeDataAdapter(cmd)
        Dim dt As New DataTable
        con.Open()
        da.Fill(dt)
        With Me.ComboBox1
            .DataSource = dt
            .DisplayMember = "TABLE_NAME"
            .ValueMember = "TABLE_NAME"
            .SelectedIndex = 0
        End With
        con.Close()

    End Sub
Private Sub masini_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbobox("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES")
End Sub
My combobox is populated after with the names of the table.
What i CANT accomplish is this:
- i`m trying to populate the datagridView dynamicaly with the records from the Combobox Selected table ( ex: Combobox1 has the table on load dcv1)
and i tried this code:
Code:
Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
        getSql("select * from '" & ComboBox1.SelectedValue & "'")
    End Sub
i tried also getSql("select * from '" & ComboBox1.Text & "'")
but its just keeps showing a SQLCE wxception:
Quote:
There was an error parsing the query. [ Token line number = 1,Token line offset = 17,Token in error = dcv1 ]
Any help would be appreciated, thank you.
Michael
PS:sry about my english
Reply With Quote
  #2  
Old 01-18-2012, 10:15 AM
DrPunk's Avatar
DrPunkRookie:Populate DatagridView from tables listed in combobox DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

SelectedText should do it.

But table names don't need inverted commas around them.

You're trying to run..
Code:
SELECT * FROM 'thistable'
... whereas it should be...
Code:
SELECT * FROM thistable
... so all you should need to do is take out the inverted commas.

Square brackets are allowed and are used if the table name or the field name contains spaces, or if the table name or field name is a reserved word. So...
Code:
SELECT * FROM [thistable]
... would be valid, and maybe more applicable in your case where you don't know what the table name might be (i.e. it could have spaces in it, or be a reserved word).
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 01-18-2012, 10:48 AM
saberx saberx is offline
Newcomer
 
Join Date: Jan 2012
Posts: 2
Default

I cant believe it was THAT simple. It worked like a charm.
I searched over the internet but i still couldn`t figured it out.
Thank you thank you.
Best wishes, Michael.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
 
Rookie:Populate DatagridView from tables listed in combobox
Rookie:Populate DatagridView from tables listed in combobox
 
-->