2

Here i have tried to do bind the gridview as per the selection in the checkboxlist. I'm getting only blank page when its executed. can any one specify what's the mistake?

protected void Button1_Click(object sender, EventArgs e)
{
String query = "Select Proj_id,Proj_name,Front_end,Back_end from Proj_details where Front_end = 'Android'";
String[] frontend ={ "Android", "Asp", "Asp.net", "C#.net", "J2EE", "Java", "Matlab", "NS2", "PHP", "VB", "VB.net" };
try
{
for (int i = 0; i <= CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
query = query.Insert(query.Length, frontend[i] + "','");
}

con.Open();
query = query.Remove(query.Length - 1);
query = query.Remove(query.Length - 1);
query = query.Insert(query.Length, ")");
SqlDataAdapter sqlada = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
sqlada.Fill(ds, "Proj_details");


gv_search_project.DataSource = ds.Tables[0];
gv_search_project.DataBind();



}
catch (Exception ex)
{

}
finally
{

Label1.Text = query.ToString();
}

} 
2
  • 2
    Maybe you got an exception. Your catch code is empty. Commented Aug 16, 2012 at 12:48
  • Ya I got this exception... System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) at System.Web.UI.WebControls.ListItemCollection.get_Item(Int32 index) at Search.Button1_Click(Object sender, EventArgs e) in e:\Students\lavan\firstweb\Search.aspx.cs:line 29 Commented Aug 16, 2012 at 13:04

3 Answers 3

2

You're going out of the array here:

for (int i = 0; i <= CheckBoxList1.Items.Count; i++)
{
   if (CheckBoxList1.Items[i].Selected)
      query = query.Insert(query.Length, frontend[i] + "','");
}

You need to replace:

i <= CheckBoxList1.Items.Count

With:

i < CheckBoxList1.Items.Count
Sign up to request clarification or add additional context in comments.

Comments

0
gv_search_project.DataSource = ds.Tables["Proj_details"];

Comments

0

You are building a query by appending additional Front_end values, but the base query you are appending to has a where clause like this:

where Front_end = 'Android'

So (if I read your code correctly) the resulting query is (for instance):

Select Proj_id,Proj_name,Front_end,Back_end 
from Proj_details 
where Front_end = 'Android'Android','C#.net','Matlab','VB')

To start with the query i simply broken. And secondly I think what you intended was to have an IN clause:

String query = "Select Proj_id,Proj_name,Front_end,Back_end from Proj_details where Front_end in ('Android','";

This, coupled with Amiram Korach's note about < vs <= should make the code work. But it will always include 'Android' (which you initial attempt also did).

Do change you empty catch clause though:

catch (Exception ex)               
{               

}

If you hadn't been swallowing all exceptions you would probably have found the problems yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.