Click here to Skip to main content
15,890,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Method Thad Fill Data to Grid and return by ref data adapter object to able me to update data in grid after row update ...
and i Mad a Method to Pass OLEDB or SQLDB data adapter Variable
but when I use a ref or Out to Data Adapter error appear

What I have tried:

this a code from Method
enum ConnectionType
  {
      SQL,
      OLEDB
  }


public void Grid(ConnectionType ADOType, GridControl obj, object cn, DataSet DS, ref object DA, ref DataTable DT, String DataTableName, string sqlString, params object[] Parameter) 
        {

            if (ADOType == ConnectionType.OLEDB)
            {
                DataManagerOLEDB DM_ = new DataManagerOLEDB();
                OleDbDataAdapter da =  (OleDbDataAdapter)DA; 
                DM_.SelectFromTable((OleDbConnection)cn, sqlString, DS, out da, 
                DataTableName, Parameter);
            }
            else
            {
                DataManagerSQL DM_ = new DataManagerSQL();
                SqlDataAdapter da = (SqlDataAdapter)DA;

                DM_.SelectFromTable((SqlConnection)cn, sqlString, DS, out da, 
                DataTableName, Parameter);
            }
            
            DT = DS.Tables[DataTableName];
            obj.DataSource = DT;


when Pass Method like This

<pre>SurveyVar.fillDxControl.Grid(ConnectionType.OLEDB, grdSurvey, SurveyVar.cnSurvey, SurveyVar.DataSetSurvey, ref SurveyVar.DataAdapterSurvey,  ref datSurvey, "T002", sqlString, vParameters);               



Error appear on data adapter variable as "
Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1503	Argument 5: cannot convert from 'ref System.Data.OleDb.OleDbDataAdapter' to 'ref object'	Survey	F:\BannaSQLSource\_New Application\Source Code\SurveyApplication\Application Caption\forms\frmSurvey.cs	125	Active
Posted
Updated 21-Feb-18 1:22am

1 solution

Let's simplify this a little:
C#
private void MyMethod(ref object o)
    {
    }
private void MyButton_Click(object sender, EventArgs ew)
    {
    OleDbDataAdapter da = new OleDbDataAdapter();
    MyMethod(ref da);
...
Why do you get an error saying:
cannot convert from 'ref System.Data.OleDb.OleDbDataAdapter' to 'ref object'
Think about it: o is a reference to an object, so you can very legitimately put any type of value into it inside the method:
C#
private void MyMethod(ref object o)
    {
    o = "hello";
    }
If the system allowed you to do that, what effect would it have?

Unpleasant, that's what. Because o is a ref, any changes to it are reflected in the calling method parameters variables -= in this case, da gets set to a string. So when control goes back to the button click event and the code tries to use the data in the DataAdapter, it finds a string and everything starts to fall over.

As a result, you can't cast a ref parameter - the type must match exactly.
If you need to pass different object types as ref parameters, then consider using generics instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900