Results 1 to 3 of 3

Thread: How could I reduce this Code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    How could I reduce this Code

    vb Code:
    1. Dim DRow As DataRow = DS.Tables(0).Rows(0)
    2.             If DS.Tables(0).Rows.Count > 0 Then
    3.                 .txtCourseCode.Text = DRow.Item("CourseCode").ToString()
    4.                 .txtRoomNumber.Text = DRow.Item("RoomNumber").ToString()
    5.                 .txtSchoolYear.Text = DRow.Item("SchoolYear").ToString()
    6.                 .txtTeacher.Text = DRow.Item("Teacher").ToString()
    7.             End If

    Since theirs only 1 ROW in the Table, How could I just read from that instead of having the DataRow and so on? Is their a way to reduce / optimize it? I know its not a lot, but I don't want redundant code.

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

    Re: How could I reduce this Code

    There is no redundant code there. The only issue with that code is that you're getting the first DataRow before you actually check that there is a first DataRow to get. If you know for sure that there's one DataRow then what's the check for? If you don't know for sure that there's one DataRow then that first line's going to throw an exception if there isn't.
    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
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: How could I reduce this Code

    Big long lists of filling Textboxes as other controls with database fields is common in VB.NET. There's nothing wrong with it.

    If constructed correctly, you could databind those textboxes to your database though, but that has drawbacks along with the benefits it offers so you'd really have to determine if it's right for you and in the end, may be just as much work to set up.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

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