Showing posts with label thecode. Show all posts
Showing posts with label thecode. Show all posts

Thursday, March 29, 2012

Populate 2 or more datagrids from one OleDBCommand

Hi All,

I wish to populate more than one datagrid from the same OleDBCommand. The
code I have is:

Dim objCmd As New OleDbCommand(strSql, objConn)

Then...

Me.dgTariffHolidayHomesBand1.DataSource = objCmd.ExecuteReader()
Me.dgTariffHolidayHomesBand1.Visible = True
Me.dgTariffHolidayHomesBand2.DataSource = objCmd.ExecuteReader()
Me.dgTariffHolidayHomesBand2.Visible = True

I get this error when the second call to objCmd.ExecuteReader() is called:
System.InvalidOperationException: ExecuteReader requires an open and
available Connection.

The reason for populating multiple grids from the same data, is that I am
calling ALL columns in the SQL, and then using (for example) columns A and
B
in DG1, C and D in DG2...and so on.

Surely I don't need a new recordset for each datagrid?

Simon.There are a couple problems with your approach.

First, the ExecuteReader method requires that you close and re-open the
connection, just as the code implies but you can't use a DataReader as the
DataSource for a DataGrid. You need something that implements IList or
IListSource. You can research both of those interfaces in the MSDN Library
to see what classes implement them.

Another problem is that you're making two queries to the database and
populating the DataGrids sequentially. What happens if the data changes
between calls? You can't be sure the data in the DataGrids will match.
What you need to do is get the data once, using a DataSet or even a
DataTable and set both DataGrids to use the same DataSource.

Hope this helps,

DalePres
MCAD, MCSE, MCDBA

"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:ODOqdixBFHA.1400@.TK2MSFTNGP11.phx.gbl...
> Hi All,
> I wish to populate more than one datagrid from the same OleDBCommand. The
> code I have is:
> Dim objCmd As New OleDbCommand(strSql, objConn)
> Then...
> Me.dgTariffHolidayHomesBand1.DataSource = objCmd.ExecuteReader()
> Me.dgTariffHolidayHomesBand1.Visible = True
> Me.dgTariffHolidayHomesBand2.DataSource = objCmd.ExecuteReader()
> Me.dgTariffHolidayHomesBand2.Visible = True
> I get this error when the second call to objCmd.ExecuteReader() is called:
> System.InvalidOperationException: ExecuteReader requires an open and
> available Connection.
> The reason for populating multiple grids from the same data, is that I am
> calling ALL columns in the SQL, and then using (for example) columns A and
> B
> in DG1, C and D in DG2...and so on.
> Surely I don't need a new recordset for each datagrid?
> Simon.

Populate 2 or more datagrids from one OleDBCommand

