473,325 Members | 2,342 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,325 software developers and data experts.

C# ADO.NET Question

I can use some help here.

I have been moving from Delphi/C++ Win32 Database Development (ADO) to
C#.Net /ADO.NET the language is not the problem ADO.NET is however
kicking my butt.

I finally get the true power and purpose of a Dataset, wow, that was a
light bulb Not easy when you are still thinking ADO RecordSets almost
got the basics down, , NOT! From what i have read so far, this is a
hurdle for lots of coders.

I have developed a tiny little DB program against SQL Server to test my
skills and all works fine, then I try the most mundane and simple of
tasks and realize I have no clue what I'm doing.

The Simple Problem (Made Small):

I have a DataGrid and a Button I click the button and I want it to
update the current record (Currently Selected in the Grid) Lets say,
update the Field/Colum Named "Price" to 19.95

Old School Delphi

MyTable.Edit;
MyTable.FieldByName("PRICE").Value := 19.95
MyTable.Post;

// ^This would have edited the CURRENT Record.

The Question
How do I Access the "Current" Active Record in a DataGrid
Programmatically and Update one of the fields Programmatically
My Guess is something Like
MyDataSet.Tables[0].SOMETHING // IM LOST HERE

By Asking this question I hope to:
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

I am a good coder, i just need the light bulb to go on- I need that
moment to happen.

My Head is Stuck In something like this:

MyDataSet.Tables[0].Edit;
MyDataSet.Tables[0].Field("PRICE") = 19.95
MyDataSet.Tables[0].Post;

Oct 15 '06 #1
7 2105
Light bulb just went off.. or ON as it were! Thank you all anyway! Any
pointers for my .Net ventures are always welcome by email!

Oct 15 '06 #2
Hi DaBrain,

and welcome to the C# world!

I guess you already got your troubles solved, but since I had already
written this post, here goes anyway in case somebody else is looking for the
same info.
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
You didn't state whether you are using .NET 1.1 or .NET 2.0, since .NET 2.0
has made things somewhat easier, but here's generic view of things.

Firstly, unlike in Delphi, ADO.NET datasets don't have the concept of
"cursors". A C# DataSet is collection of data represented as tables
(DataTable objects). If you know the Delphi TClientDataSet object, then you
can roughly compare a TClientDataSet to a C# DataTable. A DataSet is a
collection of those, i.e. a "set of data".

You can think a DataTable a large table of columns and rows. Unlike in
Delphi, you can access each row with a simple index, from zero to count-1.
To change, say, the fourth row, you could say something like:

myDataSet.Tables["Customers"].Rows[3]["ContactName"] = "Peach";

The thinking goes always first by row, then by column. Remember also, that
just like a Delphi TClientDataSet, a C# DataSet is an in-memory database. So
when you edit the data it contains, the changes are not yet written to a
database (the so-called "disconnected architecture"). Instead, you often use
the Update method of the TableAdapter that you used to fill the dataset from
the database.

Note also that just like in Delphi, you are better of using so-called typed
datasets instead of untyped ones. Using typed datasets gives you
compile-time type checking and cleaner code.

Since there really is a ton of stuff you have to learn (unfortunately or
not), and more than I can write in one post, I suggest that you take a look
at the Microsoft QuickStart Tutorials available here (written for .NET 1.1):

http://samples.gotdotnet.com/quickstart/howto/

At the top, click to "Common Tasks" and then on the left, to "Data and
ADO.NET". Also, try the link "WinForms" at the top, and then follow the
topics under the "Data Access Overview" topic under "Building Applications"
on the left.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet
In Delphi, the regular TADOTable and TClientDataSet components know their
"current record", but in .NET, this thing is a bit more complex. What you
have to understand is the concepts of binding managers and currency
managers. Don't be fooled with the term "currency", though. It has nothing
to do with money!

In C#, a currency manager holds the index of the "current record" you are
looking for. Here's a nice article about these topics:

http://www.akadia.com/services/dotnet_databinding.html

A currency manager object has a Position property, which is an integer value
that you can use to access the correct row inside the DataTable inside your
DataSet. It's all neatly described in the above document.

Good luck!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Oct 15 '06 #3
DataSets and DataTables are not appropriate for many purposes.
It sounds to me like SqlDataReader might be more appropriate (and probably
more efficient).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"DaBrain" wrote:
I can use some help here.

I have been moving from Delphi/C++ Win32 Database Development (ADO) to
C#.Net /ADO.NET the language is not the problem ADO.NET is however
kicking my butt.

