• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • Last Modified:

use datareader for binding dataGridView1

i have a table in sql server 2008
i want to bind it using datareader
0
ajaymaster1558
Asked:
ajaymaster1558
  • 4
  • 3
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you assign the datareader object the same way, why do you ask?
any issue? error messages?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
most important point for your feedback: please keep your test/code as simple as possible as to reproduce the problem.
0
 
jayakrishnabhCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ajaymaster1558Author Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
and what it's your exact error message?
0
 
ajaymaster1558Author Commented:
not get any error but it not show any data or any thing
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
ajaymaster1558Author Commented:
thanks
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now