user1352057
user1352057

Reputation: 3182

Issue when trying to filter GridView

Im trying to filter my GridView Data based on a dropbox list and a search term.

My GridView is currently bound through the ASP server side using an SQLdatasource.

I am trying to filter the GridView through C# from a onclick_button event.

Here is my filtering code as it stands:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace ViewCDs
{
public partial class _Default : System.Web.UI.Page
{


    protected void btnSearch_Click(object sender, EventArgs e)
    {

        string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Dave\Documents\cdsCollections.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        SqlConnection sqlcon = new SqlConnection(connstring);
        string str = TextBox1.Text;
        string str2 = DropDownList1.Text;
        string set = "";

        if (str2 == "Artist")
        {
            set = "artist";
        }

        if (str2 == "CD Title")
        {
            set = "cdTitle";
        }

        if (str2 == "Music Genre")
        {
            set = "genre";
        }

        SqlCommand sqlcmd = new SqlCommand("select * from cds where " + set + " like '%" + str + "%'", sqlcon);
        SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {

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

    }

}

}

And how the event is triggered on the ASP side:

  <asp:Button ID="Button1" runat="server" Text="Search" onclick="btnSearch_Click" />

This is a pretty standard layout, the results of the wildcard query are stored in the dataset object and if the dataset has a count higher than 0 then this should bound to my GridView. But this binding is not happening and I'm receiving this error:

Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.

Description: An unhandled exception occurred during the execution of the current web    request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.

Upvotes: 0

Views: 397

Answers (1)

Blachshma
Blachshma

Reputation: 17385

Without seeing your aspx code, you probably defined GridView1 like this:

<asp:GridView runat="server" ID="GridView1" DataSourceID="SOME_DATA_SOURCE" >

Notice the DataSourceID. As the error states, you can't use both DataSourceID and DataSource. The solution is to only use DataSource set it to whatever your DataSourceID's Select Query is - in the Page_Load event.

In other words, define your GridView without the DataSourceId:

     <asp:GridView runat="server" ID="GridView1" ... >

Then in the code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds =  // Get Data from DB
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

    }

Another problem you have is a security issue - your code is prone to Sql Injections - Make sure you parametrize your queries

Upvotes: 1

Related Questions