Hi guys,i was having this problem with inserting a record into access database in visualstudio 2010.
here's the problem,here's the code

bridgenumber = TextBox1.Text.ToString()
            bridgename = TextBox2.Text
            districtname = ComboBox1.Text
            sectionname = ComboBox2.Text
            roadsegmentname = ComboBox3.Text
            bridgetype = ComboBox4.Text
            bridgespan1 = TextBox3.Text
            bridgecondition = ComboBox5.Text
            acquisitionyear1 = ComboBox6.Text
            evaluationyear1 = ComboBox7.Text
            endofdesign1 = ComboBox8.Text
            bridgeasset1 = Label12.Text
            Dim str As String
            Dim conn As OleDbConnection = GetDbConnection()
            Dim cmd As OleDbCommand
            str = "INSERT INTO AdigratT (Bridge_Number,Bridge_Name,District_Name,Section_Name,Road_Segment_Name,Bridge_Type,Bridge_Span,Bridge_Condition,Acquisition_Year,Evaluation_Year,End_of_Design_Life_Year,Bridge_Asset_Value) VALUES (" &
                "'" & bridgenumber & "','" & bridgename & "','" & districtname & "','" & sectionname & "','" & _
                    roadsegmentname & "','" & bridgetype & "','" & bridgespan1 & "','" & bridgecondition & "','" & acquisitionyear1 & "','" & _
                    evaluationyear1 & "','" & endofdesign1 & "','" & bridgeasset1 & "')"
            'and CInt is used to convert number, to string
            cmd = New OleDbCommand(str, conn)
            Dim obj As Object = cmd.ExecuteNonQuery()
            If Not (obj Is Nothing) Then
                MsgBox("Record has been inserted sucessfully", vbInformation, "Bridge Asset Management")
            End If
            conn.Close()
            conn.Dispose()
            conn = Nothing
        End If
        filllistview()

When i omitted the fields(Bridge_Number,Bridge_Name...)it works perfect but when i tried to insert the fields an exption is thrown.
"The INSERT INTO statement contains the following unknown field name: 'Bridge_Number'. Make sure you have typed the name correctly, and try the operation again."
Do i just simply not write the field names?

Recommended Answers

All 3 Replies

Sorry for the Discussion Title
I cant insert a record to a database

Do you actually have a Bridge_Number Field in your Table?
If you are using An Access DataBase I would first get the Query string into the "SQL View" of the Query Design window in MS-Access and test it. Also make sure you have spelled the Field names correctly. They have to be EXACT . You also have to have them in the correct order. You will need to make sure you are using the correct Data Type for each one.

i came up with the code kRod,thank you for your support

str = "INSERT INTO AdigratT ([Bridge Number],[Bridge Name],[District Name],[Section Name],[Road Segment Name],[Bridge Type],[Bridge Span],[Bridge Condition],[Acquisition Year],[Evaluation Year],[End of Design Life Year],[Bridge Asset Value]) VALUES (" &
                    "'" & bridgenumber & "','" & bridgename & "','" & districtname & "','" & sectionname & "','" & _
                        roadsegmentname & "','" & bridgetype & "','" & bridgespan1 & "','" & bridgecondition & "','" & acquisitionyear1 & "','" & _
                        evaluationyear1 & "','" & endofdesign1 & "','" & bridgeasset1 & "')"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.