Solved

use datareader for binding dataGridView1

Posted on 2014-04-22
8
87 Views
Last Modified: 2014-11-30
i have a table in sql server 2008
i want to bind it using datareader
0
Comment
Question by:ajaymaster1558
  • 4
  • 3
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40014719
you assign the datareader object the same way, why do you ask?
any issue? error messages?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40014721
most important point for your feedback: please keep your test/code as simple as possible as to reproduce the problem.
0
 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 40014916
sample code is given below..
using (SqlCommand command = new SqlCommand("select * from YourTable", new SqlConnection("Server=.;Trusted_Connection=False;User ID=xxx;Password=xxxx;Initial Catalog=DatabaseName")))
            {
                try
                {
                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    GridView1.DataSource = reader;
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    command.Connection.Close();
                }
                finally
                {
                    command.Connection.Close();
                }
            }
0
 

Author Comment

by:ajaymaster1558
ID: 40020176
SqlConnection cnsql = new SqlConnection(cnstring);
            cnsql.Open();
            string sql = "select * from sell";
            SqlCommand cmsql = new SqlCommand(sql, cnsql);
            SqlDataReader drsql = cmsql.ExecuteReader();

            dataGridView1.DataSource = drsql;

this is my code
but it's not work
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40020470
and what it's your exact error message?
0
 

Author Comment

by:ajaymaster1558
ID: 40029339
not get any error but it not show any data or any thing
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40029348
that is another issue, for example the grid is not actually runat=server, or rebinded later with no data source ...
you will need to debug if you see any rebind after that code
0
 

Author Closing Comment

by:ajaymaster1558
ID: 40472429
thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now