Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,544 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,865 people online right now. Registration is fast and FREE... Join Now!




Using combo boxes to insert data in a database

 
Reply to this topicStart new topic

Using combo boxes to insert data in a database

sewiwi
20 Aug, 2008 - 02:26 AM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 3

I want to add data to a database using combo boxes but I keep receiving the syntax in insert into message. Please view my code and tell me where went wrong.
CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Call Validation()
            If bAllPassed = True Then
                Call InsertData()
                MsgBox("Information is saved", MsgBoxStyle.Information, "Save Information")
                Call LockControls()
            Else
                MsgBox("All the fields are compulsory therefore they should be filled", MsgBoxStyle.Information, "Field Validator")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub Validation()
        'Dim msg As String

        'Write sender
        If TextBox1.Text = " " Then
            bAllPassed = False
        Else
            empID = TextBox1.Text
        End If
        If TextBox2.Text = " " Then
            bAllPassed = False
        Else
            empPass = TextBox2.Text
        End If
        If ComboBox1.SelectedItem = "Select Role" Then
            bAllPassed = False
        Else
            empRole = ComboBox1.SelectedItem
        End If

        If TextBox6.Text = " " Then
            bAllPassed = False
        Else
            idNo = TextBox6.Text
        End If
        If TextBox3.Text = " " Then
            bAllPassed = False
        Else
            empName = TextBox3.Text
        End If
        If TextBox4.Text = " " Then
            bAllPassed = False
        Else
            empSurname = TextBox4.Text
        End If
        If ComboBox2.SelectedItem = "Select Job Title" Then
            bAllPassed = False
        Else
            jobTitle = ComboBox2.SelectedItem
        End If
        If ComboBox3.SelectedItem = "Selected Employee Level" Then
            bAllPassed = False
        Else
            empLevel = ComboBox3.SelectedItem
        End If
        If ComboBox4.SelectedItem = "Select Gender" Then
            bAllPassed = False
        Else
            gender = ComboBox4.SelectedItem
        End If
        If TextBox5.Text = " " Then
            bAllPassed = False
        Else
            If IsNumeric(TextBox5.Text) Then
                age = TextBox5.Text
            End If
        End If
        If ComboBox7.SelectedItem = "Select Employment Equit Status" Then
            bAllPassed = False
        Else
            ee = ComboBox7.SelectedItem
        End If
        If ComboBox5.SelectedItem = "Select Marital Status" Then
            bAllPassed = False
        Else
            maritalStatus = ComboBox5.SelectedItem
        End If
        If ComboBox6.SelectedItem = "Select Department Code" Then
            bAllPassed = False
        Else
            deptCode = ComboBox6.SelectedItem
        End If
    End Sub

    Private Sub InsertData()
        Dim comm As New OleDbCommand

        comm.CommandText = "INSERT INTO Employee & #40;Emp_ID,Password,Role,ID_Number,Emp_Name,Emp_Surname,Job_Title,Emp_Role,Gende
r,Age,EE,Marital_Status,Dept_Code)" & _
         "Values & #40;@Emp_ID,@Password,@Role,@ID_Number,@Emp_Name,@Emp_Surname,@Job_Title,@Emp_Ro
le,@Gender,@Age,@EE,@Marital_Status,@Dept_Code)"
        comm.Connection = oleconn
        comm.Parameters.Clear()
        comm.Parameters.Add("Emp_ID", empID)
        comm.Parameters.Add("Password", empPass)
        comm.Parameters.Add("Role", empRole)
        comm.Parameters.Add("ID_Number", idNo)
        comm.Parameters.Add("Emp_Name", empName)
        comm.Parameters.Add("Emp_Surname", empSurname)
        comm.Parameters.Add("Job_Title", jobTitle)
        comm.Parameters.Add("Emp_Role", empLevel)
        comm.Parameters.Add("Gender", gender)
        comm.Parameters.Add("Age", age)
        comm.Parameters.Add("EE", ee)
        comm.Parameters.Add("Marital_Status", maritalStatus)
        comm.Parameters.Add("Dept_Code", deptCode)


        If oleconn.State = ConnectionState.Closed Then
            oleconn.Open()
        End If

        comm.ExecuteNonQuery()
    End Sub

User is offlineProfile CardPM
+Quote Post

thava
RE: Using Combo Boxes To Insert Data In A Database
20 Aug, 2008 - 05:38 PM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Apr, 2007
Posts: 450



Thanked: 18 times
Dream Kudos: 50
My Contributions
i think this is in .net platform

i think the parameters you passed are not correct in datatype
User is offlineProfile CardPM
+Quote Post

sewiwi
RE: Using Combo Boxes To Insert Data In A Database
21 Aug, 2008 - 10:48 PM
Post #3

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 3

QUOTE(thava @ 20 Aug, 2008 - 06:38 PM) *

i think this is in .net platform

i think the parameters you passed are not correct in datatype


thanks for your time and effort in evaluating my code. i think the problem has to do with combo boxes not parameters because I used code similar to the one u saw, but on the form i used textboxes and it worked fine.
User is offlineProfile CardPM
+Quote Post

thava
RE: Using Combo Boxes To Insert Data In A Database
22 Aug, 2008 - 12:04 AM
Post #4

D.I.C Regular
Group Icon

Joined: 17 Apr, 2007
Posts: 450



Thanked: 18 times
Dream Kudos: 50
My Contributions
is that the data you enter the same data's in the text boxes i think not so

if it is not solve the problem
just tell me what are the data types of combo box used field
and what are the data types of the variables used to supply the parameters
i think this would solve this
User is offlineProfile CardPM
+Quote Post

sewiwi
RE: Using Combo Boxes To Insert Data In A Database
22 Aug, 2008 - 03:42 AM
Post #5

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 3

QUOTE(sewiwi @ 21 Aug, 2008 - 11:48 PM) *

QUOTE(thava @ 20 Aug, 2008 - 06:38 PM) *

i think this is in .net platform

i think the parameters you passed are not correct in datatype


thanks for your time and effort in evaluating my code. i think the problem has to do with combo boxes not parameters because I used code similar to the one u saw, but on the form i used textboxes and it worked fine.


the data types i use for the combo boxes are of type string except for the age variable, which is an integer. does this message help you. if not be more elaborate.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:51PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month