Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 13, 2012 02:39 PM by dn631
Member
6 Points
39 Posts
Dec 08, 2012 04:54 AM|LINK
pratik_galoria //to get only selected data foreach (CheckBox chk in chkList1.Items) { if (chk.Selected) { // your code here } }
//to get only selected data foreach (CheckBox chk in chkList1.Items) { if (chk.Selected) { // your code here } }
All-Star
31017 Points
6352 Posts
Dec 08, 2012 05:36 AM|LINK
Dec 08, 2012 05:51 AM|LINK
oned_gk hi ... Have you try my sample? The code is working. I already test it. I create database like yours in my pc.
i cudnt understand your sample because you did not bind it to the gridview
Dec 08, 2012 06:13 AM|LINK
Using Sqldatasource with autopostback control dont need code behind to binding.
If you want using code behind, use privious post to create query.
And below is the sample how to bind data
http://www.codeproject.com/Articles/14249/How-to-populate-DataGridView-GridView-with-SQL-sta
This the combination :
string strlocation = strlocation = ""; foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { strlocation = strlocation + "," + li.Text; } } string sqlquery = "SELECT * FROM [a3] WHERE ('" + strlocation + "' LIKE '%' + [Location] + '%')"; string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True"; SqlConnection sqlConnection = new SqlConnection(strSQLconnection); SqlCommand sqlCommand = new SqlCommand(sqlquery, sqlConnection); sqlConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataBind();
Dec 08, 2012 08:38 AM|LINK
<%@ 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></title> </head> <body> <form id="form1" runat="server"> <br /> <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Location" DataValueField="Location" onselectedindexchanged="CheckBoxList1_SelectedIndexChanged"> </asp:CheckBoxList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>" SelectCommand="SELECT DISTINCT [Location] FROM [a3] WHERE ([Location] IS NOT NULL) ORDER BY [Location]"> </asp:SqlDataSource> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="JobId" HeaderText="JobId" SortExpression="JobId" InsertVisible="False" ReadOnly="True" /> <asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" /> <asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" /> <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" /> <asp:BoundField DataField="Experience" HeaderText="Experience" SortExpression="Experience" /> <asp:BoundField DataField="Information" HeaderText="Information" SortExpression="Information" /> <asp:HyperLinkField DataNavigateUrlFields="JobId" DataNavigateUrlFormatString="Default2.aspx?JobId={0}" HeaderText="Apply" Text="View" Target="_blank"/> </Columns> </asp:GridView> <asp:SqlDataSource runat="server" ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>" SelectCommand="SELECT * FROM [a3]"></asp:SqlDataSource> </form> </body> </html>
Dec 08, 2012 08:41 AM|LINK
<body> <form id="form1" runat="server"> <br /> <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Location" DataValueField="Location" onselectedindexchanged="CheckBoxList1_SelectedIndexChanged"> </asp:CheckBoxList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>" SelectCommand="SELECT DISTINCT [Location] FROM [a3] WHERE ([Location] IS NOT NULL) ORDER BY [Location]"> </asp:SqlDataSource> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="JobId" HeaderText="JobId" SortExpression="JobId" InsertVisible="False" ReadOnly="True" /> <asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" /> <asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" /> <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" /> <asp:BoundField DataField="Experience" HeaderText="Experience" SortExpression="Experience" /> <asp:BoundField DataField="Information" HeaderText="Information" SortExpression="Information" /> <asp:HyperLinkField DataNavigateUrlFields="JobId" DataNavigateUrlFormatString="Default2.aspx?JobId={0}" HeaderText="Apply" Text="View" Target="_blank"/> </Columns> </asp:GridView> <asp:SqlDataSource runat="server" ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>" SelectCommand="SELECT * FROM [a3]"></asp:SqlDataSource> </form> </body>
Dec 08, 2012 11:34 PM|LINK
This work
<form id="form1" runat="server"> <br /> <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Location" DataValueField="Location" AutoPostBack="True"> </asp:CheckBoxList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>" SelectCommand="SELECT DISTINCT [Location] FROM [a3] WHERE ([Location] IS NOT NULL) ORDER BY [Location]"> </asp:SqlDataSource> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="JobId" HeaderText="JobId" SortExpression="JobId" InsertVisible="False" ReadOnly="True" /> <asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" /> <asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" /> <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" /> <asp:BoundField DataField="Experience" HeaderText="Experience" SortExpression="Experience" /> <asp:BoundField DataField="Information" HeaderText="Information" SortExpression="Information" /> <asp:HyperLinkField DataNavigateUrlFields="JobId" DataNavigateUrlFormatString="Default2.aspx?JobId={0}" HeaderText="Apply" Text="View" Target="_blank"/> </Columns> </asp:GridView> </form>
C# code (remove selectedindexchanged event
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CheckBoxList1.DataBind(); foreach (ListItem li in CheckBoxList1.Items) { li.Selected = true; } } string strlocation = strlocation = ""; foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { strlocation = strlocation + "," + li.Text; } } string sqlquery = "SELECT * FROM [a3] WHERE ('" + strlocation + "' LIKE '%' + [Location] + '%')"; string strSQLconnection =ConfigurationManager.ConnectionStrings["jobsConnectionString"].ToString(); SqlConnection sqlConnection = new SqlConnection(strSQLconnection); SqlCommand sqlCommand = new SqlCommand(sqlquery, sqlConnection); sqlConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataBind(); }
Dec 11, 2012 09:59 AM|LINK
oned_gk This work <form id="form1" runat="server"> <br /> <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Location" DataValueField="Location" AutoPostBack="True"> </asp:CheckBoxList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>" SelectCommand="SELECT DISTINCT [Location] FROM [a3] WHERE ([Location] IS NOT NULL) ORDER BY [Location]"> </asp:SqlDataSource> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="JobId" HeaderText="JobId" SortExpression="JobId" InsertVisible="False" ReadOnly="True" /> <asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" /> <asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" /> <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" /> <asp:BoundField DataField="Experience" HeaderText="Experience" SortExpression="Experience" /> <asp:BoundField DataField="Information" HeaderText="Information" SortExpression="Information" /> <asp:HyperLinkField DataNavigateUrlFields="JobId" DataNavigateUrlFormatString="Default2.aspx?JobId={0}" HeaderText="Apply" Text="View" Target="_blank"/> </Columns> </asp:GridView> </form> C# code (remove selectedindexchanged event protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CheckBoxList1.DataBind(); foreach (ListItem li in CheckBoxList1.Items) { li.Selected = true; } } string strlocation = strlocation = ""; foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { strlocation = strlocation + "," + li.Text; } } string sqlquery = "SELECT * FROM [a3] WHERE ('" + strlocation + "' LIKE '%' + [Location] + '%')"; string strSQLconnection =ConfigurationManager.ConnectionStrings["jobsConnectionString"].ToString(); SqlConnection sqlConnection = new SqlConnection(strSQLconnection); SqlCommand sqlCommand = new SqlCommand(sqlquery, sqlConnection); sqlConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataBind(); }
Is there any other way....because ...if there are 3 or 4 checkbox list then,will this work
Dec 13, 2012 02:39 PM|LINK
string sqlquery = "SELECT * FROM [a3] WHERE ('" + strlocation + "' + LIKE '%'+ [Location] + '%')"; string strSqlConnection = "Data Source=PRAVEEN-PC;Initial Catalog=jobs;Integrated Security=True"; SqlConnection sqlC = new SqlConnection(strSqlConnection); SqlCommand sqlCommand = new SqlCommand(sqlquery, sqlC); sqlC.Open(); SqlDataReader dr = sqlCommand.ExecuteReader(); GridView1.DataSource = dr; GridView1.DataBind();
error is incorrect syntax near LIKE...............................................anyidea what the error might be
dn631
Member
6 Points
39 Posts
Re: how to use chkboxlist...........
Dec 08, 2012 04:54 AM|LINK
oned_gk
All-Star
31017 Points
6352 Posts
Re: how to use chkboxlist...........
Dec 08, 2012 05:36 AM|LINK
dn631
Member
6 Points
39 Posts
Re: how to use chkboxlist...........
Dec 08, 2012 05:51 AM|LINK
i cudnt understand your sample because you did not bind it to the gridview
oned_gk
All-Star
31017 Points
6352 Posts
Re: how to use chkboxlist...........
Dec 08, 2012 06:13 AM|LINK
Using Sqldatasource with autopostback control dont need code behind to binding.
If you want using code behind, use privious post to create query.
And below is the sample how to bind data
http://www.codeproject.com/Articles/14249/How-to-populate-DataGridView-GridView-with-SQL-sta
This the combination :
string strlocation = strlocation = ""; foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { strlocation = strlocation + "," + li.Text; } } string sqlquery = "SELECT * FROM [a3] WHERE ('" + strlocation + "' LIKE '%' + [Location] + '%')"; string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True"; SqlConnection sqlConnection = new SqlConnection(strSQLconnection); SqlCommand sqlCommand = new SqlCommand(sqlquery, sqlConnection); sqlConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataBind();dn631
Member
6 Points
39 Posts
Re: how to use chkboxlist...........
Dec 08, 2012 08:38 AM|LINK
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Location"
DataValueField="Location"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>"
SelectCommand="SELECT DISTINCT [Location] FROM [a3] WHERE ([Location] IS NOT NULL) ORDER BY [Location]">
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="JobId" HeaderText="JobId" SortExpression="JobId"
InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Position" HeaderText="Position"
SortExpression="Position" />
<asp:BoundField DataField="Company" HeaderText="Company"
SortExpression="Company" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="Experience" HeaderText="Experience"
SortExpression="Experience" />
<asp:BoundField DataField="Information" HeaderText="Information"
SortExpression="Information" />
<asp:HyperLinkField DataNavigateUrlFields="JobId"
DataNavigateUrlFormatString="Default2.aspx?JobId={0}" HeaderText="Apply"
Text="View" Target="_blank"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server"
ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>"
SelectCommand="SELECT * FROM [a3]"></asp:SqlDataSource>
</form>
</body>
</html>
dn631
Member
6 Points
39 Posts
Re: how to use chkboxlist...........
Dec 08, 2012 08:41 AM|LINK
<body>
<form id="form1" runat="server">
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Location"
DataValueField="Location"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>"
SelectCommand="SELECT DISTINCT [Location] FROM [a3] WHERE ([Location] IS NOT NULL) ORDER BY [Location]">
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="JobId" HeaderText="JobId" SortExpression="JobId"
InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Position" HeaderText="Position"
SortExpression="Position" />
<asp:BoundField DataField="Company" HeaderText="Company"
SortExpression="Company" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="Experience" HeaderText="Experience"
SortExpression="Experience" />
<asp:BoundField DataField="Information" HeaderText="Information"
SortExpression="Information" />
<asp:HyperLinkField DataNavigateUrlFields="JobId"
DataNavigateUrlFormatString="Default2.aspx?JobId={0}" HeaderText="Apply"
Text="View" Target="_blank"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server"
ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>"
SelectCommand="SELECT * FROM [a3]"></asp:SqlDataSource>
</form>
</body>
oned_gk
All-Star
31017 Points
6352 Posts
Re: how to use chkboxlist...........
Dec 08, 2012 11:34 PM|LINK
This work
<form id="form1" runat="server"> <br /> <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Location" DataValueField="Location" AutoPostBack="True"> </asp:CheckBoxList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:jobsConnectionString %>" SelectCommand="SELECT DISTINCT [Location] FROM [a3] WHERE ([Location] IS NOT NULL) ORDER BY [Location]"> </asp:SqlDataSource> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="JobId" HeaderText="JobId" SortExpression="JobId" InsertVisible="False" ReadOnly="True" /> <asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" /> <asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" /> <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" /> <asp:BoundField DataField="Experience" HeaderText="Experience" SortExpression="Experience" /> <asp:BoundField DataField="Information" HeaderText="Information" SortExpression="Information" /> <asp:HyperLinkField DataNavigateUrlFields="JobId" DataNavigateUrlFormatString="Default2.aspx?JobId={0}" HeaderText="Apply" Text="View" Target="_blank"/> </Columns> </asp:GridView> </form>C# code (remove selectedindexchanged event
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CheckBoxList1.DataBind(); foreach (ListItem li in CheckBoxList1.Items) { li.Selected = true; } } string strlocation = strlocation = ""; foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { strlocation = strlocation + "," + li.Text; } } string sqlquery = "SELECT * FROM [a3] WHERE ('" + strlocation + "' LIKE '%' + [Location] + '%')"; string strSQLconnection =ConfigurationManager.ConnectionStrings["jobsConnectionString"].ToString(); SqlConnection sqlConnection = new SqlConnection(strSQLconnection); SqlCommand sqlCommand = new SqlCommand(sqlquery, sqlConnection); sqlConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataBind(); }dn631
Member
6 Points
39 Posts
Re: how to use chkboxlist...........
Dec 11, 2012 09:59 AM|LINK
Is there any other way....because ...if there are 3 or 4 checkbox list then,will this work
dn631
Member
6 Points
39 Posts
Re: how to use chkboxlist...........
Dec 13, 2012 02:39 PM|LINK
string sqlquery = "SELECT * FROM [a3] WHERE ('" + strlocation + "' + LIKE '%'+ [Location] + '%')";
string strSqlConnection = "Data Source=PRAVEEN-PC;Initial Catalog=jobs;Integrated Security=True";
SqlConnection sqlC = new SqlConnection(strSqlConnection);
SqlCommand sqlCommand = new SqlCommand(sqlquery, sqlC);
sqlC.Open();
SqlDataReader dr = sqlCommand.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
error is incorrect syntax near LIKE...............................................anyidea what the error might be