|
-
May 21st, 2007, 04:25 AM
#1
Thread Starter
New Member
Data Reader
Hello All!
Having a problem with assigning the data reader to the field which I want to do. I know how this is done in VB.NET but how does this work in C# syntax code?
VB.NET
DR = Command.ExecuteReader
While DR.Read
txtFirstName.Text = (DR! FirstName)
End While
Now when I try this in C# code I have all sorts of problems:
C#
DR = Command.ExecuteReader();
while (DR.Read())
{
txtFirstName.Text = (DR! FirstName); < Problems Here?
}
Any ideas???
-
May 21st, 2007, 05:38 AM
#2
Re: Data Reader
That notation is a VB6 hold-over that I recommend not using. This code:
Code:
txtFirstName.Text = DR("FirstName")
is valid in both VB.NET and C#.
-
Jun 1st, 2007, 01:46 AM
#3
Fanatic Member
Re: Data Reader
Hello,
Try this one :
Code:
DR = Command.ExecuteReader();
while (DR.Read())
{
txtFirstName.Text = Convert.ToString(DR["FirstName"]);
}
hope it works 
To specify the field name you should use [] instead of ().
I also have no idea why. But I think it's all about the matter of C# syntax, which is quite different with those in VB.NET.
Regards,
WJ
Last edited by Wen Lie; Jun 1st, 2007 at 01:54 AM.
-
Jun 1st, 2007, 02:02 AM
#4
Re: Data Reader
 Originally Posted by Wen Lie
To specify the field name you should use [] instead of ().
Oops. Quite right. Parentheses are for argument lists and brackets are for indexes in C syntax, while VB syntax uses parentheses for both.
There's no need for the Convert.ToString though. Presumably a column named "FirstName" contains strings already, so you should be casting, not converting. Converting a string to a string is a bit redundant.
-
Jun 1st, 2007, 02:13 AM
#5
Fanatic Member
Re: Data Reader
 Originally Posted by jmcilhinney
There's no need for the Convert.ToString though. Presumably a column named "FirstName" contains strings already, so you should be casting, not converting. Converting a string to a string is a bit redundant.
Are you sure ?
coz, I've tried before not to use "Convert" syntax, and returned me an error, which sounds like invalid type when putting the value to a textbox. Even if the content of the field is "string".
That's why I mostly use "convert" when I'm trying to insert a value from field to textbox object.
Well, I'm using C# for ASP.NET. Is it different with the one used for Windows App. ?
Coz I think web and windows in .NET use the same concept, if I'm not wrong. =)
Correct me if I'm wrong.
Thanks n Regards,
WJ
-
Jun 1st, 2007, 03:24 AM
#6
Re: Data Reader
 Originally Posted by Wen Lie
Are you sure ?
coz, I've tried before not to use "Convert" syntax, and returned me an error, which sounds like invalid type when putting the value to a textbox. Even if the content of the field is "string".
That's why I mostly use "convert" when I'm trying to insert a value from field to textbox object.
Well, I'm using C# for ASP.NET. Is it different with the one used for Windows App. ?
Coz I think web and windows in .NET use the same concept, if I'm not wrong. =)
Correct me if I'm wrong.
Thanks n Regards,
WJ
I'm quite sure. You cannot assign anything other than a string to a property of type String so this would be illegal:
Code:
txtFirstName.Text = DR["FirstName"];
because the reader returns the field value as an Object reference. The reference may refer to an object of any type, but if it refers to a string then there's no need or point to converting a string to a string. You should cast the reference as type String instead:
Code:
txtFirstName.Text = (string)DR["FirstName"];
or:
Code:
txtFirstName.Text = DR["FirstName"] as string;
-
Jun 1st, 2007, 09:21 PM
#7
Fanatic Member
Re: Data Reader
Hmm....
okei, thanks.
anyway,... why is data reader can't do the move next, previous, first, last... like I did in previous VB 6 ?
Regards,
WJ
-
Jun 1st, 2007, 09:33 PM
#8
Re: Data Reader
 Originally Posted by Wen Lie
Hmm....
okei, thanks.
anyway,... why is data reader can't do the move next, previous, first, last... like I did in previous VB 6 ?
Regards,
WJ
Why can't a car fly? Because it's not designed that way. A DataReader provides read-only, forward-only access to a result set. If you want to be able to navigate the data at will then you need to load it into a DataTable.
-
Jun 1st, 2007, 09:49 PM
#9
Fanatic Member
Re: Data Reader
=)~
hehehe... Just curious.
thanks man...
Regards,
WJ
-
Jun 1st, 2007, 11:22 PM
#10
Re: Data Reader
More specifically, ADO.NET is designed around a disconnected model. With ADO you navigated live data in the database via a Recordset. With ADO.NET you retrieve data into a local data store, manipulate it disconnected from the database and then save the changes back again. Retrieval, editing and saving a distinct operations in ADO.NET whereas in ADO they were basically blurred into one.
-
Jun 2nd, 2007, 01:58 AM
#11
Fanatic Member
Re: Data Reader
so ?
do you mean that we better use data-adapter, dataset, and data table, rather than data reader ?
-
Jun 2nd, 2007, 04:08 AM
#12
Re: Data Reader
You use whatever's appropriate for each particular circumstance. You might use a DataReader alone, a DataReader to populate a DataTable or a DataAdapter to populate a DataTable, depending on the circumstances.
-
Jun 2nd, 2007, 05:15 AM
#13
Fanatic Member
Re: Data Reader
hmm...
interesting.
I'll give it a try.
Thanks man...
anyway, I'm also a newbie in C#
=)~
-
Jun 2nd, 2007, 08:54 PM
#14
Re: Data Reader
This code is in VB.NET but the equivalent C# would be almost the same anyway. It shows when you'd use a DataReader alone, when you'd use a DataReader and a DataTable and when you'd use a DataAdapter and a DataTable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|