This is how I am dynamically adding a table to a DataSet:
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
'Create an empty DataSet
Dim dSet As New DataSet
'Create a new table & add columns
Dim dTable As New DataTable("User s")
dTable.Columns. Add("ID", Type.GetType("S ystem.Int32"))
dTable.Columns. Add("First Name", Type.GetType("S ystem.String"))
dTable.Columns. Add("Last Name", Type.GetType("S ystem.String"))
dTable.Columns( "ID").AutoIncre ment = True
'Add the new table to the DataSet's Tables collection
dSet.Tables.Add (dTable)
'Define a Primary Key on the column named ID
Dim dColumn() As DataColumn =
{dSet.Tables("U sers").Columns( "ID")}
dSet.Tables("Us ers").PrimaryKe y = dColumn
'Add a row to this table
Dim dRow As DataRow = dTable.NewRow()
dRow("ID") = 1
dRow("First Name") = "Patrick"
dRow("Last Name") = "Patterson"
dTable.Rows.Add (dRow)
End Sub
</script>
The output of the above code is displayed in a DataGrid (the code for
which I haven't reproduced here). What I would like to know is like the
DataSet & the DataTable, DataRow is also a class but if the following
line
Dim dRow As DataRow = dTable.NewRow()
is replaced by
Dim dRow As New DataRow
then VWD generates the following error:
'System.Data.Da taRow.Protected Sub New(builder As
System.Data.Dat aRowBuilder)' is not accessible in this context because
it is 'Protected'.
The "New" keyword is used for creating a new instance of an object
(like New DataSet, New DataTable etc.) which, if I am not mistaken,
means that the "New" subs of the DataSet & DataTable objects aren't
protected but unlike the "New" subs of System.Data.Dat aSet &
System.Data.Dat aTable, why is the sub "New" of System.Data.Dat aRow
protected? Why aren't the "New" subs of System.Data.Dat aSet &
System.Data.Dat aTable not protected similarly? Or in other words, why
is the "New" sub of DataRow an exception as far as the scope is
concerned?
Moreover simply by using the keyword "NewRow" in the above code - does
that automatically create a new instance of the DataRow object?
I am not sure whether I have expressed myself lucidly enough!
Thanks,
Arpan 1 2807
If I recall how this works correctly, the first part of your code adds the
columns to the table which define the schema - other items such as foreign
key constraints can also be added.
By adding the columns and other constraints you are placing restrictions on
the type of data that can appear in the row. The table.newRow command creates
a row that possesses the schema of the table, ensuring that the data you
enter matches the structure of the table.
Is this okay? I'm pretty new to asp.net myself.
Chris
"Arpan" wrote:
This is how I am dynamically adding a table to a DataSet:
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
'Create an empty DataSet
Dim dSet As New DataSet
'Create a new table & add columns
Dim dTable As New DataTable("User s")
dTable.Columns. Add("ID", Type.GetType("S ystem.Int32"))
dTable.Columns. Add("First Name", Type.GetType("S ystem.String"))
dTable.Columns. Add("Last Name", Type.GetType("S ystem.String"))
dTable.Columns( "ID").AutoIncre ment = True
'Add the new table to the DataSet's Tables collection
dSet.Tables.Add (dTable)
'Define a Primary Key on the column named ID
Dim dColumn() As DataColumn =
{dSet.Tables("U sers").Columns( "ID")}
dSet.Tables("Us ers").PrimaryKe y = dColumn
'Add a row to this table
Dim dRow As DataRow = dTable.NewRow()
dRow("ID") = 1
dRow("First Name") = "Patrick"
dRow("Last Name") = "Patterson"
dTable.Rows.Add (dRow)
End Sub
</script>
The output of the above code is displayed in a DataGrid (the code for
which I haven't reproduced here). What I would like to know is like the
DataSet & the DataTable, DataRow is also a class but if the following
line
Dim dRow As DataRow = dTable.NewRow()
is replaced by
Dim dRow As New DataRow
then VWD generates the following error:
'System.Data.Da taRow.Protected Sub New(builder As
System.Data.Dat aRowBuilder)' is not accessible in this context because
it is 'Protected'.
The "New" keyword is used for creating a new instance of an object
(like New DataSet, New DataTable etc.) which, if I am not mistaken,
means that the "New" subs of the DataSet & DataTable objects aren't
protected but unlike the "New" subs of System.Data.Dat aSet &
System.Data.Dat aTable, why is the sub "New" of System.Data.Dat aRow
protected? Why aren't the "New" subs of System.Data.Dat aSet &
System.Data.Dat aTable not protected similarly? Or in other words, why
is the "New" sub of DataRow an exception as far as the scope is
concerned?
Moreover simply by using the keyword "NewRow" in the above code - does
that automatically create a new instance of the DataRow object?
I am not sure whether I have expressed myself lucidly enough!
Thanks,
Arpan
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Marcel Sottnik |
last post by:
Hello group
Maybe this is not the right NG, but this problem seems to me to be more
language related than ADO.NET.
How can I write the constructor for specialization of DataRow which will
take a datarow as parameter. Something like:
class MyDataRow : DataRow
{
|
by: Michael Carr |
last post by:
I have a function that populates a class with values from a database. I'd
like to pass into the function either a SqlDataReader or a DataRow,
depending on which mechanism I'm using to retrieve data from the database.
However, the two classes don't appear to have any common interfaces that
would allow me to enumerate the fields. Yet, when you databind you can pass
either of these classes (as well as many others) and .NET somehow knows how...
|
by: Steve Amey |
last post by:
Hi all
I am binding a DataRow to some controls on a form. When I've finished
editing the DataRow I click on a toolbar button to save the changes. If the
focus is on a particular control, the item in the DataRow does not update to
be the value of the control, for example, if I have a TextBox called
txtEntry which is bound to the Entry column of the DataRow, if the focus
remains on the TextBox while I click on the save button the...
|
by: JIM.H. |
last post by:
Hello
I have;
DataRow dr= dataSet11.myTable.NewRow();
And I am filling the fields of this datarow. Now I need to create a copy of
this row as drCopy and change a few fields and add both to the dataset, how
can I do this?
Thanks,
|
by: samoore33 |
last post by:
I use the code below to search through a DataSet:
Dim t As DataTable
t = result.Tables("State")
Dim strExpr As String
strExpr = "id = '" & theState.ToString() & "'"
Dim foundRows() As DataRow
foundRows = t.Select(strExpr)
This of course returns foundRows. My problem is that I need to return
| |
by: mcbobin |
last post by:
Hi,
Here's hoping someone can help...
I'm using a stored procedure to return a single row of data ie a
DataRow
e.g.
public static DataRow GetManualDailySplits(string prmLocationID, string
|
by: Rainer Queck |
last post by:
Hello NG,
what would be the best way to locate a DataRrow in a DataGridView?
I have by DataTable.Select a bunch of DataRows from a DataTable (which is
the data source to the DataGridView).
Now I would like to give those related DataGridViewRows a different
BackColor.
Thanks for hints and help!
Rainer
|
by: Ryan Liu |
last post by:
Is DataRow uses DataRow and
DataRow much efficient than DataRow?
Thanks,
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
Ryan Liu
Shanghai Fengpu Software Co. Ltd
Shanghai , China
|
by: myotheraccount |
last post by:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |