error: DataSource must be the identifier of a control of type IDataSource? ASP.NET C #

0

Hi, I have the following gridview in asp .Net

<asp:GridView ID="grid1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"  CssClass="table table-bordered bs-table" 
       allowpaging="True" ShowFooter="True" BackColor="White" BorderColor="White"  DataKeyNames="">

I had created a query with SqlDataSource but it was not what I wanted and I need the SQL query of the server's server, and create it in a button with onclick event, so that when I clicked it, it will show me the records in the table:

protected void Mostrar_Click(object sender, EventArgs e)
        {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ToString());

                string sql = "SELECT bla bla.. WHERE(Sem.Semana = '52') AND(Cab.Cod_user = '@cod');

                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.Add("@cod", SqlDbType.VarChar, 5);
                cmd.Parameters["@cod"].Value = Session["Cod_user"];
                con.Open();
                cmd.ExecuteNonQuery();
                SqlDataReader rdr = cmd.ExecuteReader();
                grid1.DataSource = rdr;
                grid1.DataBind();

            }

but it shows me the error of the title by these lines .....

 grid1.DataSource = rdr;
 grid1.DataBind();
    
asked by sunflower 02.03.2018 в 19:03
source

1 answer

1

load the reader into a datatable and then assign it to the grid.

SqlDataReader rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
grid1.DataSource = dt;
grid1.DataBind();
    
answered by 02.03.2018 в 19:19