Help with C# Login

Help, needed have been trying for 2 weeks!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3604 Views - Last Post: 25 December 2008 - 09:04 AM Rate Topic: -----

#1 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Help with C# Login

Post icon  Posted 19 December 2008 - 05:09 AM

Hello again, i am in the process of creating a client database as a side project. I am very new to c# and programming in general and am having imense difficulty with getting the login feature for the windows application working.
(I am using my local host and mysql, and oledb for connecting to the database. )

I get the error the name 'user' and 'password' does not exist in the current context? Im trying to validate the input gathered from the textbox1 and textbox2 against the sql query of the database but at current it just ignores the sql command and opens up the form no matter what input i put in...

I have been trying to figure this out for almost two weeks and have looked all over the internet to find a login connection using oledb but have only been able to find 'mysqlconnection' ?

Any help would be greatly appreciated :)

My code is below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Name of Namespace
{
	public partial class Form1 : Form
	{
		public static string ConnectionString = "Provider=MySQLProv;Data Source=DATABASE NAME;User Id=root;Password=password;";
		public Form1()
		{
			InitializeComponent();

		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{


		}

		private void button1_Click(object sender, EventArgs e)
		{
			OleDbConnection myConnection = new OleDbConnection(ConnectionString);

			string uname;
			string pword;

			try
			{
				uname = textBox1.Text;
				pword = textBox2.Text;

					{
						OleDbCommand myCommand = new OleDbCommand("Select * from  user where username = ' " + textBox1.Text + " 'and  password=' " + textBox2.Text + " ' ");

						if (user = uname && password = pword)
							{
								main_screen main = new main_screen();
								main.Show();
							}

						else
							{
								Application.Exit();
							}
					}
			}



				catch (Exception ex)
					{
						myConnection.Close();
						MessageBox.Show("Wrong Input Entered", "Window Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
			}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help with C# Login

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Help with C# Login

Posted 19 December 2008 - 05:59 AM

View Postcobalt-rose, on 19 Dec, 2008 - 07:09 AM, said:

Hello again, i am in the process of creating a client database as a side project. I am very new to c# and programming in general and am having imense difficulty with getting the login feature for the windows application working.
(I am using my local host and mysql, and oledb for connecting to the database. )

I get the error the name 'user' and 'password' does not exist in the current context? Im trying to validate the input gathered from the textbox1 and textbox2 against the sql query of the database but at current it just ignores the sql command and opens up the form no matter what input i put in...

I have been trying to figure this out for almost two weeks and have looked all over the internet to find a login connection using oledb but have only been able to find 'mysqlconnection' ?

Any help would be greatly appreciated :)

My code is below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Name of Namespace
{
	public partial class Form1 : Form
	{
		public static string ConnectionString = "Provider=MySQLProv;Data Source=DATABASE NAME;User Id=root;Password=password;";
		public Form1()
		{
			InitializeComponent();

		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{


		}

		private void button1_Click(object sender, EventArgs e)
		{
			OleDbConnection myConnection = new OleDbConnection(ConnectionString);

			string uname;
			string pword;

			try
			{
				uname = textBox1.Text;
				pword = textBox2.Text;

					{
						OleDbCommand myCommand = new OleDbCommand("Select count(*) from  user where username = ' " + textBox1.Text + " 'and  password=' " + textBox2.Text + " ' ");

						if (user = uname && password = pword)
							{
								main_screen main = new main_screen();
								main.Show();
							}

						else
							{
								Application.Exit();
							}
					}
			}



				catch (Exception ex)
					{
						myConnection.Close();
						MessageBox.Show("Wrong Input Entered", "Window Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
			}
	}
}


your code for checking the database should be...

OleDbCommand myCommand = new OleDbCommand("Select count(*) from  user where username = ' " + textBox1.Text + " 'and  password=' " + textBox2.Text + " ' ");

myCommand.Connection = myConnection;

int count = myCommand.ExecuteScalar();

// Notice the double equal signs.
// Double equal signs are a comparison.
// Single equal signs are an assignment.
if (count > 0)
{
	main_screen main = new main_screen();
	main.Show();
	this.Hide();
}
else
{
	Application.Exit();
}


This post has been edited by eclipsed4utoo: 19 December 2008 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#3 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Re: Help with C# Login

Posted 19 December 2008 - 07:35 AM

Thanks you for the reply eclipsed4utoo, muchly appreciated.

I have changed the code to suit what you have suggested. I did come up with a problem in that when compiled it said unable to change 'object' to 'int' or something but i sorted that by adding a cast (i think it was called). Anyway it now runs without any errors but no matter what i enter in the textbox fields, the wrong input entered field appears and the 'main_screen' does not appear?

Here is what i have currently...
		private void button1_Click(object sender, EventArgs e)
		{
			OleDbConnection myConnection = new OleDbConnection(ConnectionString);

			string uname;
			string pword;

			try
			{
				uname = textBox1.Text;
				pword = textBox2.Text;

					{
						OleDbCommand myCommand = new OleDbCommand("Select count(*) from  user where username = ' " + textBox1.Text + " 'and  password=' " + textBox2.Text + " ' ");
						myCommand.Connection = myConnection;
						int count = Convert.ToInt16 (myCommand.ExecuteScalar());

						// Notice the double equal signs.
						// Double equal signs are a comparison.
						// Single equal signs are an assignment.
						if (count > 0)
							{
							main_screen main = new main_screen();
							main.Show();
							this.Hide();
							}

						else
							{
							Application.Exit();
							}
					}
			}
						catch (Exception ex)
						{
						myConnection.Close();
						MessageBox.Show("Wrong Input Entered", "Window Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
						}
			}
	}


Do i not need to put a myConnection.Open(); before the sql seaches the database?
Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Help with C# Login

Posted 19 December 2008 - 11:21 AM

View Postcobalt-rose, on 19 Dec, 2008 - 09:35 AM, said:

Thanks you for the reply eclipsed4utoo, muchly appreciated.

I have changed the code to suit what you have suggested. I did come up with a problem in that when compiled it said unable to change 'object' to 'int' or something but i sorted that by adding a cast (i think it was called). Anyway it now runs without any errors but no matter what i enter in the textbox fields, the wrong input entered field appears and the 'main_screen' does not appear?

Here is what i have currently...
		private void button1_Click(object sender, EventArgs e)
		{
			OleDbConnection myConnection = new OleDbConnection(ConnectionString);

			string uname;
			string pword;

			try
			{
				uname = textBox1.Text;
				pword = textBox2.Text;

					{
						OleDbCommand myCommand = new OleDbCommand("Select count(*) from  user where username = ' " + textBox1.Text + " 'and  password=' " + textBox2.Text + " ' ");
						myCommand.Connection = myConnection;
						int count = Convert.ToInt16 (myCommand.ExecuteScalar());

						// Notice the double equal signs.
						// Double equal signs are a comparison.
						// Single equal signs are an assignment.
						if (count > 0)
							{
							main_screen main = new main_screen();
							main.Show();
							this.Hide();
							}

						else
							{
							Application.Exit();
							}
					}
			}
						catch (Exception ex)
						{
						myConnection.Close();
						MessageBox.Show("Wrong Input Entered", "Window Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
						}
			}
	}


Do i not need to put a myConnection.Open(); before the sql seaches the database?
Thank you.


Sorry. There are two parts that I always forget when I code. One is casting the "ExecuteScalar()" to the correct datatype, and the second is forgetting to open the connection.

So yes, you should call the "Open()" method for the connection before the "ExecuteScalar()" is executed.
Was This Post Helpful? 0
  • +
  • -

#5 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Re: Help with C# Login

Posted 19 December 2008 - 12:02 PM

Hey i have added the cast and the myConnection.Open(); before the executescalar but there is stil no validation. It doesnt matter what i put in the text fields the 'wrong input' box appears (even if i put in the right usernam and password...?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Help with C# Login

Posted 19 December 2008 - 01:05 PM

I might suggest printing the exception's message (ex.Message) in your error so that perhaps you can narrow down the issue.
Was This Post Helpful? 0
  • +
  • -

#7 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Re: Help with C# Login

Posted 19 December 2008 - 02:37 PM

Ok thanks for the tip, (I honestly didn't think it would make much difference...) But it has and i am greatful!! :D . The error now appeares when i enter text in the username and password...when clicked the input error box appears and at the top of the window it says..." the mysqlprov provider is not on the local machine "

Now i have just googled it and some have said that it is because the user account being used does not have sufficient permission to access... But i am logged in a super admin as i messed up the framework on my usual login for vs 2008.

I have installed VS 2008 and XAMPP as normal user but have created this app and trying to run using super admin. This doesn't really matter does it?

Another reasons someone had said was " you can't use OleDB as a bridge to your MySQL.. neither MySQL made a provider to service their very unique database engine..
you can use the asp.net Odbc class or the connector.NET provided by MySQL. "
Thanks in advanced,
Was This Post Helpful? 0
  • +
  • -

#8 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Re: Help with C# Login

Posted 19 December 2008 - 03:22 PM

View Postcobalt-rose, on 19 Dec, 2008 - 01:37 PM, said:

Ok thanks for the tip, (I honestly didn't think it would make much difference...) But it has and i am greatful!! :D . The error now appeares when i enter text in the username and password...when clicked the input error box appears and at the top of the window it says..." the mysqlprov provider is not on the local machine "

Now i have just googled it and some have said that it is because the user account being used does not have sufficient permission to access... But i am logged in a super admin as i messed up the framework on my usual login for vs 2008.

I have installed VS 2008 and XAMPP as normal user but have created this app and trying to run using super admin. This doesn't really matter does it?

Another reasons someone had said was " you can't use OleDB as a bridge to your MySQL.. neither MySQL made a provider to service their very unique database engine..
you can use the asp.net Odbc class or the connector.NET provided by MySQL. "
Thanks in advanced,


I am starting to think this maybe a user access problem as i am unable to add a ole db connection i get this when using vs 2008... The error message "Cannot start your application. The workgroup information file is missing or opened exclusively by another user."

Posted Image

Also am i using the right ole db provider|??

This post has been edited by cobalt-rose: 20 December 2008 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Help with C# Login

Posted 20 December 2008 - 08:13 AM

no, the Jet provider is for Microsoft Access.

If you are using MySql, then you need to download it's .Net provider. I don't think it is included in .Net. I've never used MySQL so I can't help out much.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Help with C# Login

Posted 20 December 2008 - 08:28 AM

MySQL has an ADO .NET Connector, maybe you want to use that? Like eclipsed4utoo, I've not used MySQL with .NET, or on Windows, so I'm not much help beyond that.
Was This Post Helpful? 0
  • +
  • -

#11 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Re: Help with C# Login

Posted 22 December 2008 - 05:24 PM

Hey i have been advised to use the mysql.dll instead of ole db to connect to the database as it is quicker.

I have tried to adapt the code to using mysqlconnection and am still clueless as how to authorise the login, could somebody help me in terms of matching the input of textbox1.text and texbox2.text to what is in the database.
Thank you for any help given
C~Rose


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

namespace _Consultants
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)
{
string uname;
string pword;

try
{
uname = textBox1.Text;
pword = textBox2.Text;

{
string connectionString = "Data Source=localhost" + ";Database=_consultants" + ";User ID=root" + ";Password=password;";
MySqlConnection MySqlConn = new MySqlConnection(connectionString);
string strSQL = ("SELECT (*) FROM user where username = ' " + textBox1.Text + " 'and password=' " + textBox2.Text + " ' ");
MySqlCommand mysqlCommand = new MySqlCommand(strSQL, MySqlConn);

MySqlConn.Open();
String strResult = String.Empty;
strResult = (String)mysqlCommand.ExecuteScalar();
MySqlConn.Close();

if (strResult.Length == 0)
{
textBoxt3.Text = "INCORRECT USER/PASS!";
//could redirect to register page
} 
else 
{
textBox3.Text = "YOU ARE LOGGED IN!";
//set loggin in sessions variables
}
}

}

catch (Exception ex)
{
MessageBox.Show("Wrong Input Entered", "Window Information" + ex.Message.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Information);
MySqlConn.Close();
}

}

private void textBox3_TextChanged(object sender, EventArgs e)
{
}
}
}


Was This Post Helpful? 0
  • +
  • -

#12 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Re: Help with C# Login

Posted 24 December 2008 - 02:21 AM

I have altered some of the code a little but i am now facing an error when i run the program "YOU HAVE AND ERROR IN YOUR SQL SYNTAX CHECK THE MANUL THAT..." Could somebody comment on my sql queries as i cannot see anything wrong with them?

Code Below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

namespace _Consultants
{
public partial class Form1 : Form
{ 
public Form1()
{
InitializeComponent();
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)
{
string uname = textBox1.Text;
string pword = textBox2.Text;

string connectionString = "Data Source=localhost" + ";Database=_consultants" + ";User ID=root" + ";Password=password;";
MySqlConnection MySqlConn = new MySqlConnection(connectionString);
string strSQL = ("SELECT (*) FROM user WHERE username = ' " + textBox1.Text + " 'AND password=' " + textBox2.Text + " ' ");
MySqlCommand mysqlCommand = new MySqlCommand(strSQL, MySqlConn);

try
{
MySqlConn.Open();
MySqlDataReader mysqlReader = mysqlCommand.ExecuteReader();
while (mysqlReader.Read())
{
string strusername;
string strpassword;

strusername = (mysqlReader["username"].ToString());
strpassword = (mysqlReader["password"].ToString()); 

if (strusername == uname && pword == strpassword)
{
textBox3.Text = "INCORRECT USER/PASS!";
} 

else 
{
textBox3.Text = "YOU ARE LOGGED IN!";
}
}
MySqlConn.Close();
}

catch (Exception ex)
{ 
MessageBox.Show("Wrong Input Entered", "Window Information" + ex.Message.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Information); 
}
}

private void textBox3_TextChanged(object sender, EventArgs e)
{
}
}
}


Thanks in advance,
C~Rose

This post has been edited by cobalt-rose: 24 December 2008 - 02:22 AM

Was This Post Helpful? 0
  • +
  • -

#13 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: Help with C# Login

Posted 24 December 2008 - 08:29 AM

Two issues that I saw is that you're not passing any strings in the connection string (passing strings would be with the use of apostrophes around strings) and the Select (*) should be changed to Select * (no parenthesis needed).

This post has been edited by tody4me: 24 December 2008 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Help with C# Login

Posted 24 December 2008 - 09:23 AM

This isn't exactly the way you go about checking a username and password. You don't need to use the DataReader. You don't need to do anything in the "while" loop.

This code is simpler and works:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

namespace _Consultants
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string connectionString = "Data Source=localhost;Database=_consultants;User ID=root;Password=password;";

			using(MySqlConnection MySqlConn = new MySqlConnection(connectionString))
			{
				string strSQL = "SELECT COUNT(*) FROM user WHERE username = '" + textBox1.Text + "' AND password= '" + textBox2.Text + "' ");
				MySqlCommand mysqlCommand = new MySqlCommand(strSQL, MySqlConn);

				try
				{
					MySqlConn.Open();

					//This will return 1 if the username and password exist.
					//It will return 0 if the username and password are not found.
					int count = (int)mysqlCommand.ExecuteScalar();

					if (count > 0)
						textBox3.Text = "YOU ARE LOGGED IN!";
					else
						textBox3.Text = "INCORRECT USER/PASS!";

					MySqlConn.Close();
				}
				catch (Exception ex)
				{
					MessageBox.Show("Wrong Input Entered", "Window Information" + ex.Message.ToString(),
					MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void textBox3_TextChanged(object sender, EventArgs e)
		{
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#15 cobalt-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-October 08

Re: Help with C# Login

Posted 24 December 2008 - 05:41 PM

Hey thanks, i did try that method and i kept getting the error message: Object reference not set to an instance of an object
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

namespace _Consultants
{
	public partial class Form1 : Form
	{  
		public Form1()
		{
			InitializeComponent();
		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string uname = textBox1.Text;
			string pword = textBox2.Text;
			
			string connectionString = "Data Source=localhost" + "; Database=_consultants" + "; User ID=root" + "; Password=password;";
			MySqlConnection MySqlConn = new MySqlConnection(connectionString);
			string strSQL = ("SELECT * FROM user WHERE username= ' " + textBox1.Text + " ' AND password= ' " + textBox2.Text + " ' ");
			MySqlCommand mysqlCommand = new MySqlCommand(strSQL, MySqlConn);
					
			try
				{
					MySqlConn.Open();

					//This will return 1 if the username and password exist.
					//It will return 0 if the username and password are not found.
					int count = (int)mysqlCommand.ExecuteScalar();

					if (count > 0)
						textBox3.Text = "YOU ARE LOGGED IN!";
					else
						textBox3.Text = "INCORRECT USER/PASS!";

					MySqlConn.Close();			
					}

			catch (Exception ex)
				{ 
					MessageBox.Show("Wrong Input Entered", "Window Information" + ex.Message.ToString(),
					MessageBoxButtons.OK, MessageBoxIcon.Information);  
				}
		}

		private void textBox3_TextChanged(object sender, EventArgs e)
		{
		}

		private void label3_Click(object sender, EventArgs e)
		{
		}
	}
 }


This post has been edited by cobalt-rose: 24 December 2008 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2