Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Stefan Wuebbe
  Where is Stefan Wuebbe?
 Stefan Wuebbe
 To: Kulwant Singh
  Where is Kulwant Singh?
 Kulwant Singh
Subject: RE: variable no. of parameters
Thread ID: 393821 Message ID: 393826 # Views: 49 # Ratings: 3
Version: Not Applicable Category: .NET
Date: Tuesday, December 24, 2013 11:00:04 AM         

Hi Kulwant,

The classical .Net approach would be "overloading", not as relaxed as VFP, your insertData() method would have different "signatures", so that it would look like
public void insertData(string name, string surName, string age /* age is not int? */) {...
public void insertData(string name, string surName) {...
public void insertData(string name) {...

In newer .Net versions, parameters can be kind of optional by setting default values
public void insertData(string name, string surName = null, string age = null) {...

In your case however it would perhaps be easier to use a single "record object"?
Some patterns would call it a "business object", or "entity" or "model" in others, like the MVVM or MVC patterns

That way the entity, or data row, would know what to do if something is asking for one of its property values that is "empty", i.e. return null or zero or String.Empty or DateTime.Now or so


> Dear Experts,
> as you can see, this is C# code for inserting Data...
        public void insertData(string name,string surName, string age)
>         {
>             SqlConnection connection = new SqlConnection(getConnectionString());
>             SqlCommand cmd = new SqlCommand("INSERT INTO MYTABLE (NAME,SURNAME,AGE) VALUES (@NAME,@SURNAME,@AGE)", connection);
>             try
>             {
>                 cmd.Parameters.AddWithValue("@NAME", name.ToUpper());
>                 cmd.Parameters.AddWithValue("@SURNAME", surName.ToUpper());
>                 cmd.Parameters.AddWithValue("@AGE", age.ToUpper());
>                 connection.Open();
>                 cmd.ExecuteNonQuery();
>                 connection.Close();
>                 connection.Dispose();
>             }

> This method accepts 3 parameters and the TRY block adds 3 parameters in three lines of code.
> How do I re-write this code to send in N number of parameters ie if I need to deal with 5 fields, I send in five parameters and the try block add 5 Parameters...
> instead of a Hard coded SQL Command, if I send in another parameter ie the SQL COMMAND
> how to write code for this line
> SqlCommand cmd = new SqlCommand(.....????
> kulwant
> (Help Save the TIGER. Save Wildlife. Live & let Live)


variable no. of parameters Posted by Kulwant Singh @ 12/24/2013 7:43:45 AM
RE: variable no. of parameters Posted by Stefan Wuebbe @ 12/24/2013 11:00:04 AM
RE: variable no. of parameters Posted by Cetin Basoz @ 12/24/2013 2:31:56 PM