Putting a string onto a stream to be read into a DataGrid

  • Thread starter Thread starter James Norton-Jones
  • Start date Start date
J

James Norton-Jones

Hi,

Am I trying to hold the data of a DataGrid in a label so that when the form
is reposted the DataGrid can be repopulated. The problem I am having is that
I don't understand how to get the text into a stream in order to be able to
use DataSetOutcomes1.ReadXML(MyStream).

Thanks in advance,

James


Web Form:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="course_new.aspx.vb"
Inherits="_1137InsetBookingSystem.course_new"%>
<!-- #INCLUDE file="headeraspx.inc" -->
<!-- #INCLUDE file="tableheader.inc" -->
<form id="FormNewCourse" method="post" runat="server">
<table cellSpacing="0" cols="3" cellPadding="0" width="100%" border="0">
<tr>
<td vAlign="top"><asp:label id="lblCourseTitle" runat="server"
Font-Bold="True">Course Title</asp:label></td>
<td><asp:textbox id="txtCourseTitle" runat="server"></asp:textbox></td>
<td>&nbsp;</td>
</tr>
<tr>
<td colSpan="3"><asp:label id="lblCourseTutorList" runat="server"
Font-Bold="True">Course Tutor(s)</asp:label></td>
</tr>
<tr>
<td vAlign="top">&nbsp;</td>
<td><asp:textbox id="txtCourseTutor" runat="server"></asp:textbox></td>
<td><asp:button id="cmdAdd" runat="server" Text="Add" BackColor="#F6CD7A"
BorderStyle="Solid" BorderWidth="1px"
Width="80px"></asp:button></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:textbox id="txtTutorList" runat="server" Rows="5"
TextMode="MultiLine"></asp:textbox></td>
<td>&nbsp;</td>
</tr>
<tr>
<td colSpan="3"><asp:label id="lblLearningOutcomes" runat="server"
Font-Bold="True">Learning Outcomes</asp:label></td>
</TD></tr>
<tr>
<td>&nbsp;</td>
<td><asp:textbox id="txtOutcome" runat="server"></asp:textbox></td>
<td><asp:button id="cmdAddOutcome" runat="server" Text="Add"
BackColor="#F6CD7A" BorderStyle="Solid"
BorderWidth="1px" Width="80px"
CausesValidation="False"></asp:button></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:datagrid id=DataGridOutcomes runat="server" GridLines="None"
AutoGenerateColumns="False" DataMember="Outcomes" DataSource="<%#
DatasetOutcomes1 %>">
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<ul>
<li>
</li>
</ul>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Name" SortExpression="Name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="DisplayOrder"
SortExpression="DisplayOrder" HeaderText="DisplayOrder"></asp:BoundColumn>
<asp:ButtonColumn Text="Delete"
ButtonType="PushButton"></asp:ButtonColumn>
<asp:ButtonColumn Text="Edit"
ButtonType="PushButton"></asp:ButtonColumn>
</Columns>
</asp:datagrid></td>
<td>&nbsp;</td>
</tr>
<tr>
<td colSpan="3"><asp:label id="lblOutcomes"
runat="server"></asp:label></td>
</tr>
</table>
</form>
<!-- #INCLUDE file="tablefooter.inc" -->
<!-- #INCLUDE file="footer.inc" -->


Code Bind:

Imports System
Imports System.IO
Imports System.Xml


Public Class course_new
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.DatasetOutcomes1 = New _1137InsetBookingSystem.DatasetOutcomes
CType(Me.DatasetOutcomes1,
System.ComponentModel.ISupportInitialize).BeginInit()
'
'DatasetOutcomes1
'
Me.DatasetOutcomes1.DataSetName = "DatasetOutcomes"
Me.DatasetOutcomes1.Locale = New
System.Globalization.CultureInfo("en-GB")
CType(Me.DatasetOutcomes1,
System.ComponentModel.ISupportInitialize).EndInit()

End Sub
Protected WithEvents lblCourseTitle As System.Web.UI.WebControls.Label
Protected WithEvents txtCourseTitle As System.Web.UI.WebControls.TextBox
Protected WithEvents txtCourseTutor As System.Web.UI.WebControls.TextBox
Protected WithEvents txtTutorList As System.Web.UI.WebControls.TextBox
Protected WithEvents cmdAdd As System.Web.UI.WebControls.Button
Protected WithEvents txtOutcome As System.Web.UI.WebControls.TextBox
Protected WithEvents cmdAddOutcome As System.Web.UI.WebControls.Button
Protected WithEvents lblCourseTutorList As
System.Web.UI.WebControls.Label
Protected WithEvents lblLearningOutcomes As
System.Web.UI.WebControls.Label
Protected WithEvents DataGridOutcomes As
System.Web.UI.WebControls.DataGrid
Protected WithEvents DatasetOutcomes1 As
_1137InsetBookingSystem.DatasetOutcomes
Protected WithEvents lblOutcomes As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Public gstrTableTitle As String = "Create Course"
Public gstrTableAttribs As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
Me.DataGridOutcomes.DataBind()
Else
Dim str As String = Me.lblOutcomes.Text
Dim MyStream As StreamReader