I finally get the true power and purpose of a Dataset, wow, that was a
light bulb Not easy when you are still thinking ADO RecordSets almost
got the basics down, , NOT! From what i have read so far, this is a
hurdle for lots of coders.

I have developed a tiny little DB program against SQL Server to test my
skills and all works fine, then I try the most mundane and simple of
tasks and realize I have no clue what I'm doing.

The Simple Problem (Made Small):

I have a DataGrid and a Button I click the button and I want it to
update the current record (Currently Selected in the Grid) Lets say,
update the Field/Colum Named "Price" to 19.95

Old School Delphi

MyTable.Edit;
MyTable.FieldByName("PRICE").Value := 19.95
MyTable.Post;

// ^This would have edited the CURRENT Record.

The Question
How do I Access the "Current" Active Record in a DataGrid
Programmatically and Update one of the fields Programmatically
My Guess is something Like
MyDataSet.Tables[0].SOMETHING // IM LOST HERE

By Asking this question I hope to:
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

I am a good coder, i just need the light bulb to go on- I need that
moment to happen.

My Head is Stuck In something like this:

MyDataSet.Tables[0].Edit;
MyDataSet.Tables[0].Field("PRICE") = 19.95
MyDataSet.Tables[0].Post;

Oct 15 '06 #4
Jani,

Nice written including the currency. To add for the OP because you did that
so well, don't be fooled with the term AcceptChanges. It does not accept the
changes. They are always accepted at a rowchange, AcceptChanges set the
rowstate to nonchanged.

Cor

"Jani Järvinen [MVP]" <ja***@removethis.dystopia.fischreef in bericht
news:uP**************@TK2MSFTNGP02.phx.gbl...
Hi DaBrain,

and welcome to the C# world!

I guess you already got your troubles solved, but since I had already
written this post, here goes anyway in case somebody else is looking for
the same info.
>1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.

You didn't state whether you are using .NET 1.1 or .NET 2.0, since .NET
2.0 has made things somewhat easier, but here's generic view of things.

Firstly, unlike in Delphi, ADO.NET datasets don't have the concept of
"cursors". A C# DataSet is collection of data represented as tables
(DataTable objects). If you know the Delphi TClientDataSet object, then
you can roughly compare a TClientDataSet to a C# DataTable. A DataSet is a
collection of those, i.e. a "set of data".

You can think a DataTable a large table of columns and rows. Unlike in
Delphi, you can access each row with a simple index, from zero to count-1.
To change, say, the fourth row, you could say something like:

myDataSet.Tables["Customers"].Rows[3]["ContactName"] = "Peach";

The thinking goes always first by row, then by column. Remember also, that
just like a Delphi TClientDataSet, a C# DataSet is an in-memory database.
So when you edit the data it contains, the changes are not yet written to
a database (the so-called "disconnected architecture"). Instead, you often
use the Update method of the TableAdapter that you used to fill the
dataset from the database.

Note also that just like in Delphi, you are better of using so-called
typed datasets instead of untyped ones. Using typed datasets gives you
compile-time type checking and cleaner code.

Since there really is a ton of stuff you have to learn (unfortunately or
not), and more than I can write in one post, I suggest that you take a
look at the Microsoft QuickStart Tutorials available here (written for
.NET 1.1):

http://samples.gotdotnet.com/quickstart/howto/

At the top, click to "Common Tasks" and then on the left, to "Data and
ADO.NET". Also, try the link "WinForms" at the top, and then follow the
topics under the "Data Access Overview" topic under "Building
Applications" on the left.
>2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

In Delphi, the regular TADOTable and TClientDataSet components know their
"current record", but in .NET, this thing is a bit more complex. What you
have to understand is the concepts of binding managers and currency
managers. Don't be fooled with the term "currency", though. It has nothing
to do with money!

In C#, a currency manager holds the index of the "current record" you are
looking for. Here's a nice article about these topics:

http://www.akadia.com/services/dotnet_databinding.html

A currency manager object has a Position property, which is an integer
value that you can use to access the correct row inside the DataTable
inside your DataSet. It's all neatly described in the above document.

Good luck!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/


Oct 15 '06 #5
David,

Why? They both have their own purpose. Just read the message from Jani to
learn something about it.
In most situations is the dataset/datatable concept the best to use, their
are in my opinion situations like classic batch processing were the
SQLDataReader is better in its place.

Cor

