Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
	<asp:ListItem>Playing Cricket</asp:ListItem>
	<asp:ListItem>Reading</asp:ListItem>
	<asp:ListItem>Browsing</asp:ListItem>
</asp:CheckBoxList>

Here i have a checkboxlist1 control and i want to store the values selected by the user in database so that how to write the code for that in codebehind i have written the required codein dal and bll respectively. i have tried some thing like
C#
foreach(ListBox li in CheckBoxList1.Items)
{
    if(li.SelectedItem)
    {
    }
}

Edit - code copied from comments
Presentation layer:
C#
protected void btnsave_Click(object sender, EventArgs e)
{
    clsbo objbo = new clsbo();
    objbo.Name = TextBox1.Text.ToString();
    objbo.Department = DropDownList1.SelectedItem.Text.ToString();
    objbo.Gender = RadioButtonList1.SelectedItem.Text.ToString();

    string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    string[] validFileTypes = { "doc", "docx" };
    bool isValidFile = false;
    try
    {
        for (int i = 0; i < validFileTypes.Length; i++)
        {
            if (ext == "." + validFileTypes[i])
            {
                isValidFile = true;
                break;
            }
        }
        if (!isValidFile) //checking whether the file is valid or not
        {
            lbldisplay.ForeColor = System.Drawing.Color.Red;
            lbldisplay.Text = "Invalid File. Only " +
            string.Join(",", validFileTypes) + " files are allowed..";
            return;
        }
        foreach (ListBox li in CheckBoxList1.Items)
        {
            if (li.SelectedItem)
            {

            }
        }
    }
}

BLL:
C#
namespace BLL
{
    public class clsbll
    {
        public void insertdetails(clsbo objbo)
        {
            clsbll objbll = new clsbll();
            objbll.insertdetails(objbo);
        }
    }
}

DAL:
C#
namespace DAL
{
    public class clsdal
    {
        public void insertdata(clsbo objbo)
        {
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                using (SqlCommand cmd = new SqlCommand("insertvwtblregistration", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter nameParameter = new SqlParameter
                    {
                        ParameterName = "@Name",
                        Value = objbo.Name
                    };
                    cmd.Parameters.Add(nameParameter);
                    SqlParameter departmentParameter = new SqlParameter
                    {
                        ParameterName = "@Department",
                        Value = objbo.Department
                    };
                    cmd.Parameters.Add(departmentParameter);
                    SqlParameter HobbiesParameter = new SqlParameter
                    {
                        ParameterName = "@Hobbies",
                        Value = objbo.Hobbies
                    };
                    cmd.Parameters.Add(HobbiesParameter);
                    SqlParameter GenderParameter = new SqlParameter
                    {
                        ParameterName = "@Gender",
                        Value = objbo.Gender
                    };
                    cmd.Parameters.Add(GenderParameter);
                    SqlParameter FileuploadParameter = new SqlParameter
                    {
                        ParameterName = "@Fileupload",
                        Value = objbo.Fileupload
                    };
                    cmd.Parameters.Add(FileuploadParameter);
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}

Business objects:
C#
public class clsbo
{
    private int _Id;
    private string _Name;
    private string _Gender;
    private string _Department;
    private string _Hobbies;
    private string _Fileupload;
    public int Id
    {
        set { _Id = value; }
        get { return _Id; }
    }
    public string Name
    {
        set { _Name = value; }
        get { return _Name; }
    }
    public string Gender
    {
        set { _Gender = value; }
        get { return _Gender; }
    }
    public string Department
    {
        set { _Department = value; }
        get { return _Department; }
    }
    public string Hobbies
    {
        set { _Hobbies = value; }
        get { return _Hobbies; }
    }
    public string Fileupload
    {
        set { _Fileupload = value; }
        get { return _Fileupload; }
    }
}
Posted
Updated 8-Dec-14 3:52am
v3
Comments
ZurdoDev 8-Dec-14 7:34am    
Where are you stuck?
Kornfeld Eliyahu Peter 8-Dec-14 7:35am    
Where the 3 tiers here?
What have you done?
What is the question?
raxhemanth 8-Dec-14 7:36am    
ok let me paste the fullcode
raxhemanth 8-Dec-14 7:36am    
protected void btnsave_Click(object sender, EventArgs e)
{
clsbo objbo = new clsbo();
objbo.Name=TextBox1.Text.ToString();
objbo.Department = DropDownList1.SelectedItem.Text.ToString();
objbo.Gender = RadioButtonList1.SelectedItem.Text.ToString();

string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
string[] validFileTypes ={"doc","docx"};
bool isValidFile = false;
try
{
for (int i = 0; i < validFileTypes.Length; i++)
{
if (ext == "." + validFileTypes[i])
{
isValidFile = true;
break;
}
}
if (!isValidFile) //checking whether the file is valid or not
{
lbldisplay.ForeColor = System.Drawing.Color.Red;
lbldisplay.Text = "Invalid File. Only " +
string.Join(",", validFileTypes) + " files are allowed..";
return;
}
foreach(ListBox li in CheckBoxList1.Items)
{
if(li.SelectedItem)
{

}
}
}

}

PresentationLayer
Tomas Takac 8-Dec-14 9:44am    
First, please never post code in comments, it' virtualy unreadable. Always use "improve question" button to provide more information in your question. Second, always reply to a specific user's comment, there is a small "reply" button next to each comment. That way the user will get a notification of your reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900