i have a vb.net form linked to database access-Table1
In the form three checkbox as HB,TC and 2 textbox-ID,Age
I am using following code but is its not working.

If Hbtxt.Checked Then
                cmd = New OleDbCommand("insert into Table1(ID,Age,Hb) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Hbtxt.Text & "')", con)
            Else
                cmd = New OleDbCommand("insert into Table1(ID,Age,Hb) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','')", con)
            End If

If TCtxt.Checked Then
                cmd = New OleDbCommand("insert into Table1(ID,Age,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & TCtxt.Text & "')", con)
            Else
                cmd = New OleDbCommand("insert into Table1(ID,Age,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','')", con)
            End If
            cmd.ExecuteNonQuery()

Unless cmd is declared outside the If-Then-Else it goes out of scope once you try to execute it. You could try the following

Dim query As String = ""

If Hbtxt.Checked Then
    query = "insert into Table1 (ID,Age,Hb) " &
            "values('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Hbtxt.Text & "')"
Else
    query = "insert into Table1 (ID,Age,Hb) " &
            "values('" & IDtxt.Text & "','" & Agetxt.Text & "','')"
End If

If TCtxt.Checked Then
    query = "insert into Table1 (ID,Age,TC) " &
            "values('" & IDtxt.Text & "','" & Agetxt.Text & "','" & TCtxt.Text & "')"
Else
    query = "insert into Table1 (ID,Age,TC) " &
            "values('" & IDtxt.Text & "','" & Agetxt.Text & "','')"
End If

cmd = New OleEDbCommand(query, con)
cmd.ExecuteNonQuery()

You didn't say how it isn't working. If you are getting an error message then please post it. I have a couple of other suggestions. Please add

Debug.WriteLine(query)

just before you do ExecuteNonQuery and post the results. Are TCtxt.Checked and Hbtxt.Checked mutually exclusive? If not then this code may not do what you want. Also, please see how to use parameterized queries.

I got this error msg
type OleDbCommand is not defined

Make sure you have the OleDb namespace!

Imports System.Data.OleDb

Plug that line in at the very top of your code above everything else (Including your class line!)

See picture link Here.
It sends you to bayimg.com a free anonymous uncensored picture uploader see info Here

I also read your one thread regarding Listview to database.
So if I have 4 parameters in Listview as Hb,TC,DC,MP and i want to connect it with respective column in database acces,then is it possible.
I tried to apply ur that code form listview to SQl server, but not to get anything.

can i apply above code to atleast 20 checkbox?

I've never had the need to use databound controls so I can't offer any help in that area. As for applying the code to 20 checkboxes, you'll want to have a separate block for each checkbox as in

Dim query As String
Dim cmd As New OleDbCommand("", con)

query = "INSERT INTO Table1 (ID,Age,Hb) VALUES('" & IDtxt.Text & "','" & Agetxt.Text &
    "','" & IIf(Hbtxt.Checked, Hbtxt.Text, "") & "')"
cmd.CommandText = query
cmd.ExecuteNonQuery()

query = "INSERT INTO Table1 (ID,Age,TC) VALUES('" & IDtxt.Text & "','" & Agetxt.Text &
     "','" & IIf(TCtxt.Checked, TCtxt.Text, "") & "')"
cmd.CommandText = query
cmd.ExecuteNonQuery()

etc.

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.