473,545 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What code has the best performance to populate a Data Grid with a

I tried a sample of code in MSDN magazine, but now I'm stuck. What code has
the best performance to populate a Data Grid with a SP? Below is the code I
have, which might be completing the wrong way to populate a data grid. I
like using code and not the server explorer. (I replaced the sa password for
this post.)

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

oDa.SelectComma nd = oSelCmd;
dgDealerInfo.Da taSource = oDa;

}
}
}

The following errors occur.
The type or namespace name 'oDa' could not be found (are you missing a
using directive or an assembly reference?)

The name 'oDa' does not exist in the class or namespace
'LicenseDealerS ales.frmDealerS earch'

But you might have a better way to code to populate data grid.

Nov 17 '05 #1
9 3309
hello,
oDA, is a data adapter, you must to add to the form, i dont know if you have
it... or you must to declare it...

"Mike L" wrote:
I tried a sample of code in MSDN magazine, but now I'm stuck. What code has
the best performance to populate a Data Grid with a SP? Below is the code I
have, which might be completing the wrong way to populate a data grid. I
like using code and not the server explorer. (I replaced the sa password for
this post.)

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

oDa.SelectComma nd = oSelCmd;
dgDealerInfo.Da taSource = oDa;

}
}
}

The following errors occur.
The type or namespace name 'oDa' could not be found (are you missing a
using directive or an assembly reference?)

The name 'oDa' does not exist in the class or namespace
'LicenseDealerS ales.frmDealerS earch'

But you might have a better way to code to populate data grid.

Nov 17 '05 #2
Hi,

first of all, you should check if this is a postback or not, you just bind
the grid if it's not a postback
do you need the data for later?
If so you should store it in a dataset,

otherwise just use a DataReader as shown below:

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

if (!IsPostBack )
{
string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

SqlDataReader reader = oCmd.ExecuteRea der();

datagrid.DataSo urce = reader;
datagrid.DataBi nd();
reader.Close();

}
}
}

}

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:02******** *************** ***********@mic rosoft.com...
I tried a sample of code in MSDN magazine, but now I'm stuck. What code
has
the best performance to populate a Data Grid with a SP? Below is the code
I
have, which might be completing the wrong way to populate a data grid. I
like using code and not the server explorer. (I replaced the sa password
for
this post.)

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

oDa.SelectComma nd = oSelCmd;
dgDealerInfo.Da taSource = oDa;

}
}
}

The following errors occur.
The type or namespace name 'oDa' could not be found (are you missing a
using directive or an assembly reference?)

The name 'oDa' does not exist in the class or namespace
'LicenseDealerS ales.frmDealerS earch'

But you might have a better way to code to populate data grid.

Nov 17 '05 #3
Thanks, that's what I needed.

To save a post in the future, what is the code to save it to a dataset?

Also, not sure what your talking about postback, this is not web page but a
client/server application run in the domain.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

first of all, you should check if this is a postback or not, you just bind
the grid if it's not a postback
do you need the data for later?
If so you should store it in a dataset,

otherwise just use a DataReader as shown below:

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

if (!IsPostBack )
{
string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

SqlDataReader reader = oCmd.ExecuteRea der();

datagrid.DataSo urce = reader;
datagrid.DataBi nd();
reader.Close();

}
}
}

}

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:02******** *************** ***********@mic rosoft.com...
I tried a sample of code in MSDN magazine, but now I'm stuck. What code
has
the best performance to populate a Data Grid with a SP? Below is the code
I
have, which might be completing the wrong way to populate a data grid. I
like using code and not the server explorer. (I replaced the sa password
for
this post.)

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

oDa.SelectComma nd = oSelCmd;
dgDealerInfo.Da taSource = oDa;

}
}
}

The following errors occur.
The type or namespace name 'oDa' could not be found (are you missing a
using directive or an assembly reference?)

The name 'oDa' does not exist in the class or namespace
'LicenseDealerS ales.frmDealerS earch'

But you might have a better way to code to populate data grid.


Nov 17 '05 #4
I'm getting an error with your code.

