0
  1. Ask user for username and password
  2. On click button it checks the database connected for this username and password,
  3. When correct it stores it's ID number in a variable, by default this variable is 0.
  4. Then I run a If-statement, if the ID is >= 0, it needs to open another form. if not it displays Error message.

MessageBox in first If case displays the correct ID (1,2,3 whatever) Now then on Form3, just a simple textbox1.text = "hi"; gives me the error:

Object reference not set to an instance of an object c#. and it displays the textbox line.

    int IDnumber = 0;

        SqlCommand dataCommand = new SqlCommand(" SELECT ID FROM leden WHERE [Username]='" + username_txt.Text + "' and [Password]='" + password_txt.Text + "'", SC);
        SC.Open();
        IDnumber = Convert.ToInt32(dataCommand.ExecuteScalar());
        SC.Close();

        if (IDnumber >= 1)
        {
            MessageBox.Show("Logged in, welcome ID number:" + IDnumber, "test");
            Form3 f3 = new Form3(IDnumber);
            f3.Show();                 
        }
        else
        {
            MessageBox.Show("Wrong Username and/or Password");
        }

}

form3:

public Form3(int _IDnumber)
{
    int IDnumber = _IDnumber;
    textBox1.Text = "hi";
}

Of course the textbox is a test, to show that it doesn't matter what textbox I use, it just gives me this error. Yes, I do still use unecrypted passwords, don't know how to do that yet

crthompson
  • 14,783
  • 6
  • 51
  • 74
CularBytes
  • 8,367
  • 6
  • 65
  • 94
  • You need to make sure the InitializeComponent method has run prior to accessing the control on the form (Form3). See http://stackoverflow.com/questions/1042706/where-and-when-is-initializecomponent-called-in-windows-forms-control-in-vb-net for example. Is textBox1 actually initialized in this method? – dash Apr 14 '14 at 15:17
  • I know this is not answering your question, but concatenating a sql statement with unsanitized user input is a really bad idea. Google 'sql injection' and use a parameterized query instead. – Paul Abbott Apr 14 '14 at 15:20

2 Answers2

4

You have not initialized the components on your form yet. add

InitializeComponent();

in the beginning of your contructor.

Murdock
  • 3,722
  • 1
  • 24
  • 50
  • My gut tells me this is the issue. `Form3` custom constructor is missing the `InitializeComponent()` call. – Anthony Apr 14 '14 at 15:18
  • it doesn't show the entire form3 editing text, but I have, only in another public form3, which is the same name, thatswhy it gives me the error, lol that simple, got it fixed: public Form3() { InitializeComponent(); } public Form3(int _IDnumber) { int IDnumber = _IDnumber; textBox1.Text = "hi"; } – CularBytes Apr 14 '14 at 15:18
  • @RageCompex You need it in the beginning of all your constructors or call the base constructor – Murdock Apr 14 '14 at 15:19
  • i was also thinking what could be the reason he is seeing exception, but no reached that..good job..:) – Ehsan Sajjad Apr 14 '14 at 15:22
0

Setting controls in a Forms constructor is not a wise idea, instead implement these things in the OnLoad event.

MSDN: http://msdn.microsoft.com/en-us/library/360kwx3z(v=vs.90).aspx, you don't want to put code in constructor, use OnLoad.

T McKeown
  • 12,312
  • 1
  • 21
  • 30
  • down voter, that is absolutely a correct statement. I have experienced that many time, and of course the InitializeComponent() method was there. – T McKeown Apr 14 '14 at 15:19
  • 1
    Correct statement != Correct answer to the question at hand. This is more of a style choice, you can find [different](http://stackoverflow.com/questions/13860609) [opinions](http://stackoverflow.com/questions/2623808) on where to put initialization logic. – Anthony Apr 14 '14 at 15:23
  • This answer is irrelevant because it is not answering the question. – Dustin Kingen Apr 14 '14 at 15:24
  • This is true however this is not the reason for getting the error. – Murdock Apr 14 '14 at 15:24
  • I am not saying that the InitializeComponent() isn't the cause, i'm surprised it wasn't there. – T McKeown Apr 14 '14 at 15:24
  • guys, i need these points or else my child will be taken from me... ? – T McKeown Apr 14 '14 at 15:25