Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

C# - Passing parameters on launch Expand / Collapse
Author
Message
Posted Sunday, July 7, 2013 8:54 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 8:33 AM
Points: 50, Visits: 129
Hi,

I am writing a windows form application that will pass parameters on lauch. The parameter is A then it will run the program as default, otherwise if the length is greater I want to create a hardcoded variable to use globally. Can someone please let me understand how to do this?

Here is what I would like to do, but is not working as expected:

static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

if (args.Length < 1)
{

Application.Run(new Form1());

}
else
{
Form1 frmForm1 = new Form1();
MyVariable = "Test";
}

In this case, I would like to use "MyVariable" to help make decisions later in the application.

Thank you in advance.
Post #1471019
Posted Sunday, July 7, 2013 10:02 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 2:49 AM
Points: 1,179, Visits: 783
you are @wrong forum, this is more of SQL thing
Post #1471026
Posted Monday, July 8, 2013 8:27 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, July 21, 2014 8:50 AM
Points: 62, Visits: 751
At the risk of incurring the wrath of the pure SQL guts on the forum, for what it is worth, I'll bite...

You have not specified a definition for MyVariable; what is MyVariable a member of? It should be defined outside of the Main method at a class-level, and set inside the Main method. It should also be made accessible, either to all other classes (public), or to other classes in the project (internal).

I'm going to guess and say that the following might be applicable:

public class Program		// note the class name, here
{
public static string MyVariable { get; set; } // this is the public-scoped variable

static void Main(string[] args)
{
if (args.Length != 0)
{
MyVariable = "Test";
}

var theForm = new Form1();
Application.Run(theForm);
}
}

Now, in other places in your code, such as an event handler in the form:

public class Form1 : Form
{
protected void Button1_Click(object sender, EventArgs e)
{
if (string.Equals(Program.MyVariable, "Test")) // note how I've included the class-name
{
// ...do whatever you need to...
}
}
}

Post #1471216
Posted Monday, July 8, 2013 3:14 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 8:33 AM
Points: 50, Visits: 129
Hi,
Thank you for the risk you took as I sincerely appreciate the help and is working as needed. =)

I also need to have this parameter decide on what image to display and what database to point to.

For eample, if the value is not zero than show picturebox "B" instead of the default "A".

Same for database, I have two databases as I want to use the same front end for two different companies for the time being (which is what this parameter is really going to be used for. If the value is not zero than connect to based "B" instead of the default "A". However, I would like to keep the same connectionstring name.

Are these options possible? I have been digging in but isn't as clear cut as I thought it would be.

Thanks again for your help!!!
Post #1471379
Posted Monday, July 8, 2013 3:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:36 PM
Points: 13,111, Visits: 11,946
rayh 98086 (7/8/2013)
Hi,
Thank you for the risk you took as I sincerely appreciate the help and is working as needed. =)

I also need to have this parameter decide on what image to display and what database to point to.

For eample, if the value is not zero than show picturebox "B" instead of the default "A".

Same for database, I have two databases as I want to use the same front end for two different companies for the time being (which is what this parameter is really going to be used for. If the value is not zero than connect to based "B" instead of the default "A". However, I would like to keep the same connectionstring name.

Are these options possible? I have been digging in but isn't as clear cut as I thought it would be.

Thanks again for your help!!!


There really is no risk of posting programming questions where you did. You just don't have as good of odds at getting a good answer.

The picturebox part should be pretty straight forward. The easiest way to deal with your connection string issue is to have 2 connection strings in your config file. Then populate a string variable much the same way you deal with your parameters. Then your application always refers to the same string but the contents can vary passed on your parameter.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1471383
Posted Monday, July 8, 2013 3:52 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 8:33 AM
Points: 50, Visits: 129
Thank you Sean!

SO you are suggesting I create two connection strings for example:

CompanyOneConnectionString

and

CompanyTwoConnectionString

If yes, on what page would I sent the variable? Or would I need to do an If statement everytime there is a connectionstring to check for the args?

Thanks again!
Post #1471385
Posted Monday, July 8, 2013 9:02 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 13, 2014 8:33 AM
Points: 50, Visits: 129
Hi,

So I created the following two connection strings. Based on the launch paramters I want my program to know which database to use while using the same name ConnectionString.

Where do I create the variable? Would this be on my Form or in Program.cs? Currently I have multiple stored procedures and multiple connections that all call ConnectionString, so to be able to do this would be great.

<add name="MY_CSharpUtility.Properties.Settings.ConnectionString"
connectionString="Data Source=server1;Initial Catalog=Database1;Integrated Security=True"
providerName="System.Data.SqlClient" />

<add name="MY_CSharpUtility.Properties.Settings.Database2ConnectionString"
connectionString="Data Source=server1;Initial Catalog=Database2;Integrated Security=True"
providerName="System.Data.SqlClient" />

Thanks again!
Post #1471425
Posted Tuesday, July 9, 2013 5:57 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, July 21, 2014 8:50 AM
Points: 62, Visits: 751
This is more a question of code organisation -- something that only improves with practice.

Remember that you can create as many classes as you need to run your application (i.e. you can have more than Program and Form1, if it better organises your code).

For instance, in an app of this type, I might have a class that represents all the arguments that can be passed to the command-line, like follows:

public static class Options
{
public static void ParseArguments(IEnumerable<string> args)
{
ConnectionString = null;
TestMode = false;

int iter = 0;
foreach (string arg in args)
{
if (iter == 0)
{
Connection = ConfigurationManager.ConnectionStrings[arg];
if (string.IsNullOrEmpty(Connection)) throw new Exception("Invalid Connection argument");
}
else if (iter == 1)
{
TestMode = string.Equals(arg, "test");
}
iter++;
}
}

public static bool TestMode { get; private set; }
public static string ConnectionString { get; private set; }
}

In my Main function (the first place I can see the command-line args) I would initialise the class:

void Main(string[] args)
{
Options.ParseArguments(args);
Application.Run(new Form1());
}

...and then, everywhere else in my code, whenever I needed access to a command-line arg, I could reference Options:

void Button1_Click(object sender, EventArgs e)
{
if (Options.TestMode) MessageBox.Show("I am testing");

using (IDbConnection conn = new SqlConnection(Options.ConnectionString))
{
using (IDbCommand cmd = conn.CreateCommand("SELECT * FROM my_table"))
{
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// ... do stuff for each row
}
}
}
}
}

You don't have to structure it like I have (I myself probably wouldn't use statics), nor do you have to write your own parsing routines -- there are plenty other ways to do it, if that approach seems like too much hard work (including pre-written libraries to handle command-line arguments). Google is your friend.

Good luck.
J.
Post #1471566
Posted Tuesday, July 9, 2013 7:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:36 PM
Points: 13,111, Visits: 11,946
No need to make this complicated. Just figure out your connection string like you did you original variable.

public class Program		// note the class name, here
{
public static string MyVariable { get; set; } // this is the public-scoped variable
public static string ConnectionString { get; set; }

static void Main(string[] args)
{
if (args.Length != 0)
{
MyVariable = "Test";
ConnectionString = ConfigurationManager.ConnectionStrings["Conn2"].ConnectionString;
}
else
{
ConnectionString = ConfigurationManager.ConnectionStrings["Conn1"].ConnectionString;
}


var theForm = new Form1();
Application.Run(theForm);
}
}

Then just change your code slightly to use the static string each time you make a new connection instead of reading from the config.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1471609
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse