1

my gridview1 is connected to sqldatasource1 and now i create a search command using searchBOX.text and search_button

var ds = new DataSet();

            using (var newconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["User_DB_Connectionstring"].ConnectionString))
            {
                strconn = "select * from User_TBL_DB where (Firstname like '%@search%')";
                var xp = new SqlCommand(strconn, newconnection);
                xp.CommandType = CommandType.Text;
                xp.Parameters.AddWithValue("@search", SearchBOX.Text);

                newconnection.Open();

                xp.ExecuteNonQuery();
                var da = new SqlDataAdapter();
                da.SelectCommand = xp;
               da.Fill(ds, "Name");
            }

            GridView1.DataSource = ds;
            GridView1.DataBind();

i have a problem with GridView1.Datasource =ds; and my datasourceID which is datasourceID="sqlDatasource1" it always give me this error """Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.""

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Align="Center" 
    BackColor="#DEBA84" BorderColor="#DEBA84" BorderWidth="1px" 
    CellPadding="3" ShowFooter="True" 
    Width="713px" BorderStyle="None" CellSpacing="2" 
    onrowcommand="GridView1_RowCommand" DataSourceID="SqlDataSource1" 
    onrowdeleting="GridView1_RowDeleting1" AutoGenerateSelectButton="True" 
    onselectedindexchanged="GridView1_SelectedIndexChanged" 
    AllowPaging="True" >

How am i going to fix this?

kaizoshi
  • 81
  • 11

3 Answers3

1

You might try to set the DataSourceID to null before assigning data source.

Another approach is to bind data programmatically in the PageLoad event so that the datasourceid can be removed in the markup

PythaLye
  • 314
  • 1
  • 8
  • thank you ^_^ it worked i just assign my datasource to null like this.. gridview1.DataSourceID=""; then I connect my gridview using code then when i need to add edit or delete i assign my DataSourceID to SqlDataSource1 again like this gridview1.DataSourceID="SqlDataSource1" but the problem is ... the whole page is refreshing or reopening when i change the DatasourceID :-D .. so the current activity is gone -_- – kaizoshi Feb 20 '15 at 09:52
0

This error means you have two datasource attached to one gridview. Hence you can either set gridview1's datasource programatically or by using sqldatasource1. If you are using SqlDatasource to populate data then change the select query in sqldatasource and use a parameterized stored procedure. Something like this:

     CREATE PROCEDURE StoredProcedure
            /*
            (
            @parameter1 int = 5,
            @parameter2 datatype OUTPUT
            )
            */
        @search nvarchar(1000)
        AS
            /* SET NOCOUNT ON */
    BEGIN
        SET NOCOUNT ON
          /* Variable Declaration */
        Declare @sql_query nvarchar(4000)
        Declare @parameterDefination As Nvarchar(2000)
          /* Build the Transact-SQL String with the input parameters */ 
        Set @sql_query='select * from User_TBL_DB where (1=1) '
         /* check for the condition and build the WHERE clause accordingly */
        If Firstname <> '-1' 
         Set @sql_query = @sql_query + 'AND (Firstname like "%@search%")'
        /* Specify Parameter Format for all input parameters included 
             in the stmt */
        Set @parameterDefination = '@search nvarchar(1000)'
         /* Execute the Transact-SQL String with all parameter value's 
               Using sp_executesql Command */
        Execute sp_Executesql @sql_query, @parameterDefination, @search
        If @@ERROR <> 0 GoTo ErrorHandler
            Set NoCount OFF
            Return(0) 
        ErrorHandler:
            Return(@@ERROR)
    END

While configuring Sqldatasource1 assign Default value of @search to '-1' and on pageload add gridview1.databind()

Or

Remove sqldatasource1 and use your code to search data.

  • ahm ... i'm not sure if I can use this Sir Shatabda Dhar Gupta because I don't think that you're using either C# language or ASP.NET .. because i tried both .. all the syntax are not working.. but then again thank you for trying to help me.. It is also possible that ... I just don't understand this.. maybe because this is beyond my knowledge in programming -_- – kaizoshi Feb 20 '15 at 10:02
  • you have to create a stored procedure in your database; Hence this is SQL for creating a stored procedure. then you need to call the stored procedure in your asp.net page. following is the way to call stored procedure: http://stackoverflow.com/questions/1260952/how-to-execute-a-stored-procedure-within-c-sharp-program – Shatabda Dhar Gupta Feb 23 '15 at 05:45
0

it is clear from your code that you are using two datasource for same gridview ,one is

GridView1.DataSource = ds;

and another one is

DataSourceID="SqlDataSource1"

just remove > DataSourceID="SqlDataSource1" ,because it seems like you are binding your griview programatically .

Bibek Gautam
  • 581
  • 8
  • 30