Click here to Skip to main content
16,017,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Special thanks to
Now what I want to do is display the table in the database, this is what I have tried:


C++
int DisplayTable()
        {
            using (SqlConnection myConnection2 = new SqlConnection(DBConn))
           {
              SqlCommand MyCommand2 = new SqlCommand("SELECT * from TheTableName;", myConnection2);
              myConnection2.Open();
              SqlDataReader reader = MyCommand2.ExecuteReader();

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

           return MyCommand2.ExecuteNonQuery();
           }
      }



Then put in the button like I did before:

C#
protected void btnDisplay_Click(object sender, EventArgs e)
        {
            DisplayTable();

        }



MSIL
but this does not work and gives the error:

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

What should I do?


Special thanks to Wayne Gaylard for the answer to my first question, if you are still out there please help.

Thanks in advance...
Posted

The answer is in the error:

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

Remove one definition. At a guess, check your Gridview definition, and take out the DataSourceID assignment, since you are setting the DataSource in the code.
 
Share this answer
 
This line on my .aspx page was put there automatically "DataSourceID="SqlDataSource1" Which is what I deleted.

Then I got an error about the reader that was still opened...

I got it work as follows:

C++
int DisplayTable()
        {
            using (SqlConnection myConnection2 = new SqlConnection(DBConn))
           {
              SqlCommand MyCommand2 = new SqlCommand("SELECT * from TheTableName;", myConnection2);
              myConnection2.Open();
              SqlDataReader reader = MyCommand2.ExecuteReader();
           GridView1.DataSource  = reader;
           GridView1.DataBind();
           reader.Close();
           return MyCommand2.ExecuteNonQuery();
           }
      }


As a student who has only just started programming, I would like to thank everyone who has given me such good answers that apply to my questions, if only there was a way I could let them know how gratefull I am.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900