Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2006, 02:07 AM
Registered User
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't fill up list of Staff Name in the combobox


Hi to all,

I'm having the problem with the codings.
It's a window application connected to the SQL Server database. The database that I placed is found in the PC (Local)(Window NT).

How to make sure that the Staff Name field from the table in the database can be filled
up in the combobox when the form is loaded ? When I start debugging, the error shown as Incorrect syntax near 'char'.


My codings shown as follows:

Private Sub frmLeave_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim sqlcom As SqlCommand = New SqlCommand()
        Dim sqlReader As SqlDataReader


        sqlcon.ConnectionString = "Data Source=.;Initial Catalog=CRM;Integrated Security=True"

        'Open connection, execute the command
        sqlcon.Open()

        'Set the SqlCommand object properties
        sqlcom.Connection = sqlcon
        sqlcom.CommandText = "SELECT INTO Staff (Staff Name) VALUES(@Staff Name)"

        'Parameter for Staff Name ComboBox
        sqlcom.Parameters.Add("@Staff Name", SqlDbType.Char)
        sqlcom.Parameters(0).Value = cboStaffName.Text

        'Read records from database
        sqlReader = sqlcom.ExecuteReader() 'Error: Incorrect syntax near 'char'.


        'Close Reader
        sqlReader.Close()
        'Close connection
        sqlcon.Close()


    End Sub

Hope to receive reply soon.

Best Regards
Tracy
 
Old October 11th, 2006, 03:23 PM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to notrosh
Default

Tracy,

Below you will find some code to populate a combobox. I choose to use a Dataset instead of using Parameters.

--------------Code Below-------------------


----Declarations------------

 'Connection String
    Public connectionstring As String = "Data Source=FA2-0282\SQLEXPRESS;Initial Catalog=ComboBox;Integrated Security=True;Pooling=False"

----FormLoad----------------

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        populateStaffComboBox()
    End Sub

----Seperate Subroutine-----

 Public Sub populateStaffComboBox()
        'Declared new SqlConnection and set it too
        'public connection string which
        'I declared above
        Dim conn As New SqlConnection(connectionstring)

        'Open connection
        conn.Open()

        'Connection String
        Dim sql As String = "select staff_first_name from staff"

        'Declare DataAdapter
        Dim StaffAdapter As New SqlDataAdapter

        'Declare SQL command and set it too sql string
        '(sql) and connection (conn)
        Dim cmd As New SqlCommand(sql, conn)

        'Delcare Dataset and DataView --- notice
        'i declared both of them as New.
        Dim ds As New DataSet
        Dim dv As New DataView

        'Here I am setting my SqlCommand
        '-- cmd -- Command type to a text value
        'So that it will be accepted by the Adapter
        cmd.CommandType = CommandType.Text

        'Here I am declaring the select command for my Adapter
        StaffAdapter.SelectCommand = cmd

        'Try Catch Block for error checking
        Try
            'I fill my dataset using the Fill method
            StaffAdapter.Fill(ds)

            'Setting my Dataview equal to my dataset
            'basically the default table
            dv = ds.Tables(0).DefaultView

            'I am populating my combo box here
            With Me.cboTest

                'I set the datasource of the combobox
                'to my dataview
                .DataSource = dv

                'Here i tell the combobox what to display
                .DisplayMember = "Staff_First_Name"
            End With
        Catch ex As Exception

            'In case I have an errors --- ;)
            MessageBox.Show(ex.Message.ToString)

        End Try

        'Close my connection
        conn.Close()

    End Sub


-----------End Code----------------

Everyone have a great day...

notrosh

www.nwadnug.org
 
Old October 12th, 2006, 12:12 AM
Registered User
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply, notrosh.

Best Regards
Tracy

 
Old October 12th, 2006, 09:06 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to notrosh
Default

No problem. I hope it helped.

notrosh

www.nwadnug.org





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox List Fill Range jani Access VBA 1 July 29th, 2008 05:04 PM
how to fill the combobox with data from different alfiecrosby General .NET 0 May 14th, 2008 10:54 AM
Fill DataGrid based on the selection in combobox drani C# 12 October 11th, 2007 05:41 PM
How to fill a combobox with the items from a query MacDevv C# 1 August 21st, 2006 07:59 AM
fill dropdown list with items when parent list isaac_cm Pro PHP 1 July 10th, 2006 05:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.