1 Replies - 2975 Views - Last Post: 30 December 2012 - 07:18 AM Rate Topic: -----

#1 NejcZ   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-December 12

Data input in textbox question c#

Posted 30 December 2012 - 01:55 AM

Hello all.
I'm using this code for my login form button:
private void btnOK_Click(object sender, EventArgs e)
            SqlConnection con = Program.GetConnection;
            SqlDataReader dr = null;

                SqlCommand cmd = new SqlCommand("SELECT * FROM tabelaZaposlenih WHERE up_ime='" +
                    txtName.Text + "' AND geslo='" + txtpassword.Text + "'", con);
                dr = cmd.ExecuteReader();
                if (dr.Read())
                    Program.UserLoginName = dr.GetString(1);
                    new Form1().Show();

                    MessageBox.Show("Invalid username and password!");
            catch (Exception ex)


After login i want do display logged in user name in another forms textbox field. I would also like to get users ID, so i can create other functionalities. I understand i have to use this for putting text into textbox.

textBox1.Text = Program.UserLoginName;

But where do i put that? Only way i can get this to work is with button:

private void button1_Click(object sender, EventArgs e)
            textBox1.Text = Program.UserLoginName;

But that is quite useless since i nedd the program to do that on it's own. Any help would be apriciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Data input in textbox question c#

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Data input in textbox question c#

Posted 30 December 2012 - 07:18 AM

Have your loginForm raise an event then react to it, in place of reacting to the button_click event.

How do I?

[*]Q: ...get Form 'A' to make a change or talk to Form 'B'

NOTE: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.

Also - fix up your control names as soon as you place them on your form - Item 2 below.

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.

  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot


    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

This post has been edited by tlhIn`toq: 30 December 2012 - 07:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1