.NET
  Home arrow .NET arrow Page 4 - Working with ADO.NET
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Working with ADO.NET
By: Anand Narayanaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2005-08-29

    Table of Contents:
  • Working with ADO.NET
  • Connecting to the database
  • Inserting Records
  • Searching Records

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with ADO.NET - Searching Records


    (Page 4 of 4 )

    The second part of our application is the Search functionality. For this purpose, add a new Windows Form to the application (Project | Add Windows Form). Select the menu item titled Windows Form, give a meaningful name and click the Open button. Create a user interface as shown in figure 2 and suply the required properties.

    The application links the new form you have created from the main form. For this purpose, double click the Search button and add the following line of code:

    Dim search1 As Search = New Search

    search1.Show()

    We make use of a ComboBox and a TextBox for our search application. The ComboBox populates the list of all names from the database, and the textbox will display the corresponding e-mail address by searching the records from our database.

    In the previous session, you learned about the use of built-in data controls for establishing a connection to the database. In this session, we will see how to do this job with the help of .NET code. The code for establishing the connection and populating a ComboBox is shown in the listing given below. Please be sure to supply the code inside the Load event of the form.

    m_oledbconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:/customer.mdb"

            m_oledbconnection.Open()

     

            Dim m_oledbdataadapter As OleDbDataAdapter

            m_oledbdataadapter = New OleDbDataAdapter("Select * from customer_list", m_oledbconnection)

     

            Dim cmd As New OleDbCommand("Select * from customer_list", m_oledbconnection)

     

            Dim m_oledbcommandbuilder As OleDbCommandBuilder

            m_oledbcommandbuilder = New OleDbCommandBuilder(m_oledbdataadapter)

     

            Dim m_datatable As New DataTable

            m_oledbdataadapter.Fill(m_datatable)

     

            Dim objreader As OleDbDataReader = cmd.ExecuteReader()

            'ComboBox Population

            Do While objreader.Read()

                cboName.Items.Add(objreader("Name"))

           Loop

            m_oledbconnection.Close()

            cboName.SelectedIndex = 0

    Please note that you will have to modify the path name of the database before attempting to execute the application. Double click the ComboBox control on the form and add the following lines of code:

    Try

                Dim strsql As String

                strsql = "SELECT * FROM customer_list WHERE Name = '" & cboName.Text & "' "

                m_oledbconnection.Open()

                Dim cmd As New OleDbCommand(strsql, m_oledbconnection)

                Dim objreader As OleDbDataReader = cmd.ExecuteReader

                objreader.Read()

                txtEmail.Text = objreader("E-mail")

                objreader.Close()

                m_oledbconnection.Close()

     

            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)

            End Try

    The above code will search for the field named ‘Name’ from the database customer_list. If it matches with the one listed on the ComboBox, the corresponding e-mail will be displayed on the TextBox control with the code given below:

    txtEmail.Text = objreader("E-mail")

    In the next part of this article, you will learn how to edit and delete data from a database using ADO.NET.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · I am the author for the article - Working with ADO.NET. I hope you will find this...
       · Hi, how to use Dataset with ADO.Net ? there is any free material is there in...
       · Please checkhttp://msdn.microsoft.com/msdnmag/issues/02/01/data/A google...
       · The iGrep search will help you more than Google or any other search. The iGrep...
     

    .NET ARTICLES

    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway