Results 1 to 13 of 13

Thread: ADO .NET Code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Thumbs up ADO .NET Code

    I got around and around without finding nothing useful for creating an ADO.NET connection to an access database... Finally I got it working and now I post here the code:
    VB Code:
    1. Dim myConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0...." ' Put here the connection string
    2. Dim mySelectQuery As String = "SELECT * FROM table" 'This is your sql query
    3. Dim myConnection As New System.Data.OleDb.OleDbConnection(myConnString) 'Create a new connection
    4. Dim myCommand As New System.Data.OleDb.OleDbCommand(mySelectQuery, myConnection)        
    5. Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader()
    6.        
    7. myConnection.Open() ' Open Connection
    8.  
    9. Try
    10.     While myReader.Read() ' Read all records
    11.         ' Do what you want with data...
    12.         ' Use myReader. methods to retrive your data:
    13.         ' myReader.GetInt32(0) gets a 32-bit int from 1st field (zero-based)
    14.         ' myReader.GetString(1) gets a string from 2nd field....
    15.         ' For exampe:
    16.         MsgBox(myReader.GetInt32(0).ToString() + ", " + myReader.GetString(1))
    17.     End While
    18. Finally
    19.     ' Close connection
    20.     myReader.Close()
    21.     myConnection.Close()
    22. End Try
    I hope this will be useful

    Xmas.
    Learn, this is the Keyword...

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Talking Little mistake...

    Made a little mistake... Put the myConnection.Open() statement before the dim myReader....
    You'll get an error...
    Learn, this is the Keyword...

  3. #3
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401
    xmas79, i know what you mean. here's my version:

    http://www.vbforums.com/showthread.p...hreadid=221166
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

  4. #4
    Member
    Join Date
    Nov 2002
    Posts
    61
    You have done great job.Really Good.

    I am struggling to put data into datagrid at runtime by dataset.

    Here is my code:
    Dim SQLConn As SqlConnection
    SQLConn = New SqlConnection()
    Dim sConnectionString As String
    sConnectionString = "Data Source=VIDHYA;user id=sa;PWD=;Initial Catalog=pubs;"
    SQLConn = New SqlConnection(sConnectionString)
    Dim SQLCommand As New SqlCommand("SELECT * FROM addressdb", SQLConn)
    Dim SQlAdop As New SqlDataAdapter(SQLCommand)
    Dim mydataset As New DataSet("selectAddress")

    Try
    SQLConn.Open()

    SQlAdop.Fill(mydataset, "addressdb")

    Dim rowaddress As System.Data.DataRow


    DataGrid1.DataSource = mydataset



    Catch Mycxception As Exception
    MessageBox.Show(Mycxception.ToString())
    Finally
    If SQLConn.State = ConnectionState.Open Then
    SQLConn.Close()
    End If
    If Not mydataset Is Nothing Then
    mydataset = Nothing
    End If
    End Sub.


    Please help me out

  5. #5
    Junior Member
    Join Date
    Nov 2002
    Location
    New York
    Posts
    29
    I think the problem is here:

    DataGrid1.DataSource = mydataset

    this should read

    DataGrid1.DataSource = mydataset.Table(0).DefaultView

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    iulianionescu is right. You should also add this:
    VB Code:
    1. DataGrid1.DataBind()
    It should work now.
    Learn, this is the Keyword...

  7. #7
    Member
    Join Date
    Nov 2002
    Posts
    61
    Yes it works fine.But i cannot use
    Datadrid1.DataBind()

    In Vb.NET

    datagrid1.databindings() is not working.

    Thanks.

  8. #8
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401
    here's a way that i've used:
    Code:
            Dim strSQLStm As String = "SELECT clm_First as FirstName, " & _
                                             "clm_Last as Surname, " & _
                                             "clm_Address as Address, " & _
                                             "clm_Suburb as Suburb, " & _
                                             "clm_Postcode as Postcode, " & _
                                             "clm_ClientID as ClientID " & _
                                      "FROM tbl_ClientMaster " & _
                                      "WHERE (clm_First LIKE '" & txtFirst.Text & "%') " & _
                                        "AND (clm_Last LIKE '" & txtSurname.Text & "%') " & _
                                      "ORDER BY clm_First, clm_Last"
    
            Dim daCustomers As New OleDb.OleDbDataAdapter(strSQLStm, gconDatabase)
            Dim dsCustomers As New DataSet()
            daCustomers.Fill(dsCustomers, "tbl_ClientMaster")
            grdClients.SetDataBinding(dsCustomers, "tbl_ClientMaster")
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

  9. #9
    Member
    Join Date
    Nov 2002
    Posts
    61

    I need to select column value in Datagrid and displays in textbox

    Hi All,

    Thanks For helping me.

    I Need one more help.

    In Visual Basic 6.we select the column and displays it in textbox .
    Private Sub LedgerGrid_Click()

    If MSFlexGrid.Col = 1 Then
    Text1.Text = MSFlexGrid.Text
    End If

    End Sub

    Samething i would like to do in VB.NET.But Vb.NET does not have Col property.How can i do it VB.NET

    Thanks.

  10. #10
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401
    I don't think this isn't exactly what you're after but might help your a bit:
    Code:
    dim grdClients as new DataGrid
    Private Const cColClientID As Integer = 5
    ClientID = grdClients.Item(grdClients.CurrentRowIndex, cColClientID)
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

  11. #11
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872
    Xmas79,
    I used your code, and i've put the myconnection.open before the dim myreader but i got this error:

    An unhandled exception of type "System.Data.OleDB.OleDBException occurred in System.data.dll
    Additional information: No error information available: DB_SEC_E_AUTH_FAILED(0x80040e4d)

    what does this mean? why doesn't it work?
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  12. #12
    Member
    Join Date
    Nov 2002
    Posts
    61

  13. #13
    Junior Member
    Join Date
    Nov 2002
    Location
    New York
    Posts
    29
    Why do you need to open the connection anyway? The dataadapter fills the dataset without a prior need for the connection to be opened. Actually this is the main difference with .NET that the database access is done in a non-connected mode and the connection is only briefly done while the data is exchanged...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width