Gentlemen,
I want to get a value from the database but i need the info permanent.
I have a database with 3 columns .With 2 of the columns if it match i will get the value or info of the 3rd column.
But when i close the connection i will lose the value.
The issue is that i want to when log in the system with form2 the name of the username will come up on Form1.
Loading
Arnold PetronaPosted Jul 18, 2008, 5:02 PM
Satish KathiPosted Jul 18, 2008, 4:28 PM
Create static string variable in Program.cs to hold the User Name.
static class Program
{
public static string UserName;
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
When the login is success in login page assign user name to the static user name variable created in program.cs
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//your code to check the login goes here
//Assign the user name retreived from the data base to the static variable in Program.cs
//If UserName has Value
if (! string.IsNullOrEmpty(Program.UserName))
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
this.DialogResult = DialogResult.No;
MessageBox.Show("Invalid Login Application will close", "Log In Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
this.Close();
}
}
}
In any form you can access the Static user Name in Program.cs
Code in Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Login f = new Login();
DialogResult dr = f.ShowDialog();
if (dr == DialogResult.OK)
{
//Assign the User Name in Program.cs to Label to display on the form
lblUSerName.Text = Program.UserName;
}
else
{
this.Close();
}
}
}
Hope this helps