.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to insert data of selected checkboxlist control in database.. (sql server)

Posted By: Nehal     Posted Date: October 29, 2009    Points:2   Category :ASP.Net
how to store checkboxlist data into database using asp.net with c#...

For example i have under hobbies list: music,reading,watchingTV,playing,chatting,singing,cooking,dancing

In this list If i select music,reading and playing from the checkboxlist and click button the items selected should to be stored in backend table for particular user..how to do this ...Please Explain me with an example..
I have created 3 tables.
1 is user.-->user_id, user_name
2 is event--> event_id, event_name, event_provider
3 is event_user--> event_id, user_id, date.
so that i can find the whole list which particuler user have selected how many and which events...

Responses
Author: Amit Mehra             
Posted Date: October 29, 2009     Points: 5   

Hi Nehal,

Please refer this post to get the data from checkboxlist and save into SQL Server..

http://forums.asp.net/t/1398453.aspx

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Ashutosh Jha             
Posted Date: December 05, 2013     Points: 20   

Hi Nehal,

To insert selected value to database-

string s1= string.Empty;
foreach (ListItem item in this.CheckBoxList1.Items)
if (item.Selected)
s1+= item + ",";

SqlCommand cmd = new SqlCommand("Insert into tblname values('" + s1 + "')", cn); //
conn2.Open();
int i = cmd.ExecuteNonQuery();
conn2.Close();


to select on "onpageevent"-

void LoadCheckList()
{
string s1 = "";
SqlCommand cmd = new SqlCommand("Select s1 from tblname", conn2);
SqlDataReader dr;
conn2.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
s1= dr["s1"].ToString();
}
conn2.Close();
string[] a1 = s1.Split(',');
foreach (ListItem item in this.CheckBoxList1.Items)
{
foreach (string s in a1)
{
if (item.Text== s)
{
item.Selected = true;
}
}
}
}

Regards,
Ashutosh Jha
ashutosh741@gmail.com

http://www.tricksroad.com


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend