Insert multiple selected CheckBox text in Database using C# and VB.Net in ASP.Net

Last Reply 28 days ago By pandeyism

Posted 28 days ago

Hello,

This is my table:

CREATE TABLE jobTitle (
    PersonID int,
    Jobtitle1 varchar(255),
    Jobtitle2 varchar(255),
    Jobtitle3 varchar(255),
    Jobtitle4 varchar(255),
    Jobtitle5 varchar(255)
);

I try to apply the codes from a demo.

Insert checkbox text to SQL

I got SQL message: System.Data.SqlClient.SqlException: 'Column name or number of supplied values does not match table definition.'

Can you help me? Thank you.

Posted 28 days ago

Hi KatieNgoc,

Refer below sample.

HTML

<asp:CheckBox ID="CheckBox1" runat="server" Text="Jobtitle1" />
<br />
<asp:CheckBox ID="CheckBox2" runat="server" Text="Jobtitle2" />
<br />
<asp:CheckBox ID="CheckBox4" runat="server" Text="Jobtitle3" />
<br />
<asp:CheckBox ID="CheckBox3" runat="server" Text="Jobtitle4" />
<br />
<asp:CheckBox ID="CheckBox5" runat="server" Text="Jobtitle5" />
<br />
<asp:Button Text="Insert" runat="server" OnClick="Save" />

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using System.Data;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Save(object sender, EventArgs e)
{
    SqlConnection con = null;
    SqlCommand cmd = null;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    con = new SqlConnection(constr);
    cmd = new SqlCommand("INSERT INTO jobTitle(Jobtitle1,Jobtitle2,Jobtitle3,Jobtitle4,Jobtitle5) VALUES(@Jobtitle1,@Jobtitle2,@Jobtitle3,@Jobtitle4,@Jobtitle5)", con);
    cmd.Parameters.AddWithValue("@Jobtitle1", CheckBox1.Checked ? CheckBox1.Text : " ");
    cmd.Parameters.AddWithValue("@Jobtitle2", CheckBox2.Checked ? CheckBox2.Text : " ");
    cmd.Parameters.AddWithValue("@Jobtitle3", CheckBox3.Checked ? CheckBox3.Text : " ");
    cmd.Parameters.AddWithValue("@Jobtitle4", CheckBox4.Checked ? CheckBox4.Text : " ");
    cmd.Parameters.AddWithValue("@Jobtitle5", CheckBox5.Checked ? CheckBox5.Text : " ");
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();   
}

VB.Net

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim con As SqlConnection = Nothing
    Dim cmd As SqlCommand = Nothing
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    con = New SqlConnection(constr)
    cmd = New SqlCommand("INSERT INTO jobTitle(Jobtitle1,Jobtitle2,Jobtitle3,Jobtitle4,Jobtitle5) VALUES(@Jobtitle1,@Jobtitle2,@Jobtitle3,@Jobtitle4,@Jobtitle5)", con)
    cmd.Parameters.AddWithValue("@Jobtitle1", If(CheckBox1.Checked, CheckBox1.Text, " "))
    cmd.Parameters.AddWithValue("@Jobtitle2", If(CheckBox2.Checked, CheckBox2.Text, " "))
    cmd.Parameters.AddWithValue("@Jobtitle3", If(CheckBox3.Checked, CheckBox3.Text, " "))
    cmd.Parameters.AddWithValue("@Jobtitle4", If(CheckBox4.Checked, CheckBox4.Text, " "))
    cmd.Parameters.AddWithValue("@Jobtitle5", If(CheckBox5.Checked, CheckBox5.Text, " "))
    con.Open()
    cmd.ExecuteNonQuery()
    con.Close()   
End Sub