473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Simple question on DataTable and DataGrid

Hi,

I'm trying to load data from SQL Server into a DataGrid. I planned on using
a DataTable to store the data, but I can't get the logic correct. Can
someone please tell me what I'm doing wrong? Thanks.

try

{

DatabaseConnection dc = new DatabaseConnection(); //this handles all the ADO
connections - this works fine used in other parts of the application

dc.Connect(); //opens the connection for reading

this.sqlSelectCommand1.CommandText = "SELECT RecipeIngredientName,
IngredientQuantity FROM RecipeIngredients WHERE (RecipeID = " + sRecipeID +
") order by RecipeIngredientName asc";
SqlCommand cmdSQL = null;
cmdSQL = new SqlCommand(this.sqlSelectCommand1.CommandText,dc.C onnection);

SqlDataReader reader = cmdSQL.ExecuteReader();
while (reader.Read())

{

What do I do here? I need to load the data into a table so the grid can
display it

}

} //try

Thanks in advance,

Doug

Nov 15 '05 #1
2 1055
Doug wrote:
SqlDataReader reader = cmdSQL.ExecuteReader();


Use a SqlDataAdapter instead. Once the SelectCommand property has been
set, you can just call the Fill method to populate your DataTable:

SqlConnection conn = new SqlConnection(<connectionstring>);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(<querystring>, conn);
adapter.Fill(datatable);

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
Hi Doug,

Why don't you use SqlDataAdapter.
Just create it, set its SelectCommand to sqlSelectCommand1 and invoke

myDataAdapter.Fill(myDataTable);

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Doug" <dk*******@hotmail.com> wrote in message
news:O3**************@TK2MSFTNGP10.phx.gbl...
Hi,

I'm trying to load data from SQL Server into a DataGrid. I planned on using a DataTable to store the data, but I can't get the logic correct. Can
someone please tell me what I'm doing wrong? Thanks.

try

{

DatabaseConnection dc = new DatabaseConnection(); //this handles all the ADO connections - this works fine used in other parts of the application

dc.Connect(); //opens the connection for reading

this.sqlSelectCommand1.CommandText = "SELECT RecipeIngredientName,
IngredientQuantity FROM RecipeIngredients WHERE (RecipeID = " + sRecipeID + ") order by RecipeIngredientName asc";
SqlCommand cmdSQL = null;
cmdSQL = new SqlCommand(this.sqlSelectCommand1.CommandText,dc.C onnection);

SqlDataReader reader = cmdSQL.ExecuteReader();
while (reader.Read())

{

What do I do here? I need to load the data into a table so the grid can
display it

}

} //try

Thanks in advance,

Doug

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dr. Len | last post by:
Hi all! In my app I implemented a separate worker thread that handles network communication using Net.Sockets. It needs to make updates to a DataTable object, which is displayed to the user by a...
1
by: thejackofall | last post by:
Hi. I am having a problem doing a simple sorting. Would someone be cool enough to take a look and advise? It doesn't sort. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
0
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some...
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
2
by: ven | last post by:
Hello i have a dynamic datatable in my page. I wanna to write data to textboxes and after click on button "ADD data" i want to refresh datagrid on page... Here is my code : ' Insert page code...
3
by: Tim::.. | last post by:
I currently have the following datagrid but want to turn the name and email column into a hyperlink in the codebehind! Can someone please tell me how I achieve this! Thanks Private Sub...
0
by: Becker | last post by:
I have a datatable that I use to display running processes. These running processes are spawned as different threads. I use the readerwriterlock class to ensure that when I write to the datatable,...
3
by: Datatable Dataset Datagrid help | last post by:
Hi I am somewhat confused, I am new at VB.net I use XML data, I have a datagrid, I created a datatable so that I can create a custom format like true is this graphic false is this graphic and...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
13
by: Saber | last post by:
I did a lot of searches and read something about datagrids. But I couldn't find the answer of my simple question, how can I show only my desired columns of a table? for example I wrote this sql...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.