Solved

Starting with VB.net & ADO.Net

Posted on 2004-10-03
2
182 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

14 Experts available now in Live!

Get 1:1 Help Now