Cant figure out why getConnectionString keeps passing NULL












-1















Im trying to get my connection string from one form to another but it keeps passing NULL Im new to working with different classes so it could be a verry simple mistake.



Form 1



public partial class Form1 : Form
{
private string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";
public Form1()
{
InitializeComponent();
}

public string getConnectionString()
{
return ConnectionString;
}

private void btn_login_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("Select count(*) From tblLogin where Username ='" + txt_username.Text + "' and Password ='" + txt_password.Text + "'", con);
DataTable dt = new DataTable();
sqa.Fill(dt);

if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Form2 main = new Form2();
main.Show();
}

else
{
MessageBox.Show("Username or Password is incorrect");
txt_username.Clear();
txt_password.Clear();
}
}
}


Form 2



public partial class Form2 : Form
{
private Form1 form1;
public Form2()
{
InitializeComponent();
}

private void btn_search_Click(object sender, EventArgs e)
{
if (rb_Artist.Checked == true)
{
String ConnectionString = form1.getConnectionString();
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("SELECT * FROM tblArtist where Name like" + txt_search.Text, con);
DataTable dt = new DataTable();
sqa.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}









share|improve this question

























  • What specifically is null?

    – mjwills
    Nov 15 '18 at 12:04











  • Change private string to private const string.

    – mjwills
    Nov 15 '18 at 12:05








  • 1





    Possible duplicate of What is a NullReferenceException, and how do I fix it?

    – mjwills
    Nov 15 '18 at 12:08
















-1















Im trying to get my connection string from one form to another but it keeps passing NULL Im new to working with different classes so it could be a verry simple mistake.



Form 1



public partial class Form1 : Form
{
private string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";
public Form1()
{
InitializeComponent();
}

public string getConnectionString()
{
return ConnectionString;
}

private void btn_login_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("Select count(*) From tblLogin where Username ='" + txt_username.Text + "' and Password ='" + txt_password.Text + "'", con);
DataTable dt = new DataTable();
sqa.Fill(dt);

if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Form2 main = new Form2();
main.Show();
}

else
{
MessageBox.Show("Username or Password is incorrect");
txt_username.Clear();
txt_password.Clear();
}
}
}


Form 2



public partial class Form2 : Form
{
private Form1 form1;
public Form2()
{
InitializeComponent();
}

private void btn_search_Click(object sender, EventArgs e)
{
if (rb_Artist.Checked == true)
{
String ConnectionString = form1.getConnectionString();
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("SELECT * FROM tblArtist where Name like" + txt_search.Text, con);
DataTable dt = new DataTable();
sqa.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}









share|improve this question

























  • What specifically is null?

    – mjwills
    Nov 15 '18 at 12:04











  • Change private string to private const string.

    – mjwills
    Nov 15 '18 at 12:05








  • 1





    Possible duplicate of What is a NullReferenceException, and how do I fix it?

    – mjwills
    Nov 15 '18 at 12:08














-1












-1








-1








Im trying to get my connection string from one form to another but it keeps passing NULL Im new to working with different classes so it could be a verry simple mistake.



Form 1



public partial class Form1 : Form
{
private string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";
public Form1()
{
InitializeComponent();
}

public string getConnectionString()
{
return ConnectionString;
}

private void btn_login_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("Select count(*) From tblLogin where Username ='" + txt_username.Text + "' and Password ='" + txt_password.Text + "'", con);
DataTable dt = new DataTable();
sqa.Fill(dt);

if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Form2 main = new Form2();
main.Show();
}

else
{
MessageBox.Show("Username or Password is incorrect");
txt_username.Clear();
txt_password.Clear();
}
}
}


Form 2



public partial class Form2 : Form
{
private Form1 form1;
public Form2()
{
InitializeComponent();
}

private void btn_search_Click(object sender, EventArgs e)
{
if (rb_Artist.Checked == true)
{
String ConnectionString = form1.getConnectionString();
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("SELECT * FROM tblArtist where Name like" + txt_search.Text, con);
DataTable dt = new DataTable();
sqa.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}









share|improve this question
















Im trying to get my connection string from one form to another but it keeps passing NULL Im new to working with different classes so it could be a verry simple mistake.



Form 1



public partial class Form1 : Form
{
private string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";
public Form1()
{
InitializeComponent();
}

public string getConnectionString()
{
return ConnectionString;
}

private void btn_login_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("Select count(*) From tblLogin where Username ='" + txt_username.Text + "' and Password ='" + txt_password.Text + "'", con);
DataTable dt = new DataTable();
sqa.Fill(dt);

if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Form2 main = new Form2();
main.Show();
}

else
{
MessageBox.Show("Username or Password is incorrect");
txt_username.Clear();
txt_password.Clear();
}
}
}


Form 2



