July 7, 2013 at 8:54 pm
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.
July 7, 2013 at 10:02 pm
you are @wrong forum, this is more of SQL thing
July 8, 2013 at 8:27 am
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...
}
}
}
July 8, 2013 at 3:14 pm
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!!!
July 8, 2013 at 3:32 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 8, 2013 at 3:52 pm
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!
July 8, 2013 at 9:02 pm
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!
July 9, 2013 at 5:57 am
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.
July 9, 2013 at 7:29 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply