Simple 2.0 dataset?

J

Justin

I'm just learning ASP.NET 2.0 and everything I find on Datasets including
the MSPress book I bought go way to far for what I need. I do not need to
bind a datasource. My source will be a text file that I will read line by
line.

All I want to do is create a dataset and add records to it manualy that I
will read from a text file. To start I'm just trying to add a single
record. I then want to bind that dataset to a gridview.

To start is this correct for far:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Dim ds As Data.DataSet = New Data.DataSet()

GridView1.DataSource = ds
GridView1.DataBind()
End Sub


How do I add records to it?

Thanks for any help
 
K

Ken Cox [Microsoft MVP]

Hi Justin,

Here's a quick sample .aspx page that creates a dataset in code, adds a few
lines of data and persists the dataset to a file. When you click the button,
it loads the dataset from the file and displays it in the grid.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<%@ import namespace="system.data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Create a datatable and populate it
If Not IsPostBack Then
Dim ds As Data.DataSet = _
New Data.DataSet()
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
' Add the table to the dataset
ds.Tables.Add(dt)
' Write the dataset to a file
ds.WriteXml(Server.MapPath("app_data/ds.xsd"), _
Data.XmlWriteMode.WriteSchema)
End If
End Sub

Protected Sub Button1_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Create an empty dataset
Dim ds As New DataSet
' Read the dataset from the file
ds.ReadXml(Server.MapPath("app_data/ds.xsd"), _
XmlReadMode.ReadSchema)
' Bind to the Gridview
If Not IsNothing(ds) Then
GridView1.DataSource = ds
GridView1.DataBind()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Create a DataSet in code and read it</title>
</head>
<body>
<form id="form1" runat="server">
<div>
DataSet sample by Ken Cox - Microsoft MVP [ASP.NET]<br />
<br />
<asp:gridview id="GridView1" runat="server">
</asp:gridview>
<br />
&nbsp;<asp:button id="Button1" runat="server"
onclick="Button1_Click" text="Use DataSet From File" /></div>
</form>
</body>
</html>
 
J

Justin

This has been very very very helpful. Thank you very much!!!

During all of my searching I have found nothing on datatable and datarow.
Those where two huge missing puzzle pieces. ASP.NET 2005 Edition Step by
Step makes no reference of these. I thought it might be too basic. I
couldn't find anything of the same caliber as the Programming Microsoft
ASP.NET 1.1 book I have. I tried to learn from that while using VS2005 and
I was ripping my hair out. Too many differences.

I'm still trying to get your code to write the XML file. I'm thinking
that's a site issue. However I have two things to bring up.

1. My MSPress resource says IMPORT is no longer supported???
2. I found these bugs:

Dim ds As New Data.DataSet
ds.ReadXml(Server.MapPath("app_data/ds.xsd"), Data.XmlReadMode.ReadSchema)

Both lines where missing "Data."

Thank you again you certainly got me on the right path now. Can you
recommend a desent resource (book)? This step by step one was a waste of 40
bucks :(
 
J

Justin

I figured out the problem with it not writing the file. The page isn't
doing anything inside page_load. I moved all the code to another Sub
(button) and when I click the new button it works fine. Why wouldn't it run
the code within page_load on it's own?
 
K

Ken Cox [Microsoft MVP]

Hi Justin,

Glad to help!

You would have to prefix with Data. if you didn't include the import
reference

import namespace="system.data"

As for a book, ASP.NET 2.0 by Stephen Walther is good. I'd recommend ASP.NET
2.0 All-in-one Desk Reference for Dummies but I didn't write the data
minibooks. <grin>

Ken
Microsoft MVP [ASP.NET]
 

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,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top