473,249 Members | 1,408 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,249 software developers and data experts.

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.EventArgs e)
{

string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

oDa.SelectCommand = oSelCmd;
dgDealerInfo.DataSource = 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
'LicenseDealerSales.frmDealerSearch'

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

Nov 17 '05 #1
9 3285
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.EventArgs e)
{

string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

oDa.SelectCommand = oSelCmd;
dgDealerInfo.DataSource = 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
'LicenseDealerSales.frmDealerSearch'

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.EventArgs e)
{

if (!IsPostBack )
{
string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

SqlDataReader reader = oCmd.ExecuteReader();

datagrid.DataSource = reader;
datagrid.DataBind();
reader.Close();

}
}
}

}

cheers,

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

"Mike L" <Ca***@nospam.nospam> wrote in message
news:02**********************************@microsof t.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.EventArgs e)
{

string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

oDa.SelectCommand = oSelCmd;
dgDealerInfo.DataSource = 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
'LicenseDealerSales.frmDealerSearch'

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.EventArgs e)
{

if (!IsPostBack )
{
string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

SqlDataReader reader = oCmd.ExecuteReader();

datagrid.DataSource = reader;
datagrid.DataBind();
reader.Close();

}
}
}

}

cheers,

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

"Mike L" <Ca***@nospam.nospam> wrote in message
news:02**********************************@microsof t.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.EventArgs e)
{

string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

oDa.SelectCommand = oSelCmd;
dgDealerInfo.DataSource = 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
'LicenseDealerSales.frmDealerSearch'

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.EventArgs e)
{
string sConnString = "Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

SqlDataReader reader = oCmd.ExecuteReader();

dgDealerInfo.DataSource = reader;
dgDealerInfo.DataBind();
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.EventArgs e)
{

if (!IsPostBack )
{
string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

SqlDataReader reader = oCmd.ExecuteReader();

datagrid.DataSource = reader;
datagrid.DataBind();
reader.Close();

}
}
}

}

cheers,

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

"Mike L" <Ca***@nospam.nospam> wrote in message
news:02**********************************@microsof t.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.EventArgs e)
{

string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

oDa.SelectCommand = oSelCmd;
dgDealerInfo.DataSource = 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
'LicenseDealerSales.frmDealerSearch'

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.Exception: Complex DataBinding accepts as a data
source either an IList or an IListSource" on dgDealerInfo.DataSource = reader;

Here is my code.
private void frmDealerSearch_Load(object sender, System.EventArgs e)
{
string sConnString = "Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@sDealerNum", SqlDbType.NChar, 6);
oCmd.Parameters["@sDealerNum"].Value = "462004";

SqlDataReader reader = oCmd.ExecuteReader();

dgDealerInfo.DataSource = 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.Exception: Complex DataBinding accepts as a data
source either an IList or an IListSource" on dgDealerInfo.DataSource = reader;

Here is my code.
private void frmDealerSearch_Load(object sender, System.EventArgs e)
{
string sConnString = "Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@sDealerNum", SqlDbType.NChar, 6);
oCmd.Parameters["@sDealerNum"].Value = "462004";

SqlDataReader reader = oCmd.ExecuteReader();

dgDealerInfo.DataSource = 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;Database=License;Integrated
Security=False;User ID=sa;password=password";
string sProc = "prGet_DealerInfo";

using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;
oDa.SelectCommand = oSelCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);
dgDealerInfo.DataSource = 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
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...
3
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...
2
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?...
4
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...
0
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...
3
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...
5
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...
5
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...
2
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...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.