datagrid - edit problem

W

wolfgang wagner

hi all!

im trying to edit data in a datagrid (like described here:
http://aspnet.4guysfromrolla.com/articles/071002-1.aspx) but if i click
the edit button i get the following error

invalid attempt to FieldCount because datareader is already closed
(sorry, this error message is translated from german)

when trying to do the databinding.

heres my code:

page.aspx
<asp:datagrid id="dgSoftware" style="Z-INDEX: 102; LEFT: 23px; POSITION:
absolute; TOP: 68px"
runat="server" Height="235px" Width="856px"
AutoGenerateColumns="False" DataKeyField="sw_ref" GridLines="None"
CellPadding="3" BackColor="White"
BorderWidth="2px" CellSpacing="1" BorderStyle="Ridge"
BorderColor="White">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="sw_bez_str"
HeaderText="Bezeichnung"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_herst_str"
HeaderText="Hersteller"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_filesys_str"
HeaderText="Dateisystem"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Softwareart">
<ItemTemplate>
<%# getHtswtyp(DataBinder.Eval(Container.DataItem,
"htswtyp_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownSwref" Runat="server"
DataTextField="htswtyp_name_str" DataValueField="htswtyp_ref"
DataSource='<%# getSwtyp() %>' SelectedIndex='<%#
GetSelIndexSwart(DataBinder.Eval(Container.DataItem, "htswtyp_ref")) %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="sw_bem_str"
HeaderText="Bemerkung"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="zugehörige Hardware">
<ItemTemplate>
<%# getHwref(DataBinder.Eval(Container.DataItem,
"hw_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownHwref" Runat="server"
DataTextField="id" DataValueField="hwId" DataSource='<%# getRechner()%>'
SelectedIndex='<%#
GetSelIndexRechner(DataBinder.Eval(Container.DataItem, "hw_ref")) %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Aktualisieren" CancelText="Abbrechen"
EditText="Bearbeiten"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Löschen"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
Position="TopAndBottom" BackColor="#C6C3C6"></PagerStyle>
</asp:datagrid>


codebehind code:
....
private void Page_Load(object sender, System.EventArgs e)
{
// Hier Benutzercode zur Seiteninitialisierung einfügen
this.strDbConn = ConfigurationSettings.AppSettings["connString"];
this.strDownloadKennung =
ConfigurationSettings.AppSettings["downloadKennung"];
//this.iId = (int) Session.Get("id");
alHwId = new ArrayList((ArrayList) Session["alHwId"]);
this.iId = 1231;
this.datAktDatum = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");

//datatable mit id/hwid erzeugen
DataColumn dcTemp;
DataRow drTemp;

dcTemp = new DataColumn();
dcTemp.DataType = System.Type.GetType("System.Int32");
dcTemp.ColumnName = "id";
dtHwid.Columns.Add(dcTemp);

dcTemp = new DataColumn();
dcTemp.DataType = System.Type.GetType("System.Int32");
dcTemp.ColumnName = "hwId";
dtHwid.Columns.Add(dcTemp);

ArrayList alIds = (ArrayList)Session["alHwId"];
int[][] iIds = new int[alIds.Count][];
for (int i = 0; i < alIds.Count; i++)
{
drTemp = dtHwid.NewRow();
drTemp["id"] = i+1;
drTemp["hwId"] = Convert.ToInt32(alIds.ToString());

dtHwid.Rows.Add(drTemp);
}
//ENDE datatable

if (!IsPostBack)
{
showDataSoftware();
showDataHardware();
}
}
....
private void showDataSoftware()
{
string strWherebuilder = "";
for (int i =0; i < this.alHwId.Count; i++)
{
if (this.alHwId.Count == 1 || i == (this.alHwId.Count-1) )
strWherebuilder += this.alHwId.ToString();
else strWherebuilder += this.alHwId.ToString() + " OR hw_ref
= ";
}
string query = "SELECT * FROM sw WHERE hw_ref = " + strWherebuilder;

//DEBUG
this.tbDebug.Text += "QUERY: " + query;

this.sqlConn = new SqlConnection(this.strDbConn);
this.sqlAdapter = new SqlDataAdapter(query, this.sqlConn);
this.sqlConn.Open();
this.dsDaten = new DataSet();
this.sqlAdapter.Fill(this.dsDaten, "sw" );

//this.createList(query);

//neue zeile einfuegen
DataRow BlankRow = this.dsDaten.Tables["sw"].NewRow( );
BlankRow[1] = "Bitte Wert eingeben!";
this.dsDaten.Tables[ "sw" ].Rows.InsertAt( BlankRow, 0 );

//daten binden
this.dgSoftware.DataSource = this.dsDaten;
this.dgSoftware.DataBind(); <----------error occurs here!!!!
this.sqlConn.Close();
}
....

does anyone know what could be the problem???

thx for help in advance!

greets
wolfgang
 
R

Riki

wolfgang said:
hi all!

im trying to edit data in a datagrid (like described here:
http://aspnet.4guysfromrolla.com/articles/071002-1.aspx) but if i
click the edit button i get the following error

invalid attempt to FieldCount because datareader is already closed
(sorry, this error message is translated from german)

when trying to do the databinding.

heres my code:

page.aspx
<asp:datagrid id="dgSoftware" style="Z-INDEX: 102; LEFT: 23px;
POSITION: absolute; TOP: 68px"
runat="server" Height="235px" Width="856px"
AutoGenerateColumns="False" DataKeyField="sw_ref" GridLines="None"
CellPadding="3" BackColor="White"
BorderWidth="2px" CellSpacing="1" BorderStyle="Ridge"
BorderColor="White">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black"
BackColor="#C6C3C6"></FooterStyle> <Columns>
<asp:BoundColumn DataField="sw_bez_str"
HeaderText="Bezeichnung"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_herst_str"
HeaderText="Hersteller"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_filesys_str"
HeaderText="Dateisystem"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Softwareart">
<ItemTemplate>
<%# getHtswtyp(DataBinder.Eval(Container.DataItem,
"htswtyp_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownSwref" Runat="server"
DataTextField="htswtyp_name_str" DataValueField="htswtyp_ref"
DataSource='<%# getSwtyp() %>' SelectedIndex='<%#
GetSelIndexSwart(DataBinder.Eval(Container.DataItem, "htswtyp_ref"))
%>' /> </EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="sw_bem_str"
HeaderText="Bemerkung"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="zugehörige Hardware">
<ItemTemplate>
<%# getHwref(DataBinder.Eval(Container.DataItem,
"hw_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownHwref" Runat="server"
DataTextField="id" DataValueField="hwId" DataSource='<%#
getRechner()%>' SelectedIndex='<%#
GetSelIndexRechner(DataBinder.Eval(Container.DataItem, "hw_ref")) %>'
/> </EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Aktualisieren" CancelText="Abbrechen"
EditText="Bearbeiten"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Löschen"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
Position="TopAndBottom" BackColor="#C6C3C6"></PagerStyle>
</asp:datagrid>


codebehind code:
...
private void Page_Load(object sender, System.EventArgs e)
{
// Hier Benutzercode zur Seiteninitialisierung einfügen
this.strDbConn =
ConfigurationSettings.AppSettings["connString"];
this.strDownloadKennung =
ConfigurationSettings.AppSettings["downloadKennung"];
//this.iId = (int) Session.Get("id"); alHwId = new
ArrayList((ArrayList) Session["alHwId"]); this.iId = 1231;
this.datAktDatum = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");

//datatable mit id/hwid erzeugen
DataColumn dcTemp;
DataRow drTemp;

dcTemp = new DataColumn();
dcTemp.DataType = System.Type.GetType("System.Int32");
dcTemp.ColumnName = "id";
dtHwid.Columns.Add(dcTemp);

dcTemp = new DataColumn();
dcTemp.DataType = System.Type.GetType("System.Int32");
dcTemp.ColumnName = "hwId";
dtHwid.Columns.Add(dcTemp);

ArrayList alIds = (ArrayList)Session["alHwId"];
int[][] iIds = new int[alIds.Count][];
for (int i = 0; i < alIds.Count; i++)
{
drTemp = dtHwid.NewRow();
drTemp["id"] = i+1;
drTemp["hwId"] = Convert.ToInt32(alIds.ToString());

dtHwid.Rows.Add(drTemp);
}
//ENDE datatable

if (!IsPostBack)
{
showDataSoftware();
showDataHardware();
}
}
...
private void showDataSoftware()
{
string strWherebuilder = "";
for (int i =0; i < this.alHwId.Count; i++)
{
if (this.alHwId.Count == 1 || i == (this.alHwId.Count-1) )
strWherebuilder += this.alHwId.ToString();
else strWherebuilder += this.alHwId.ToString() + " OR
hw_ref = ";
}
string query = "SELECT * FROM sw WHERE hw_ref = " +
strWherebuilder;
//DEBUG
this.tbDebug.Text += "QUERY: " + query;

this.sqlConn = new SqlConnection(this.strDbConn);
this.sqlAdapter = new SqlDataAdapter(query, this.sqlConn);
this.sqlConn.Open();
this.dsDaten = new DataSet();
this.sqlAdapter.Fill(this.dsDaten, "sw" );

//this.createList(query);

//neue zeile einfuegen
DataRow BlankRow = this.dsDaten.Tables["sw"].NewRow( );
BlankRow[1] = "Bitte Wert eingeben!";
this.dsDaten.Tables[ "sw" ].Rows.InsertAt( BlankRow, 0 );

//daten binden
this.dgSoftware.DataSource = this.dsDaten;
this.dgSoftware.DataBind(); <----------error occurs here!!!!
this.sqlConn.Close();
}
...

does anyone know what could be the problem???

thx for help in advance!

greets
wolfgang


The problem is probably that you're trying to set the datasource
of your dropdownlist (getRechner()), while the datagrid is still
being filled.

Instead of setting the datasource in a databinding expression,
try to do it from ItemDataBound. It will occur after databinding,
and it will not cause a conflict anymore.
 
W

wolfgang wagner

Riki said:
wolfgang said:
hi all!

im trying to edit data in a datagrid (like described here:
http://aspnet.4guysfromrolla.com/articles/071002-1.aspx) but if i
click the edit button i get the following error

invalid attempt to FieldCount because datareader is already closed
(sorry, this error message is translated from german)

when trying to do the databinding.

heres my code:

page.aspx
<asp:datagrid id="dgSoftware" style="Z-INDEX: 102; LEFT: 23px;
POSITION: absolute; TOP: 68px"
runat="server" Height="235px" Width="856px"
AutoGenerateColumns="False" DataKeyField="sw_ref" GridLines="None"
CellPadding="3" BackColor="White"
BorderWidth="2px" CellSpacing="1" BorderStyle="Ridge"
BorderColor="White">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black"
BackColor="#C6C3C6"></FooterStyle> <Columns>
<asp:BoundColumn DataField="sw_bez_str"
HeaderText="Bezeichnung"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_herst_str"
HeaderText="Hersteller"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_filesys_str"
HeaderText="Dateisystem"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Softwareart">
<ItemTemplate>
<%# getHtswtyp(DataBinder.Eval(Container.DataItem,
"htswtyp_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownSwref" Runat="server"
DataTextField="htswtyp_name_str" DataValueField="htswtyp_ref"
DataSource='<%# getSwtyp() %>' SelectedIndex='<%#
GetSelIndexSwart(DataBinder.Eval(Container.DataItem, "htswtyp_ref"))
%>' /> </EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="sw_bem_str"
HeaderText="Bemerkung"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="zugehörige Hardware">
<ItemTemplate>
<%# getHwref(DataBinder.Eval(Container.DataItem,
"hw_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownHwref" Runat="server"
DataTextField="id" DataValueField="hwId" DataSource='<%#
getRechner()%>' SelectedIndex='<%#
GetSelIndexRechner(DataBinder.Eval(Container.DataItem, "hw_ref")) %>'
/> </EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Aktualisieren" CancelText="Abbrechen"
EditText="Bearbeiten"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Löschen"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
Position="TopAndBottom" BackColor="#C6C3C6"></PagerStyle>
</asp:datagrid>


codebehind code:
...
private void Page_Load(object sender, System.EventArgs e)
{
// Hier Benutzercode zur Seiteninitialisierung einfügen
this.strDbConn =
ConfigurationSettings.AppSettings["connString"];
this.strDownloadKennung =
ConfigurationSettings.AppSettings["downloadKennung"];
//this.iId = (int) Session.Get("id"); alHwId = new
ArrayList((ArrayList) Session["alHwId"]); this.iId = 1231;
this.datAktDatum = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");

//datatable mit id/hwid erzeugen
DataColumn dcTemp;
DataRow drTemp;

dcTemp = new DataColumn();
dcTemp.DataType = System.Type.GetType("System.Int32");
dcTemp.ColumnName = "id";
dtHwid.Columns.Add(dcTemp);

dcTemp = new DataColumn();
dcTemp.DataType = System.Type.GetType("System.Int32");
dcTemp.ColumnName = "hwId";
dtHwid.Columns.Add(dcTemp);

ArrayList alIds = (ArrayList)Session["alHwId"];
int[][] iIds = new int[alIds.Count][];
for (int i = 0; i < alIds.Count; i++)
{
drTemp = dtHwid.NewRow();
drTemp["id"] = i+1;
drTemp["hwId"] = Convert.ToInt32(alIds.ToString());

dtHwid.Rows.Add(drTemp);
}
//ENDE datatable

if (!IsPostBack)
{
showDataSoftware();
showDataHardware();
}
}
...
private void showDataSoftware()
{
string strWherebuilder = "";
for (int i =0; i < this.alHwId.Count; i++)
{
if (this.alHwId.Count == 1 || i == (this.alHwId.Count-1) )
strWherebuilder += this.alHwId.ToString();
else strWherebuilder += this.alHwId.ToString() + " OR
hw_ref = ";
}
string query = "SELECT * FROM sw WHERE hw_ref = " +
strWherebuilder;
//DEBUG
this.tbDebug.Text += "QUERY: " + query;

this.sqlConn = new SqlConnection(this.strDbConn);
this.sqlAdapter = new SqlDataAdapter(query, this.sqlConn);
this.sqlConn.Open();
this.dsDaten = new DataSet();
this.sqlAdapter.Fill(this.dsDaten, "sw" );

//this.createList(query);

//neue zeile einfuegen
DataRow BlankRow = this.dsDaten.Tables["sw"].NewRow( );
BlankRow[1] = "Bitte Wert eingeben!";
this.dsDaten.Tables[ "sw" ].Rows.InsertAt( BlankRow, 0 );

//daten binden
this.dgSoftware.DataSource = this.dsDaten;
this.dgSoftware.DataBind(); <----------error occurs here!!!!
this.sqlConn.Close();
}
...

does anyone know what could be the problem???

thx for help in advance!

greets
wolfgang



The problem is probably that you're trying to set the datasource
of your dropdownlist (getRechner()), while the datagrid is still
being filled.

Instead of setting the datasource in a databinding expression,
try to do it from ItemDataBound. It will occur after databinding,
and it will not cause a conflict anymore.

thx a lot!!
getting rind of the datasource and selectedindex code in the aspx page
make it running.
but now i do have the problem that i cannot select indexes of the
dropdownlist!
how am i able to select an index of a dropdownlist in edit mode??

wolfgang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top