Click here to Skip to main content
15,896,330 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
Hello sir i wanna search the data using checkbox aur checkboxlist like if i select two checkbox then i wanna to get the data of both the checkbox id's. please give me a demo code for this type of query.
Posted
Comments
ZurdoDev 7-Aug-13 13:15pm    
google has demo code. Where are you stuck?

1 solution

Hello ,

You can try this code:

ASP.NET
<div>>
        <asp:CheckBoxList ID="chkUserData" runat="server" AutoPostBack="true" 
            onselectedindexchanged="chkUserData_SelectedIndexChanged" 
            RepeatDirection="Horizontal">
        </asp:CheckBoxList>
        <asp:GridView ID="gvUserDetails" runat="server" EmptyDataText="No Record Found">
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>



C#
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select UserId,UserName,Email from UserMaster", con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                chkUserData.Items.Insert(i, new ListItem(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString()));
            }
        }
    }

  protected void chkUserData_SelectedIndexChanged(object sender, EventArgs e)
    {
 if (!String.IsNullOrEmpty(chkUserData.SelectedValue))
        {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select UserId,UserName,Email from UserMaster where UserId='" + chkUserData.SelectedValue + "'", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvUserDetails.DataSource = ds;
        gvUserDetails.DataBind();
    }
        else
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select UserId,UserName,Email from UserMaster", con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            gvUserDetails.DataSource = ds;
            gvUserDetails.DataBind();
        }
}


Here you can change as you want.
 
Share this answer
 
Comments
yuvi joshi 13-Aug-13 5:45am    
sir i have tried this but this code is showing me one select result at a time. i wanna get multiple result from checkbox checked items..please help me???????

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