Solved

Starting with VB.net & ADO.Net

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

862 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

29 Experts available now in Live!

Get 1:1 Help Now