Solved

Starting with VB.net & ADO.Net

Posted on 2004-10-03
2
175 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
2 Comments
 
LVL 27

Accepted Solution

by:
Dabas earned 50 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now