public partial class Form2 : Form
{
private Form1 form1;
public Form2()
{
InitializeComponent();
}

private void btn_search_Click(object sender, EventArgs e)
{
if (rb_Artist.Checked == true)
{
String ConnectionString = form1.getConnectionString();
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("SELECT * FROM tblArtist where Name like" + txt_search.Text, con);
DataTable dt = new DataTable();
sqa.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}






c# database forms class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:06









er-sho

6,5452618




6,5452618










asked Nov 15 '18 at 11:59









im_ Rubenim_ Ruben

188




188













  • What specifically is null?

    – mjwills
    Nov 15 '18 at 12:04











  • Change private string to private const string.

    – mjwills
    Nov 15 '18 at 12:05








  • 1





    Possible duplicate of What is a NullReferenceException, and how do I fix it?

    – mjwills
    Nov 15 '18 at 12:08



















  • What specifically is null?

    – mjwills
    Nov 15 '18 at 12:04











  • Change private string to private const string.

    – mjwills
    Nov 15 '18 at 12:05








  • 1





    Possible duplicate of What is a NullReferenceException, and how do I fix it?

    – mjwills
    Nov 15 '18 at 12:08

















What specifically is null?

– mjwills
Nov 15 '18 at 12:04





What specifically is null?

– mjwills
Nov 15 '18 at 12:04













Change private string to private const string.

– mjwills
Nov 15 '18 at 12:05







Change private string to private const string.

– mjwills
Nov 15 '18 at 12:05






1




1





Possible duplicate of What is a NullReferenceException, and how do I fix it?

– mjwills
Nov 15 '18 at 12:08





Possible duplicate of What is a NullReferenceException, and how do I fix it?

– mjwills
Nov 15 '18 at 12:08












2 Answers
2






active

oldest

votes


















3














It's because you never actually initialize Form1 in your Form2.



Change private Form1 form1; to private Form1 form1 = new Form1();






share|improve this answer



















  • 2





    Thanks alot I didnt even realise it after trying for over an hour!

    – im_ Ruben
    Nov 15 '18 at 12:11











  • No problem also just a tip if you want to use variables like connection strings in several forms or classes configure them in the app.config file. You can access and change them more easy than trying to pass them from form to form. .

    – Niklas7
    Nov 15 '18 at 12:24





















-1














public static string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";



using in Form2
not create form1 object



String ConnectionString = form1.ConnectionString



this variable try to solve this problem






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319044%2fcant-figure-out-why-getconnectionstring-keeps-passing-null%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    It's because you never actually initialize Form1 in your Form2.



    Change private Form1 form1; to private Form1 form1 = new Form1();






    share|improve this answer



















    • 2





      Thanks alot I didnt even realise it after trying for over an hour!

      – im_ Ruben
      Nov 15 '18 at 12:11











    • No problem also just a tip if you want to use variables like connection strings in several forms or classes configure them in the app.config file. You can access and change them more easy than trying to pass them from form to form. .

      – Niklas7
      Nov 15 '18 at 12:24


















    3














    It's because you never actually initialize Form1 in your Form2.



    Change private Form1 form1; to private Form1 form1 = new Form1();






    share|improve this answer



















    • 2





      Thanks alot I didnt even realise it after trying for over an hour!

      – im_ Ruben
      Nov 15 '18 at 12:11











    • No problem also just a tip if you want to use variables like connection strings in several forms or classes configure them in the app.config file. You can access and change them more easy than trying to pass them from form to form. .

      – Niklas7
      Nov 15 '18 at 12:24
















    3












    3








    3







    It's because you never actually initialize Form1 in your Form2.



    Change private Form1 form1; to private Form1 form1 = new Form1();






    share|improve this answer













    It's because you never actually initialize Form1 in your Form2.



    Change private Form1 form1; to private Form1 form1 = new Form1();







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 12:06









    Niklas7Niklas7

    1262




    1262








    • 2





      Thanks alot I didnt even realise it after trying for over an hour!

      – im_ Ruben
      Nov 15 '18 at 12:11











    • No problem also just a tip if you want to use variables like connection strings in several forms or classes configure them in the app.config file. You can access and change them more easy than trying to pass them from form to form. .

      – Niklas7
      Nov 15 '18 at 12:24
















    • 2





      Thanks alot I didnt even realise it after trying for over an hour!

      – im_ Ruben
      Nov 15 '18 at 12:11











    • No problem also just a tip if you want to use variables like connection strings in several forms or classes configure them in the app.config file. You can access and change them more easy than trying to pass them from form to form. .

      – Niklas7
      Nov 15 '18 at 12:24










    2




    2





    Thanks alot I didnt even realise it after trying for over an hour!

    – im_ Ruben
    Nov 15 '18 at 12:11





    Thanks alot I didnt even realise it after trying for over an hour!

    – im_ Ruben
    Nov 15 '18 at 12:11













    No problem also just a tip if you want to use variables like connection strings in several forms or classes configure them in the app.config file. You can access and change them more easy than trying to pass them from form to form. .

    – Niklas7
    Nov 15 '18 at 12:24







    No problem also just a tip if you want to use variables like connection strings in several forms or classes configure them in the app.config file. You can access and change them more easy than trying to pass them from form to form. .

    – Niklas7
    Nov 15 '18 at 12:24















    -1














    public static string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";



    using in Form2
    not create form1 object



    String ConnectionString = form1.ConnectionString



    this variable try to solve this problem






    share|improve this answer




























      -1














      public static string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";



      using in Form2
      not create form1 object



      String ConnectionString = form1.ConnectionString



      this variable try to solve this problem






      share|improve this answer


























        -1












        -1








        -1







        public static string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";



        using in Form2
        not create form1 object



        String ConnectionString = form1.ConnectionString



        this variable try to solve this problem






        share|improve this answer













        public static string ConnectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersRubenDocumentsdbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";



        using in Form2
        not create form1 object



        String ConnectionString = form1.ConnectionString



        this variable try to solve this problem







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 12:22









        mitulpatelmitulpatel

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319044%2fcant-figure-out-why-getconnectionstring-keeps-passing-null%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly