How to insert multiple values selected in checkbox in database.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Chandu KumawatRank #1,449 · 1.1K reputationPosted Feb 17, 2016, 1:20 AM EST
Amit Kumar SinghRank #562 · 4.3K reputationPosted Feb 17, 2016, 1:26 AM EST
Krishna Rajput SinghRank #605 · 4K reputationPosted Feb 17, 2016, 2:07 AM EST
Amit Kumar SinghRank #562 · 4.3K reputationPosted Feb 17, 2016, 2:41 AM EST
(
HobbyId INT identity1,1) NOT NULL Primary Key,
Hobby VARCHAR(100),
)
Hobbies:
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class ASPNET_MultipleCheckBox : System.Web.UI.Page
{
public static string conString = Convert.ToString(ConfigurationManager.ConnectionStrings["dbconnect"].ConnectionString);
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(conString);
string s1 = string.Empty;
foreach (ListItem item in this.chkHobbies.Items)
{
if (item.Selected)
{
s1 = Convert.ToString(item);
com = new SqlCommand("Insert into HOBBIES(Hobby)values('" + s1 + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
Response.Write("Inserted Successfully");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
}