Click here to Skip to main content
14,115,288 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Sir,
I am getting error in this line:
foreach(ListItem 1stitem in CheckBoxList1.Items)
        {
            if(1stitem.Selected==true)
            {
                s+=","+1stitem.Text;
            }


Here ListItem is not coming automatically and it is not accepting the object of ListItem.What is the problem?Why it is accepting?

My all codings are:

SqlConnection cn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Q;Integrated Security=True");
    string s;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckBoxList1.Items.Add("BBSR");
        CheckBoxList1.Items.Add("BLR");
        CheckBoxList1.Items.Add("KJR");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cn.Open();
        foreach(ListItem 1stitem in CheckBoxList1.Items)
        {
            if(1stitem.Selected==true)
            {
                s+=","+1stitem.Text;
            }
        }
        Label1.Text=s;
        //SqlCommand cm = new SqlCommand("insert into registration values('"+CheckBoxList1.Text+"')",cn);
        //cm.ExecuteNonQuery();
        cn.Close();

    }



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        </asp:CheckBoxList>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" />

    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>
Posted
Updated 5-Jun-11 23:49pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Everything is correct. But, the problem is in this variable 1stitem. You cannot use a integer in the first word of declaration. So just replace that and try. That's all your problem.
foreach(ListItem stitem in CheckBoxList1.Items)
{
    if(stitem.Selected==true)
    {
        s += "," + stitem.Text;
    }
}
   
v3
Comments
Banajyotsna 6-Jun-11 6:51am
   
foreach(ListItem 1stitem in CheckBoxList1.Items)
Sir problem is in this line.The ListItem is not coming automatically and also my visual studio is not accepting the object of ListItem.Showing error 1stitem.
Banajyotsna 6-Jun-11 6:52am
   
here my visual studio is not accepting the object of ListItem
Toniyo Jackson 6-Jun-11 7:12am
   
see my updated answer
Banajyotsna 7-Jun-11 6:03am
   
Thanks a lot sir.It is running perfectly now.
Banajyotsna 7-Jun-11 6:03am
   
Thanks a lot sir.It is running perfectly now.Sir is this concept possible in window based application?Can you tell me this concept in window based application?Please sir
Toniyo Jackson 7-Jun-11 6:12am
   
You can not create a variable starts with integer in anywhere(windows, web).
Toniyo Jackson 7-Jun-11 6:12am
   
If it helps you vote and accept the answer :)
Banajyotsna 7-Jun-11 6:16am
   
i voted you and accepted this ans also.
Toniyo Jackson 7-Jun-11 6:18am
   
Okay. Thanks :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

According to my understanding of the question,instead of:
if(1stitem.Selected==true)
{
s+=","+1stitem.Text;
}

you can write the code as below:
if(1stitem.Selected)
{
s+=","+1stitem.Value;
}

This will give you the value.
Hope this works.
   
Comments
Banajyotsna 6-Jun-11 6:50am
   
foreach(ListItem 1stitem in CheckBoxList1.Items)
Sir problem is in this line.The ListItem is not coming automatically and also my visual studio is not accepting the object of ListItem.Showing error 1stitem.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190518.1 | Last Updated 6 Jun 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100