p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Visual Basic 2008 Programmer's Reference ISBN: 978-0-470-18262-8 (http://p2p.wrox.com/forumdisplay.php?f=384)
-   -   Adapting Ch. 20 code, to create a 'SELECT' DataSet ? (http://p2p.wrox.com/showthread.php?t=75091)

liamfitz July 3rd, 2009 06:20 PM

Adapting Ch. 20 code, to create a 'SELECT' DataSet ?
 
[:confused:]I'm using the following code to crete a new dataset, which then repopulates a DataGridView, based on an 'Add Query' DataSet ( creted using the wizard ). I'd like to know the underlying code, so I can slightly adapt it in a sub-routine. For example

Private Sub btnWhatever_Click( ..................................... and so on )

create connection to DB
create an SQL command with (?) in appropriate WHERE clause.
pass the parameter(s) to the command.
execute the command.
fill the DataGridView with new DataSet
close connection

End Sub()

Code:


Private
Sub btnABig_Click(ByVal sender AsObject, ByVal e As System.EventArgs)_ & Handles btnABig.Click

Try
Me.CustomersTableAdapter.initA(Me.Bm2009Customers.Customers)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
EndTry
EndSub


In Ch. 20 there's only examples for non-query routines. Any help much appreciated.

MuhammadShahid July 7th, 2009 10:56 AM

Hi ,

If you are using Wizard to create a DataSet and Tables Adapter use the following code to fill the corresponding tables.
Code:

DataSet1 ds1 = new DataSet1();
AnyTableAdapter ata = new AnyTableAdapter();
ata.Fill(ds1.anyTable);

In any other case if u try to fill programmatic ally use the following methods fill by procedure and fill by Query.

Return DataTable fill by Query method
Code:

public DataTable RunSelectQuery(string sqlQuery, OracleParameter[] @params)
        {
            OracleDataAdapter objDataAdapter = default(OracleDataAdapter); ;
            DataTable dt = new DataTable();
            try
            {
                OpenConnection();
                OracleCommand cmd = new OracleCommand(sqlQuery, dbConnection);
                cmd.CommandType = CommandType.Text;
                objDataAdapter = new OracleDataAdapter(cmd);             
                ResolveParameters(ref cmd, @params);
                objDataAdapter.Fill(dt);
            }
            catch (Exception oErr)
            {             
                throw oErr;
            }
            finally
            {
                CloseConnection();
                if ((objDataAdapter != null)) objDataAdapter.Dispose();
            }
            return dt;
        }

Return DataSet fill By procedure
Code:

public DataSet FillDataSetWithProcedure(string spName, OracleParameter[] @params)
        {
            DataSet returnDs = new DataSet();
            OracleDataAdapter da = new OracleDataAdapter();
            OracleCommand cmd = new OracleCommand();
            try
            {
                OpenConnection();
                cmd = new OracleCommand(spName, dbConnection);
                cmd.CommandType = CommandType.StoredProcedure;           
                ResolveProcParameters(ref cmd, @params);
                da = new OracleDataAdapter(cmd);
                da.Fill(returnDs);
            }
            catch (Exception ex)
            {             
                throw ex;
            }
            finally
            {
                CloseConnection();
                if ((da != null)) da.Dispose();
                if ((cmd != null)) cmd.Dispose();
            }

            return returnDs;
        }

The return data Source assigned to specified DataGridView to fill the record.

The above code return in C# supported by Oracle Data Access . if you need any further help let me know about it.


All times are GMT -4. The time now is 06:20 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.