473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Simple 2.0 dataset?

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
Aug 21 '06 #1
4 1944
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>
"Justin" <Ju****@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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

Aug 22 '06 #2
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 :(
Aug 23 '06 #3
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?
Aug 23 '06 #4
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]
"Justin" <Ju****@NoSpam.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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 :(

Aug 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tom | last post by:
hi I have a simple question. I am writing a simple remoting app.. basically I have client/server and a serialised service class.. the problem is I am doing some db work on the server and I want...
7
by: LeROY | last post by:
I need to output some related data tables as XML. I have my data adapters and my dataset with the relations defined. It is certainly simple enough to use the dataset.WriteXML function. However,...
0
by: Pete Beech | last post by:
Hi, I've looked all over for any information about this, and either this is a bug that I cannot find reported or I've misunderstood something. Lets say, in the XML Designer in VS.NET 2003, you...
2
by: Ivan Simurina | last post by:
Hi I would appreciate being clarified simple peace of code, protected void ShowDailyEvents() { DateTime d = MyCalendar.SelectedDate; DataSet dataSet = LoadMyCalendarData(); if (dataSet ==...
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
5
by: Lasse Edsvik | last post by:
Hello Im trying to create a simple testclass that connects to a db on localhost and a method that returns a dataset. I get these errors: Unhandled Exception: System.InvalidOperationException:...
5
by: Stephanie_Stowe | last post by:
Hi. I am trying to get used to AS.NET. I have been doing ASP classic for years, and am now in a position to do ASP.NET. I am in the stumbling around until I get my bearings phase. I hope you will...
0
by: Pietje puk | last post by:
Hello, Since im quite new to ASP.NET i wanted to ask you folks what the best way is to create a WebForm for modifying 1 field from a record. The manipulation of this field can be done by using...
6
by: Arne Beruldsen | last post by:
I have a very simple Access data base. No new info is going to be added...the only changes are to existing fields. I have 2 tables both with one row each. I'm using vb.net. I can easily...
5
by: Mark Chambers | last post by:
Hi there, Can anyone explain the following (very) simple scenario. 1) I make an exact copy of my "DataSet" and delete one record from a given table (in the copy) 2) I invoke...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.