how to insert the values of checkbox and radiobuttons in the sql databse using vb.net???

we are getting problem in inserting and don't know what to do................

like i have one form with two text boxes named username and password and one label say gender....which has two checkboxes male and female and another label with two radiobuttons....yes and no....
on the submit button i want to insert the values into database.....made in
SQL server management studio 2008 and em using vb.net 2008

Imports System.Data.SqlClient
Public Class Form1
    Dim con As New SqlConnection
    Dim com As New SqlCommand
    Dim d As New SqlDataAdapter
    Dim da As Integer
    Dim rbtn As String
    Dim a, b As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New SqlConnection("server=abc-pc\sqlexpress;database=abc;trusted_connection=yes")
        con.Open()
        com = New SqlCommand("INSERT INTO user (username,password,bonus,others) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + a + "','" + b + "')", con)
com.ExecuteNonQuery()
        MessageBox.Show("saved",da)
        con.Close()
    End Sub

em getting error at com.executenonquery

Dani AI

Generated

A couple of things are likely causing your ExecuteNonQuery error and the wrong values to be saved. First, your table name is user, which is a T-SQL reserved word. That will throw an error like "Incorrect syntax near 'user'." Either rename the table (recommended) or bracket it: [user]. Second, build the INSERT with parameters, not string concatenation. It prevents SQL injection, avoids broken SQL when values contain quotes, and lets you pass proper types (e.g., a BIT for Yes/No). Also, do not store raw passwords; at minimum hash+salt before saving.

Here is a minimal, safe pattern you can drop in. It brackets the reserved names, uses parameters, and maps your controls to stable values (gender to M/F, decision to BIT):

Imports System.Data.SqlClient

Using con As New SqlConnection("Data Source=abc-pc\SQLEXPRESS;Initial Catalog=abc;Integrated Security=True")
    Dim sql = "INSERT INTO [user] ([username],[password],[bonus],[others]) VALUES (@u,@p,@gender,@yesno);"
    Using cmd As New SqlCommand(sql, con)
        cmd.Parameters.Add("@u", SqlDbType.NVarChar, 50).Value = TextBox1.Text.Trim()
        cmd.Parameters.Add("@p", SqlDbType.NVarChar, 255).Value = TextBox2.Text  ' TODO: replace with a hashed value
        Dim gender As Object = If(chkMale.Checked Xor chkFemale.Checked, If(chkMale.Checked, "M", "F"), CType(DBNull.Value, Object))
        cmd.Parameters.Add("@gender", SqlDbType.NVarChar, 1).Value = gender
        cmd.Parameters.Add("@yesno", SqlDbType.Bit).Value = rdYes.Checked
        con.Open()
        Dim rows = cmd.ExecuteNonQuery()
        MessageBox.Show("Saved " & rows & " row(s).")
    End Using
End Using

Notes tying back to the thread: showed how to read the control text; the missing piece is parameterization and ensuring mutually exclusive choices. For gender, two CheckBoxes allow both or neither; better use two RadioButtons in the same container (WinForms) or the same GroupName (ASP.NET). Align column types with what you store (e.g., BIT for Yes/No, CHAR(1) for gender), and prefer table/column names like dbo.Users(UserName, PasswordHash, Gender, OptIn) to avoid reserved words entirely.

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim Gender As String = Nothing
        Dim Decision As String = Nothing
        If chkMale.Checked Then
            Gender = chkMale.Text.ToString
        ElseIf chkFemale.Checked Then
            Gender = chkFemale.Text.ToString
        End If
        If rdYes.Checked Then
            Decision = rdYes.Text.ToString
        Else
            Decision = rdNo.Text.ToString
        End If
    End Sub

Use, like the Above coding and Store the Resultant String Values in the Database.

Imports System.Data.SqlClient
Public Class Form1
    Dim con As New SqlConnection
    Dim com As New SqlCommand
    Dim d As New SqlDataAdapter
    Dim da As Integer
    Dim rbtn As String
    Dim a, b As String
    If chkMale.Checked Then
        a = chkMale.Text.ToString
    ElseIf chkFemale.Checked Then
        a = chkFemale.Text.ToString
    End If
    If rdYes.Checked Then
        b = rdYes.Text.ToString
    Else
        b = rdNo.Text.ToString
    End If
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New SqlConnection("server=abc-pc\sqlexpress;database=abc;trusted_connection=yes")
        con.Open()
        com = New SqlCommand("INSERT INTO user (username,password,bonus,others) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + b + "','" + a + "')", con)
com.ExecuteNonQuery()
        MessageBox.Show("saved",da)
        con.Close()
    End Sub

Use, the Above coding for storing into Database.

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.