Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:21:07+00:00 2026-06-14T08:21:07+00:00

having difficulty with a bit of code I have. I think variables are stuck

  • 0

having difficulty with a bit of code I have. I think variables are stuck inside the method I’m invoking and when I go to run that method again variables are no longer there.

Below is an example of my main:

        public partial class Login : Form
{

    public Login()
    {
        InitializeComponent();
    }
    sqlconnect sql = new sqlconnect();
    public string pass;
    public string user;


    public void button1_Click(object sender, EventArgs e)
    {
        //Username and password textboxes send to public strings
        user = textBox1.Text;
        pass = textBox2.Text;

            try
            {
                //try connecting to SQL database using SQL class
                sql.GetSqlConnection();
                sql.myConnection.Open();
                this.Hide();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Here is the SQL connection class:

    namespace NHS_Workshop_System
         {

public class sqlconnect
{
    public SqlConnection myConnection { get; set; }
    Settings1 set = new Settings1();
    Login var = new Login();

    public SqlConnection GetSqlConnection()
    {
        if (myConnection == null)
            myConnection = new SqlConnection("user id="+(var.user)+"; password="+(var.pass)+";server="+(set.SelectServer)+";Trusted_Connection="+(set.SelectContype)+";database="+(set.SelectDataBase)+"; connection timeout="+(set.Condrop)+"");

        return myConnection;
    }


}

}

So when I try to login it may work first time but if another form tries to run that method the username and password are missing. I’m not sure how to make the variables global so that every time I run the method it calls those details without fail. So the second time it uses sqlconnection on another form the ex message states that username and password are non existent. Is this a scoping issue if so can someone shed some light on how i might go about this maybe a new method of gaining access to an SQL server? Thank you tearing out my hair

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:21:08+00:00Added an answer on June 14, 2026 at 8:21 am

    Below is the answer for your question the way you have it now. But I strongly recommend you to think about what you are doing since your class logics doesn’t seem to be very good. I see that your trying to implement Singleton which is good, but you can still make more instances of sqlconnect if you would like.


    In your Login class, pass this instance of Login so the sqlconnector knows the user and pass that is declared here.

    public partial class Login : Form
    {
    
    // Old style - sqlconnect sql = new sqlconnect();
    sqlconnect sql;
    public string pass; // These arent declared and wont be of use for your sql connect class
    public string user;
    
    public Login( String pass, String user )
    {
        this.pass = pass;
        this.user = user;
        // Now that we have a login with declared variables, pass it to the sqlconnect object
        this.sql = new sqlconnect(this);
        InitializeComponent();
    }
    

    Now in your sqlconnect class, before your Login variable was just empty. Now you have received an instance of it in your constructor that has it’s variables set,

    public class sqlconnect
    {
        public SqlConnection myConnection { get; set; }
        Settings1 set = new Settings1();
        // Here you maked a new one, use the existing instead from which this class is called.
        // changed variable named from var to login..
        Login login; 
    
    
        // *NEW* Constructor where you pass your login object
        public sqlconnect(Login login)
        {
            this.login = login;
        }
    
        public SqlConnection GetSqlConnection()
        {
            if (myConnection == null)
                myConnection = new SqlConnection("user id="+(login.user)+"; password="+(login.pass)+";server="+(set.SelectServer)+";Trusted_Connection="+(set.SelectContype)+";database="+(set.SelectDataBase)+"; connection timeout="+(set.Condrop)+"");
    
            return myConnection;
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following bit of code that I am having some difficulty with.
I am having a bit of difficulty with this current code I have set
Having a bit of difficulty finding out the proper way to mix in code
I'm having a bit of difficulty with some java code class foo{ public bar()
I'm having a bit of difficulty setting a variable from the code behind and
Hey, I'm having some difficulty with CSS and IE6 compatibility. URL : http://bit.ly/dlX7cS Problem
I'm having a bit of difficulty figuring out how to use this Regular Expression
I'm having a bit of difficulty passing a reference type between webservices. My set
I'm having a bit of difficulty understanding how the python __init__ ( ) function
I'm having a bit of difficulty coming up with the right answer to this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.