Library tutorials & articles

An Introduction to VB.NET and Database Programming

Data Binding

Actually binding data to a control is very easy in the IDE. Just select the control on the form and then go to the properties pane and select the DataSource and the display member. These will both be drop downs and the datasets you have created will be selectable. The DataSource is the name of your Dataset.Table (so in the case of my category combo box the DataSource was DsCategories.Categories. The Display member is the actual field in the table that you want to be bound to that field and displayed. In the Recipe application that was category. In order to get this the selected category has to be passed to the DataAdapter that populates the Recipe DataSet. To do that I used the code below:

Private Sub cboCategory_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.SelectedIndexChanged

If bNewRow Then
         txtCategory.Text = cboCategory.Text
Else
         DsRecipes1.Clear()
DaRecipes.SelectCommand.Parameters( "Category" ).Value = cboCategory.Text
         DaRecipes.Fill(DsRecipes1)
         cboRecipe.Focus()
End If

End Sub

The code above clears and refills DsRecipes1. The line in red sends a value to the DataAdapter which is parameterized. The remainder of the application uses the binding manager and bound controls to navigate through the data, edit the data or add new data.

The important thing to remember when you are trying to add, delete, or edit data is that your application is working with its own DataSet not the actual database. To update the data you must use a command to take the current dataset and update the database with it. Here is the code that I used to accomplish this:

Private SubUpdateDatabase()
     DaRecipes.Update(DsRecipes1.Recipes)
End Sub

Any code that updates the database uses the above code by containing Me.UpdateDatabase() . This code is included in the update and delete button code of the Recipe Application code. Below is the code for the delete button so you can see it in action:

Private Sub btnDelete_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

Dim iResult As DialogResult = MessageBox.Show( "Delete " & txtTitle.Text & " ?" , "Confirm Delete" , MessageBoxButtons.YesNo, MessageBoxIcon.Question)

If iResult = DialogResult.Yes Then
         RecipesBindingManager.RemoveAt(RecipesBindingManager.Position)
Me.UpdateDatabase()
         cboCategory.SelectedIndex = -1
Me.SetMaintenanceButtons( True )
Me.SetEntryControls( False )
Me.SetComboBoxControls( True )
         cboCategory.Focus()
         btnDelete.Enabled = True
End If
End Sub

Comments

  1. 12 Jan 2009 at 11:11
    here you can find a detailed help on ado.net http://vb.net-informations.com/ado.net/vb.net-ado.net-tutorial.htm calos.
  2. 03 Jan 2009 at 10:06
    Lahana Kapsülü Lahana Kapsülü Tala Krem Tala Krem diyet diyet sivilce sivilce
  3. 08 Jul 2008 at 04:52

    Hi Chathura,

          Good day to you. As you asked the question about the vb.net. And from your explanation you also said that you are little good in vb6. So once you are entering the .net framework area you should be aware of this area. Anyway you are a new user of this programming even u are good in vb6. The main matter in vb.net is that, its quit similar like java. Where in java, you write program in one platform but you can run the program from all other platform. But in vb.net you write the program in all platform but run the program from one platform. Mostly we use vb.net for database connectivity application. We choose this also for web applications. So its very usual that to make a program in vb.net we will be using SQL access, ado.net or asp.net etc. So its natural that if you download vb.net the other software also included with it. May I know what site you used to download vb.net? May be you are downloading fro, any wrong sites. Thank you.

     

                                                                                                                              Best regards

                                                                                                                                  Imran
     

  4. 11 Nov 2007 at 09:54

     i want to how to have a data set of more than10 tables

  5. 27 Jul 2007 at 15:21
    You are welcome.

  6. 28 May 2007 at 09:08

    Hi,

    As a M.E.  student of Computer Engineering  i am serching for the VB.net Platfrom how 2 start with & find your para usefull. Thanks

     

     

     

     

  7. 17 Apr 2007 at 09:11

    Hi All

    I'm a beginner to VB .NET but have quite good knowledge in VB 6. I'm currently in the process of switching to .net.

    When I install visual studio 2005, there are SQL Server Mobile package and some other SQL server stuff. could you please tell me why they are there and what I can do with them?

    Also, is there one called profile or something? As I heard, as a VB 6 project can hold many forms, in .Net there are profiles which can hold many projects. Could you please explain?

    Appreciate your help

    Best Regards,

    Chathura 

  8. 16 Dec 2006 at 08:11

    Hi,

    I am having a problem in data binding in vb.net. I have to use MS-Access as database and win forms controls to design a application. Please guide me . Specially in adding the records into the table at run time. Like I just enter the data in the text boxes and clike on the ADD button. The data should automatically be added into the table.

                                   Thank you.

  9. 24 Oct 2005 at 21:47

    a web site that I wrote works well under internet explorer, but when I run the site on mazilla the forms get changed.
    The colors are deleted, and the sizes of the textbox's, change.  When I examined the html code generated, there  was a difference between the code  for IE and Mazilla.  How do I correct this

  10. 25 Aug 2005 at 01:38

    Hi,
    I have a bit understanding of VB.net.
    Currently I m using winXP Pro.
    I Create an Installation, and I set
    DetectNewerInstalledVersion to False
    RemovePreviousVersions to True.


    However, When I first install a fresh copy, it run fine. An entry was created on Add/Remove Program.


    After I update something on the application and rebuild the installation file and change the product code and upgrade code; I re-run the installation again without uninstall the previous version.


    Installation was success. But the problem is it create 2 entry in Add/Remove Program.


    If i install 5 time, it will create 5 entry.
    I'm not sure what's wrong with my installation scripts.


    Like Winzip or Winrar. no matter how many time we run the installation, it only create one entry on Add/REmove program.
    Is there any other way to fix this ?


    Please help!

  11. 10 Jan 2005 at 22:36

    Hi,
        The topic which you have explained in the article has been of much help to me,I have Joined an organisation as a programmer.As of now,i have not yet started woith programming.
    But since i am new to this field,i would expect you to kindly post more articles which would make working with databases easy.


    Regards,
    Anu

  12. 09 Mar 2004 at 20:35

    Chas,


    VB .NET is the Visual Basic language which you can purchase by its self.  .NET Studio is the development Suite which can contain several different development languages, such as VB, C#, Visual J, etc.  I think you can get VB .Net for like $150 and the .Net Suite is around $1000.


    If you are a college student you can either of these at a greatly reduced rate for the academic version.


    Chester

  13. 02 Mar 2004 at 11:31
    Visual Studio is the Microsoft IDE for writing VB.NET and C# programs - VB.NET is the programming language!

    However, if you purchase VB.NET Professional, that will basically be Visual Studio with support for VB.NET (and not C#)
  14. 02 Mar 2004 at 10:43
    I read your article and its really nice.

    I wanted to ask you, what's the difference between Visual Basic.NET and Visual Studio.NET

    I know a litlle about Visual Basic 6 but I just got a new job and want to learn more stuff so that
    I can create a nice program for my company.

    I will purchase the books you recommend for beginners.   thanks

  15. 01 Jan 1999 at 00:00

    This thread is for discussions of An Introduction to VB.NET and Database Programming.

Leave a comment

Sign in or Join us (it's free).

Chas Profitt

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

Want to stay in touch with what's going on? Follow us on twitter!