Solved

Populate DataGrid from SQL

Posted on 2009-04-09
6
339 Views
Last Modified: 2012-05-06
I have a DataGrid, and I want to populate it with data from a table in my SQL 2005 database.

As this is all rather new to me can anybody provide code as to how I can populate it (columns being created etc)?  I will be calling a select stored procedure I have made.
0
Comment
Question by:angus_young_acdc
[X]
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
  • 3
  • 3
6 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24105519
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24105558

//C# windows application code, 
 
 //create connection to database
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = "Data Source=.;Initial Catalog=AdventureWorks;Integrated Security=True";
            conn.Open();
 
            //setup SqlCommand and assign SQL query in command
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Connection = conn;
            command.CommandText = "spProduct";
 
            //create one data adapter which will execute the command and fill the data into data set
            System.Data.SqlClient.SqlDataAdapter recordAdp = new System.Data.SqlClient.SqlDataAdapter();
            recordAdp.SelectCommand = command;
            System.Data.DataSet recordSet = new System.Data.DataSet();
            recordAdp.Fill(recordSet);
            command.Dispose();
            conn.Close();
            
            //datagridview1 should be on the form
            dataGridView1.DataSource = recordSet.Tables[0].DefaultView;
 
 
 
--sql server SP code
--create table
create table product
(
Id int,
name varchar(10),
productclass varchar(10)
)
 
--create sp
create proc spProduct
as
select * from product
 
--insert data
insert into product
select 1,          'foo',              '123' UNION ALL
select 2,          'som',              '456' UNION ALL
select 3,          'gee',              '789' UNION ALL
select 4,          'hhh',              'xyz' UNION ALL
select 5,          'sss',              'asd' UNION ALL
select 6,          'ppp',              'abc' UNION ALL
select 7,          'bbb',              'bar' 

Open in new window

0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 24105618
Hi RiteshShah

I have tried to modify that code (as it's using a different grid, and is ASP whereas I'm making a windows app) to the following:

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["SQLConnectionString"]);
            SqlCommand cmd = new SqlCommand("dbo.[sUsersSelect]", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
           
            grdAdminResult.DataSource = cmd.ExecuteReader();
            cmd.Connection.Close();
            cmd.Connection.Dispose();

But I get an exception on the ExecuteReader:
Complex DataBinding accepts as a data source either an IList or an IListSource.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 15

Author Comment

by:angus_young_acdc
ID: 24105623
Ignore the above.  Didn't see your second post, will try that now.
0
 
LVL 15

Author Closing Comment

by:angus_young_acdc
ID: 31568443
Worked like a charm, thank you very much!
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24105631
I wrote the code for windows C# application but there is not a much difference, you can use it in web application as well. do let me know if you will face any problem.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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