"David Anton" <Da********@discussions.microsoft.comschreef in bericht
news:F4**********************************@microsof t.com...
DataSets and DataTables are not appropriate for many purposes.
It sounds to me like SqlDataReader might be more appropriate (and probably
more efficient).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"DaBrain" wrote:
>I can use some help here.

I have been moving from Delphi/C++ Win32 Database Development (ADO) to
C#.Net /ADO.NET the language is not the problem ADO.NET is however
kicking my butt.

I finally get the true power and purpose of a Dataset, wow, that was a
light bulb Not easy when you are still thinking ADO RecordSets almost
got the basics down, , NOT! From what i have read so far, this is a
hurdle for lots of coders.

I have developed a tiny little DB program against SQL Server to test my
skills and all works fine, then I try the most mundane and simple of
tasks and realize I have no clue what I'm doing.

The Simple Problem (Made Small):

I have a DataGrid and a Button I click the button and I want it to
update the current record (Currently Selected in the Grid) Lets say,
update the Field/Colum Named "Price" to 19.95

Old School Delphi

MyTable.Edit;
MyTable.FieldByName("PRICE").Value := 19.95
MyTable.Post;

// ^This would have edited the CURRENT Record.

The Question
How do I Access the "Current" Active Record in a DataGrid
Programmatically and Update one of the fields Programmatically
My Guess is something Like
MyDataSet.Tables[0].SOMETHING // IM LOST HERE

By Asking this question I hope to:
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

I am a good coder, i just need the light bulb to go on- I need that
moment to happen.

My Head is Stuck In something like this:

MyDataSet.Tables[0].Edit;
MyDataSet.Tables[0].Field("PRICE") = 19.95
MyDataSet.Tables[0].Post;


Oct 15 '06 #6
Well, DataReader is better if you're using the DB primarily for
serialization are handling all the relational-stuff DB-side... if
you're mapping the resulting rows directly into your objects, then the
DataReader is all you need. This is common in CSLA-style projects
(which I consider a worst-of-both-worlds approach that would be better
handled with a system like NHibernate).

Cor Ligthert [MVP] wrote:
David,

Why? They both have their own purpose. Just read the message from Jani to
learn something about it.
In most situations is the dataset/datatable concept the best to use, their
are in my opinion situations like classic batch processing were the
SQLDataReader is better in its place.

Cor

"David Anton" <Da********@discussions.microsoft.comschreef in bericht
news:F4**********************************@microsof t.com...
DataSets and DataTables are not appropriate for many purposes.
It sounds to me like SqlDataReader might be more appropriate (and probably
more efficient).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"DaBrain" wrote:
I can use some help here.

I have been moving from Delphi/C++ Win32 Database Development (ADO) to
C#.Net /ADO.NET the language is not the problem ADO.NET is however
kicking my butt.

I finally get the true power and purpose of a Dataset, wow, that was a
light bulb Not easy when you are still thinking ADO RecordSets almost
got the basics down, , NOT! From what i have read so far, this is a
hurdle for lots of coders.

I have developed a tiny little DB program against SQL Server to test my
skills and all works fine, then I try the most mundane and simple of
tasks and realize I have no clue what I'm doing.

The Simple Problem (Made Small):

I have a DataGrid and a Button I click the button and I want it to
update the current record (Currently Selected in the Grid) Lets say,
update the Field/Colum Named "Price" to 19.95

Old School Delphi

MyTable.Edit;
MyTable.FieldByName("PRICE").Value := 19.95
MyTable.Post;

// ^This would have edited the CURRENT Record.

The Question
How do I Access the "Current" Active Record in a DataGrid
Programmatically and Update one of the fields Programmatically
My Guess is something Like
MyDataSet.Tables[0].SOMETHING // IM LOST HERE

By Asking this question I hope to:
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

I am a good coder, i just need the light bulb to go on- I need that
moment to happen.

My Head is Stuck In something like this:

MyDataSet.Tables[0].Edit;
MyDataSet.Tables[0].Field("PRICE") = 19.95
MyDataSet.Tables[0].Post;

Oct 16 '06 #7
Cor,

thanks.

Learning ADO.NET isn't something that I'd call easy, but eventually one will
learn. Because of the complexity, I find it difficult to explain the core
concepts in a compact way; there's just too much to talk about initially.

The MSDN documentation does a good job in my opinion, but the problem is
that the texts are often very reference-like, whereas beginners are often
looking for simpler overviews without too much detail.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Oct 16 '06 #8

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

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.