tags:

views:

32

answers:

2

simple thing get a list of columns present in the binded grid view, thats it. but gives errors.

try to get the number of columns also does not work! it says zero?

heres the code,

please help

code>
 try
            {

                commandString = "Select * from Table_1";

                conn = new SqlConnection(Class1.connection);

                command = new SqlCommand(commandString, conn);

                conn.Open();

                reader = command.ExecuteReader();



                GridView1.DataSource = reader;

                GridView1.DataBind();

               //also not wrking??
                Label1.Text = GridView1.Columns.Count.ToString;


                reader.Close();
                reader.Dispose();

                conn.Close();
                conn.Dispose();

whats wrong,

is there an alternate way to ask grid view for its columns?

autogenerate columns is true.

A: 

Binding to a reader isn't a very good idea...bind it to a DataTable and see how well that works for you. This may help - http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/df9f7cdb-9b27-4e54-8527-1e4701cefbe1/

Dan H
why isnt it a not a good idea, i mean are there any security issues, or data will be lost issues or lag issues, or just difficulty?
First, I'm not sure how paging would work in the gridview if it's bound to a reader, but just as a best practice your presentation layer shouldn't know anything about your database. By binding your gridview to a reader you have high coupling between layers. Normally you want to return a DataTable or, better yet, a Data Transfer Object (DTO) from a service layer or business layer. I guess if it's a small app where you don't care about scalability then binding to a reader is OK, but personally I would still bind it to a DataTable.
Dan H
A: