Results 1 to 14 of 14

Thread: Data Reader

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    8

    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???

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data Reader

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Re: Data Reader

    Quote 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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data Reader

    Quote 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;
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data Reader

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Re: Data Reader

    =)~
    hehehe... Just curious.
    thanks man...

    Regards,
    WJ

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Re: Data Reader

    so ?
    do you mean that we better use data-adapter, dataset, and data table, rather than data reader ?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524

    Re: Data Reader

    hmm...
    interesting.
    I'll give it a try.
    Thanks man...

    anyway, I'm also a newbie in C#
    =)~

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width