Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir,
I am getting error in this line:
MIDL
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:

C#
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();

    }



XML
<%@ 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

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.
C#
foreach(ListItem stitem in CheckBoxList1.Items)
{
    if(stitem.Selected==true)
    {
        s += "," + stitem.Text;
    }
}
 
Share this answer
 
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
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.
 
Share this answer
 
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)



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