locked
How to pass dataset as argument to a method??

    Question

  • I am unable to pass a dataset into a method. I read somewhere that it must be passed by reference; however, I am unable to find an exact example and thus am having some trouble with this.

    vampDataOledb.insertBuilder("tblJuveniles", strConnectionString, Database6DataSet);
    
    //the first argument is the table name followed by the connection string then the dataset.
    
    //I get an error under the dataset stating //"'Juvenile_Database_Round_2.Database6DataSet' is a 'type' but is //used like a 'variable'.
    
    //this is the method
    
    
    public static void insertBuilder(string strTableName, string strConnectionString, DataSet dS)
     {
     //strFieldNames = new string[30];
    
     string[] strDataTypes = new string[30];
     string[] strColumnNames = new string[30];
    
     OleDbConnection Launch = new OleDbConnection();
     Launch.ConnectionString = strConnectionString;
    
     int x = 0;
     int intStringCount = 0;
     string strInsertStatement;
    
     //String builder is declared to start building the INSERT statement
     StringBuilder myStringBuilder = new StringBuilder("INSERT INTO ");
     myStringBuilder.Append(strTableName);
     myStringBuilder.Append(" (");
    
     //DataTable table = GetTable(strTableName);
    
    
    
     
     foreach (DataTable dT in dS.Tables)
     {
     if (strTableName == dT.TableName)
     {
     for (int z = 0; z < dT.Columns.Count | z < 30; z++)
     {
      strDataTypes[z] = dT.Columns[z].DataType.ToString();
      strColumnNames[z] = dT.Columns[z].ColumnName;
     }
     }
     
     }
     
    
    //And as you might be able to see, I am trying to create an automatic query/ oled innsert method. This is why I need to pass this method my dataset.
    
    

    //ing this with a dataset, as I can not find any exact examples.

     

    this is my call

     

     




    • Edited by Vamp10988 Wednesday, August 17, 2011 4:43 PM
    Wednesday, August 17, 2011 4:38 PM

Answers

  • You need to pass in an instance of the dataset not the name of the type.  That is what the exception is telling you.

    var ds = new Database6Dataset();
    vampDataOledb.insertBuilder("tblJuveniles", strConnectionString, ds);

    This is just an empty example to show that you need an instance of the type not just the type.

    Wednesday, August 17, 2011 4:43 PM

All replies

  • You need to pass in an instance of the dataset not the name of the type.  That is what the exception is telling you.

    var ds = new Database6Dataset();
    vampDataOledb.insertBuilder("tblJuveniles", strConnectionString, ds);

    This is just an empty example to show that you need an instance of the type not just the type.

    Wednesday, August 17, 2011 4:43 PM
  • That is one of the things I am confused about. Im not sure I understand what "new instance" means. Does this mean I will create a copy of my dataset and thus any changes made to this copy in my method will not reflect in my original dataset? Or does this mean it will create a reference that will change the original dataset as well? 
    Wednesday, August 17, 2011 5:33 PM
  • If you understand what a method is, then look at instances as being the next step.  An object instance is a unique package of properties, methods and other class members.  You declare variables as object instances with the 'new' keyword.

    For example, suppose you had a class named House, which had a color property.  You declare unique instances of a red house, a blue house, etc.  You could add several instances into a data collection called 'neighborhood'.  You could even define a class named Neighborhood, and then add those to a data collection called 'town'.  Etc.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Wednesday, August 17, 2011 9:50 PM
  • Another way to think about it is, the class Database6Dataset is the blueprints for the house.  And using the new operator you construct the house.  Saying you're getting a copy isn't right, because that implies you have something to base the copy on.  You don't until you use new.  (See my example above.)
    Friday, August 19, 2011 1:21 PM