(141): 'System.Windows .Forms.DataGrid ' does not contain a definition for
'DataBind'

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{
string sConnString = "Data Source=db;Datab ase=License;Int egrated
Security=False; User ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e = CommandType.Sto redProcedure;

SqlDataReader reader = oCmd.ExecuteRea der();

dgDealerInfo.Da taSource = reader;
dgDealerInfo.Da taBind();
reader.Close();
}
}

}

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

first of all, you should check if this is a postback or not, you just bind
the grid if it's not a postback
do you need the data for later?
If so you should store it in a dataset,

otherwise just use a DataReader as shown below:

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

if (!IsPostBack )
{
string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

SqlDataReader reader = oCmd.ExecuteRea der();

datagrid.DataSo urce = reader;
datagrid.DataBi nd();
reader.Close();

}
}
}

}

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:02******** *************** ***********@mic rosoft.com...
I tried a sample of code in MSDN magazine, but now I'm stuck. What code
has
the best performance to populate a Data Grid with a SP? Below is the code
I
have, which might be completing the wrong way to populate a data grid. I
like using code and not the server explorer. (I replaced the sa password
for
this post.)

private void frmDealerSearch _Load(object sender, System.EventArg s e)
{

string sConnString = "Data
Source=db;Datab ase=License;Int egrated Security=False; User
ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new
SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e =
CommandType.Sto redProcedure;

oDa.SelectComma nd = oSelCmd;
dgDealerInfo.Da taSource = oDa;

}
}
}

The following errors occur.
The type or namespace name 'oDa' could not be found (are you missing a
using directive or an assembly reference?)

The name 'oDa' does not exist in the class or namespace
'LicenseDealerS ales.frmDealerS earch'

But you might have a better way to code to populate data grid.


Nov 17 '05 #5
Hi Cadel,

Thanks for your feedback.

Actually, "Ignacio Machin \( .NET/ C# MVP \)" assumes that you used Asp.net
DataGrid, so the code snippet he provided is Asp.net specific. For Winform
datagrid, there is no postback concept, we can just use the SqlDataAdapter
to fill a DataSet, then set the DataSet as the DataSource of DataGrid
control, and no DataBind method needed.

Hope this helps
=============== =============== =============
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #6
I'm getting an error "System.Excepti on: Complex DataBinding accepts as a data
source either an IList or an IListSource" on dgDealerInfo.Da taSource = reader;

Here is my code.
private void frmDealerSearch _Load(object sender, System.EventArg s e)
{
string sConnString = "Data Source=db;Datab ase=License;Int egrated
Security=False; User ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e = CommandType.Sto redProcedure;

oCmd.Parameters .Add("@sDealerN um", SqlDbType.NChar , 6);
oCmd.Parameters["@sDealerNu m"].Value = "462004";

SqlDataReader reader = oCmd.ExecuteRea der();

dgDealerInfo.Da taSource = reader;
reader.Close();
}
}

""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your feedback.

Actually, "Ignacio Machin \( .NET/ C# MVP \)" assumes that you used Asp.net
DataGrid, so the code snippet he provided is Asp.net specific. For Winform
datagrid, there is no postback concept, we can just use the SqlDataAdapter
to fill a DataSet, then set the DataSet as the DataSource of DataGrid
control, and no DataBind method needed.

Hope this helps
=============== =============== =============
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #7
I'm getting an error "System.Excepti on: Complex DataBinding accepts as a data
source either an IList or an IListSource" on dgDealerInfo.Da taSource = reader;

Here is my code.
private void frmDealerSearch _Load(object sender, System.EventArg s e)
{
string sConnString = "Data Source=db;Datab ase=License;Int egrated
Security=False; User ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e = CommandType.Sto redProcedure;

oCmd.Parameters .Add("@sDealerN um", SqlDbType.NChar , 6);
oCmd.Parameters["@sDealerNu m"].Value = "462004";

SqlDataReader reader = oCmd.ExecuteRea der();

dgDealerInfo.Da taSource = reader;
reader.Close();
}
}

""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your feedback.

Actually, "Ignacio Machin \( .NET/ C# MVP \)" assumes that you used Asp.net
DataGrid, so the code snippet he provided is Asp.net specific. For Winform
datagrid, there is no postback concept, we can just use the SqlDataAdapter
to fill a DataSet, then set the DataSet as the DataSource of DataGrid
control, and no DataBind method needed.

