Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

use datareader for binding dataGridView1

Posted on 2014-04-22
8
96 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 143

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 143

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
LVL 143

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 143

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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