If str.Length > 0 Then
MyStream = New StreamReader(str)
Me.DatasetOutcomes1.ReadXml(MyStream)

End If



'Me.DatasetOutcomes1.GetXml()
'

End If
End Sub

Private Sub UpdateOutcomes()
Me.DataGridOutcomes.DataSource = Me.DatasetOutcomes1
Me.DataGridOutcomes.DataBind()
End Sub

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAdd.Click

If InStr(Me.txtTutorList.Text, Me.txtCourseTutor.Text) < 1 Then
If Me.txtTutorList.Text.Length > 0 Then
Me.txtTutorList.Text = Me.txtTutorList.Text & ";" & vbCrLf &
Me.txtCourseTutor.Text
Else
Me.txtTutorList.Text = Me.txtCourseTutor.Text
End If
End If
End Sub

Private Sub cmdAddOutcome_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdAddOutcome.Click
Dim dr As DatasetOutcomes.OutcomesRow

dr = Me.DatasetOutcomes1.Outcomes.NewRow()
dr.Item("Name") = Me.txtOutcome.Text
dr.Item("DisplayOrder") = Me.DatasetOutcomes1.Outcomes.Rows.Count
'DatasetOutcomes1.Outcomes.Rows.InsertAt(dr,
Me.DatasetOutcomes1.Outcomes.Rows.Count)

DatasetOutcomes1.Outcomes.Rows.Add(dr)
Me.DataGridOutcomes.DataBind()

Me.lblOutcomes.Text = Me.DatasetOutcomes1.GetXml

End Sub

End Class
 
Most of the time, your code can follow this pattern:

Private Sub Page_Load(ByVal sender As System.Object, ByVal a As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
myDataSet = GetDataSetSomeHow()
myDataGrid.DataSource = myDataSet
myDataGrid.DataMember = "FirstTable"

DataBind()
End If
End Sub

When the page is posted back, the grid (or other controls) will populate
itself from ViewState.
 
DatasetCourse is linked to an xml schema to define the data structure and
the data its self is an xml doc. There is no database behind it, hence the
need to repopulate from the xml data on postback.


Schema:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="DatasetOutcomes"
targetNamespace="http://tempuri.org/DatasetOutcomes.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns="http://http://localhost/1137InsetBookingSystem/DatasetOutcomes.xsd"
xmlns:mstns="http://tempuri.org/DatasetOutcomes.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="DatasetOutcomes" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Outcomes">
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string" minOccurs="0" />
<xs:element name="DisplayOrder" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="DatasetOutcomesKey1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:Outcomes" />
<xs:field xpath="mstns:Description" />
</xs:key>
</xs:element>
</xs:schema>
 
Ok, I don't think I've ever used an updateable XML file yet, so I didn't see
that. In order to update even a single row, you have to read in the entire
file.

Please pardon me for being dense, was your question ever actually answered?
:-)

If I were you, I think I'd store DatasetCourse in Session state. On the
other hand, if you're going to be making multiple changes to the data per
session, I think I'd keep it in Session, but flush to disk whenever a change
was made. That way, if the session expires (seen as Session("DatasetCourse")
Is Nothing), then you can re-load it from the disk file (which you'll need
to uniquely name, without resorting to the Session ID!)
 
Runs on multiple servers - and no Sql Server database to store session state
in.
 
It will eventually be written to a SQL DB but each of the data grid items
will relate to the new course record. As such the course details need top be
completed on the form.Once all the course details have been captured the
data will be split into several tables. Each of the outcome rows will relate
to one course record and will be held in a table call tblCourseOutcomes. I
did want to get into a situation of create temp record in the DB.
 
Oh, if there is a Sql Server database, had you considered using the
"SQLServer" mode of the <sessionState> element in web.config? I haven't
tried it - perhaps it's too slow.
 
To be honest I didn't know it existed, I'll look into it thanks. It would
certainly reduce the HTML payload.
 
Back
Top