Hi All,
I wish to populate more than one datagrid from the same OleDBCommand. The
code I have is:
Dim objCmd As New OleDbCommand(strSql, objConn)
Then...
Me.dgTariffHolidayHomesBand1.DataSource = objCmd.ExecuteReader()
Me.dgTariffHolidayHomesBand1.Visible = True
Me.dgTariffHolidayHomesBand2.DataSource = objCmd.ExecuteReader()
Me.dgTariffHolidayHomesBand2.Visible = True
I get this error when the second call to objCmd.ExecuteReader() is called:
System.InvalidOperationException: ExecuteReader requires an open and
available Connection.
The reason for populating multiple grids from the same data, is that I am
calling ALL columns in the SQL, and then using (for example) columns A and
B
in DG1, C and D in DG2...and so on.
Surely I don't need a new recordset for each datagrid?
Simon.There are a couple problems with your approach.
First, the ExecuteReader method requires that you close and re-open the
connection, just as the code implies but you can't use a DataReader as the
DataSource for a DataGrid. You need something that implements IList or
IListSource. You can research both of those interfaces in the MSDN Library
to see what classes implement them.
Another problem is that you're making two queries to the database and
populating the DataGrids sequentially. What happens if the data changes
between calls? You can't be sure the data in the DataGrids will match.
What you need to do is get the data once, using a DataSet or even a
DataTable and set both DataGrids to use the same DataSource.
Hope this helps,
DalePres
MCAD, MCSE, MCDBA
"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:ODOqdixBFHA.1400@.TK2MSFTNGP11.phx.gbl...
> Hi All,
> I wish to populate more than one datagrid from the same OleDBCommand. The
> code I have is:
> Dim objCmd As New OleDbCommand(strSql, objConn)
> Then...
> Me.dgTariffHolidayHomesBand1.DataSource = objCmd.ExecuteReader()
> Me.dgTariffHolidayHomesBand1.Visible = True
> Me.dgTariffHolidayHomesBand2.DataSource = objCmd.ExecuteReader()
> Me.dgTariffHolidayHomesBand2.Visible = True
> I get this error when the second call to objCmd.ExecuteReader() is called:
> System.InvalidOperationException: ExecuteReader requires an open and
> available Connection.
> The reason for populating multiple grids from the same data, is that I am
> calling ALL columns in the SQL, and then using (for example) columns A and
> B
> in DG1, C and D in DG2...and so on.
> Surely I don't need a new recordset for each datagrid?
> Simon.
>

Saturday, March 24, 2012

populate FormView fields from a DropDownList

Hi,
I'm trying to build an online form using asp.net and vb.net for the
code behind in Visual Studio 2005.
I want to fill 3 fields in a FormView that's connected to one table
(table1) with information from another table (table2) by choosing the
key field from table2 in a DropDownList.
Table2 has 3 fields with info that doesn't change (name, phone# and
email for about 5 customer service reps). I want each rep to choose
their name in the DropDownList to populate those fields in the
FormView.
I'm connecting to the sql db with an ObjectDataSource.
I'm thinking I just need to put some VB code into the
SelectedIndexChanged event of the ddl, something like:
*something goes here* = DropDownList1.SelectedValue.ToString()
But I'm drawing a blank on what would go to the left of the equal
sign.
Thanks in advance for the help.myFormView.FindControl("myControlId") will find the control if it is in the
template the formview is going to present. Otherwise you may need to do
something like myFormView.EditItemTemplate.FindControl("myControlId").
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"mkidd" <michaelk@.volkcorp.com> wrote in message
news:19842b1a-bab5-4594-aaef-c1cf99efd809@.u10g2000prn.googlegroups.com...
> Hi,
> I'm trying to build an online form using asp.net and vb.net for the
> code behind in Visual Studio 2005.
> I want to fill 3 fields in a FormView that's connected to one table
> (table1) with information from another table (table2) by choosing the
> key field from table2 in a DropDownList.
> Table2 has 3 fields with info that doesn't change (name, phone# and
> email for about 5 customer service reps). I want each rep to choose
> their name in the DropDownList to populate those fields in the
> FormView.
> I'm connecting to the sql db with an ObjectDataSource.
> I'm thinking I just need to put some VB code into the
> SelectedIndexChanged event of the ddl, something like:
> *something goes here* = DropDownList1.SelectedValue.ToString()
> But I'm drawing a blank on what would go to the left of the equal
> sign.
> Thanks in advance for the help.
Thanks, but can you help me with what to do to write the data from the
DropDownList table to the FormView after the FindControl event? I'm
really new at this.
On Jan 14, 9:15 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@.mMvVpPsS.org> wrote:
> myFormView.FindControl("myControlId") will find the control if it is in th
e
> template the formview is going to present. Otherwise you may need to do
> something like myFormView.EditItemTemplate.FindControl("myControlId").
> --
> Eliyahu Goldin,
> Software Developer
> Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
> "mkidd" <micha...@.volkcorp.com> wrote in message
> news:19842b1a-bab5-4594-aaef-c1cf99efd809@.u10g2000prn.googlegroups.com...
>
>
>
>
>
>
>
>
>