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>
<aspropDownList 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>
<aspropDownList 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
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>
<aspropDownList 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>
<aspropDownList 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