By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

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

P: n/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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.