if statement > from1 read from fields..

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 15
Reputation: njitram is an unknown quantity at this point 
Solved Threads: 0
njitram njitram is offline Offline
Newbie Poster

if statement > from1 read from fields..

 
0
  #1
Jul 14th, 2008
Hello i just started up today with C#,
Im femilair with PHP so i can do some things i have google etc.

But now im stuck.. with this:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using MySql.Data.MySqlClient;
  9.  
  10. namespace WindowsApplication2
  11. {
  12. public partial class Form1 : Form
  13. {
  14.  
  15. public Form1()
  16. {
  17. InitializeComponent();
  18.  
  19. username.Text = "";
  20. password.PasswordChar = '*';
  21.  
  22. string loginus = "";
  23. string loginpas = "";
  24.  
  25. loginus = username.Text;
  26. loginpas = password.Text;
  27.  
  28. if (loginus == false)
  29. {
  30. string MyConString = "SERVER=localhost;" +
  31. "DATABASE=ot;" +
  32. "UID=" + loginus + ";" +
  33. "PASSWORD=" + loginpas + ";";
  34. MySqlConnection connection = new MySqlConnection(MyConString);
  35. MySqlCommand command = connection.CreateCommand();
  36. MySqlDataReader Reader;
  37. command.CommandText = "select * from players ORDER BY id";
  38. connection.Open();
  39. Reader = command.ExecuteReader();
  40. while (Reader.Read())
  41. {
  42. string idPlayers = "";
  43. idPlayers = Reader.GetString(0) + "\t" + Reader.GetString(1);
  44. listBox1.Items.Add(idPlayers);
  45. }
  46. connection.Close();
  47. }
  48. }
  49.  
  50.  
  51.  
  52. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  53. {
  54.  
  55. }
  56.  
  57. private void button1_Click(object sender, EventArgs e)
  58. {
  59. string loginus = "";
  60. string loginpas = "";
  61.  
  62. loginus = username.Text;
  63. loginpas = password.Text;
  64. }
  65.  
  66. }
  67. }

I made a form with a user/password fields and a logon button,
and a field that reads from DB. Everything did work, but now i want to make the user/password field active to work (to access the db) not sure how yet.. so if you guys can help me with that also it would be greate!

With this code im getting the error for the if statement i made
(not sure how to fix it properly)


Im not sure if some one can help me out
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 89
Reputation: camilojvarona is an unknown quantity at this point 
Solved Threads: 10
camilojvarona camilojvarona is offline Offline
Junior Poster in Training

Re: if statement > from1 read from fields..

 
0
  #2
Jul 14th, 2008
Hi,

In C# if you are gonig to use the == operator its not defined for type string and boll(ie string == bool)but since you are trying to check it the string 'loginus' is not empty you can use one of this options

1. if(loginus != "")
2. if(loginus != string.Empty)
3. if(!string.IsNullOrEmpty())
4. if(string.IsNullOrEmpty() == false)
5. if(string.Count != 0)

All this are valids if statements pick the one you like better.

Regards,
Camilo
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 15
Reputation: njitram is an unknown quantity at this point 
Solved Threads: 0
njitram njitram is offline Offline
Newbie Poster

Re: if statement > from1 read from fields..

 
0
  #3
Jul 14th, 2008
Originally Posted by camilojvarona View Post
Hi,

In C# if you are gonig to use the == operator its not defined for type string and boll(ie string == bool)but since you are trying to check it the string 'loginus' is not empty you can use one of this options

1. if(loginus != "")
2. if(loginus != string.Empty)
3. if(!string.IsNullOrEmpty())
4. if(string.IsNullOrEmpty() == false)
5. if(string.Count != 0)

All this are valids if statements pick the one you like better.

Regards,
Camilo
Camilo big Thanks! now i have another problem.. =)

When im trying to send the information from username/password to my MySQL string it just wont refresh from1() anny idea how i should do it? i have it now like this:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using MySql.Data.MySqlClient;
  9.  
  10. namespace WindowsApplication2
  11. {
  12. public partial class Form1 : Form
  13. {
  14. string loginus = "";
  15. string loginpas = "";
  16.  
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. loginus = username.Text;
  20. loginpas = password.Text;
  21. }
  22.  
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. if (loginus != string.Empty)
  27. {
  28. string MyConString = "SERVER=localhost;" +
  29. "DATABASE=ot;" +
  30. "UID=" + loginus + ";" +
  31. "PASSWORD=" + loginpas + ";";
  32. MySqlConnection connection = new MySqlConnection(MyConString);
  33. MySqlCommand command = connection.CreateCommand();
  34. MySqlDataReader Reader;
  35. command.CommandText = "select * from players ORDER BY id";
  36. connection.Open();
  37. Reader = command.ExecuteReader();
  38. while (Reader.Read())
  39. {
  40. string idPlayers = "";
  41. idPlayers = Reader.GetString(0) + "\t" + Reader.GetString(1);
  42. listBox1.Items.Add(idPlayers);
  43. }
  44. connection.Close();
  45. }
  46. }
  47.  
  48.  
  49.  
  50. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  51. {
  52.  
  53. }
  54.  
  55.  
  56.  
  57. }
  58. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 89
Reputation: camilojvarona is an unknown quantity at this point 
Solved Threads: 10
camilojvarona camilojvarona is offline Offline
Junior Poster in Training

Re: if statement > from1 read from fields..

 
0
  #4
Jul 14th, 2008
Yes

The code whe you are populating the LisBox is the Constructor and only executes whe you crate a new objec(when you use the 'new' keyword) and at this point the 'logingus' is empty so it would not 'enter' the 'if'. A solution is to change that code to the button1Click method.

Camilo
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 15
Reputation: njitram is an unknown quantity at this point 
Solved Threads: 0
njitram njitram is offline Offline
Newbie Poster

Re: if statement > from1 read from fields..

 
0
  #5
Jul 14th, 2008
Ahh alright then!
Big Thanks !
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC