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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit Kumar SinghPosted Feb 17, 2016, 7:41 AM
(
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);
}
}
}
Krishna Rajput SinghPosted Feb 17, 2016, 7:07 AM
Amit Kumar SinghPosted Feb 17, 2016, 6:26 AM
Chandu KumawatPosted Feb 17, 2016, 6:20 AM