Hope this helps
=============== =============== =============
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #8
Hi Cadel,

Thanks for your feedback.

Yes, winform databinding is a 2 way databinding, it can not accept
SqlDataReader as the datasource, so you can not use "Ignacio Machin \(
NET/ C# MVP \)"'s code snippet, actually this code snippet is only for
Asp.net DataGrid.

For winform databinding, we should use a SqlDataAdapter to read the
database, then use SqlDataAdapter. Fill method to fill a dataset. At last,
we should set DataSet as the datasource of winform DataGrid control. Like
the sample code below:

string sConnString = "Data Source=db;Datab ase=License;Int egrated
Security=False; User ID=sa;password= password";
string sProc = "prGet_DealerIn fo";

using (SqlConnection oCn = new SqlConnection(s ConnString))
{
using (SqlCommand oCmd = new SqlCommand(sPro c, oCn))
{
oCn.Open();
oCmd.CommandTyp e = CommandType.Sto redProcedure;
oDa.SelectComma nd = oSelCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);
dgDealerInfo.Da taSource = ds;
}
}
Note: I just add a little modification to your original code snippet in the
first post. I assume oDa is the SqlDataAdapter.

Hope this helps
=============== =============== =============== ===========
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #9
Hi Cadel,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #10

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

Similar topics

2
1563
by: Chris Mullins | last post by:
I'm building a GUI that needs to be able to view a large amount of text arranged in rows. Large being anywhere from a few hundred lines through a few hundred thousand. I need a way to "cap" the max number of rows, so that old rows are discarded in favor of new rows if the limit is reached. My use case is very similar to that of the SQL...
3
4070
by: Snake | last post by:
I have a vb .net program which fills a data grid upon form load from an acccess database. This works great. Now, I have to add a combo box and use it to alter the underlying sql statement and re-fill the data grid. I have never done this before, but I created a new sql statement in strSql from the combobox, update the data adapter, and try...
2
5123
by: Anita C | last post by:
Hi, How do I associate or map a specific column in a datatable to a particular element present in an xml document - to read into a datatable as well as write from the datatable to the xml element? Also, how can I associate all the attributes and their values of a particular element to the Name & Value columns of a datatable - to read into a...
4
2948
by: Mountain Bikn' Guy | last post by:
I need some advice on this. I am working on a fairly complex calculator app (C#) with lots of functions (and these functions in turn use math functions from an unmanaged C DLL). A calculation takes a lot of time (up to hours), and should run on a separate thread from the one that the GUI uses. The GUI also needs to display various properties...
0
1073
by: Carlos Lozano | last post by:
Hi, I have to populate a grid from a data table. I do not know how big the table can grow and would like to plan ahead to avoid problems. I will give the user options to filter the data to show only the possible records to easy the navigation. I am looking for the short way to avoid the hassle of creating columns and everything. Options...
3
1388
by: Sivaprasad | last post by:
Hello, Can anybody suggest me, which is the best grid that can be used in ASP.Net. The main functionality I'm looking for is I shoud be able to do heirarchical view of data. Need to do a row merge and column merge. And i dont' want to bind to anything. Similar to Video flex grid ( Now component one)
5
999
by: Mr Newbie | last post by:
Hi, I have tables which are going to display working hours ( Timesheet ) I was wondering the best way to total them. The grid is filled with data from a query bound to a table. Does anyone have any "Best Practices" to offer as a solution. I need to total both down and across. Many thanks
5
5893
by: Rich | last post by:
Hello, I have a search application to search data in tables in a database (3 sql server tables). I populate 2 comboboxes with with data from each table. One combobox will contain unique CompanyID's. The second combobox will contain unique memberID's. Each of the tables that I have to search contain a CompanyID and a memberID field, and...
2
6382
by: John Straumann | last post by:
Hello all: I am trying to populate a GridView with data being read from a call to a web service. I have seen plenty of examples that simply execute a SQL data reader, set the Grid data source to the reader, and then call DataBind(), but I cannot seem to figure out how to build the GridView row by row with anything except text. I need the...
0
7502
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7434
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.