Solved

Starting with VB.net & ADO.Net

Posted on 2004-10-03
2
184 Views
Last Modified: 2010-04-23
Hi

I'm just starting out with VB.net and ADO.net.  Although I'm getting everything to work, the code seems very long winded.  For instance this is my code to check the number of records in a dataset set table and add a blank record if it is 0.
'
  Dim intRecCount As Integer
        OleDbDataAdapter1.Fill(MemberDataSet1)
        intRecCount = Me.BindingContext(MemberDataSet1, "Members").Count
        If intRecCount = 0 Then
            Dim dr As DataRow
            dr = MemberDataSet1.Tables("Members").NewRow
            MemberDataSet1.Tables("Members").Rows.Add(dr)
            Me.BindingContext(MemberDataSet1.Tables("Members")).EndCurrentEdit()
            OleDbDataAdapter1.Update(MemberDataSet1.Tables("Members"))
        End If
'
It works Ok but it seems a lot of code.  This is just an example - all the code I'm generating seems as complex.  Am I missing something?

0
Comment
Question by:Petermcg001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Accepted Solution

by:
Dabas earned 50 total points
ID: 12213668
Hi Petermcg001:
What about removing the binding part of it?

This is slightly shorter:

 Dim intRecCount As Integer
        OleDbDataAdapter1.Fill(MemberDataSet1)
        intRecCount = MemberDataSet1.Tables(0).Rows.Count
        If intRecCount = 0 Then
            Dim dr As DataRow
            dr = MemberDataSet1.Tables(0).NewRow
            MemberDataSet1.Tables(0).Rows.Add(dr)
            OleDbDataAdapter1.Update(MemberDataSet1)
        End If

Another alternative is to use a command object with the appropriate INSERT SQL statement instead

    If intRecCount = 0 then
        Dim cm As New OleDb.OleDbCommand("INSERT INTO Members .....", cn)
        cm.ExecuteNonQuery()
    End IF

Dabas
0
 
LVL 4

Expert Comment

by:VIkasumit
ID: 12216123
Hi..

The Alternative Option is what i use most.

When you just want to need to insert the data in the table than better use the xxxCommand object than using the Dataset it is good if you want ot Dispaly data and for select querys with some data otherwise better use the xxxCommand Object for select Max, min ave, or Insert, update and than refresh your dataset for display is what i use though not very sure how good it is in terms of the Speed on server and bandwidth utilization if run on Client server arctiechture.... but run ine on single machine or even if only about 100 user are using your program at one time

VIkasumit
www.sumitonnet.20m.com
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question