3 Replies - 47112 Views - Last Post: 19 May 2008 - 04:58 PM Rate Topic: -----

#1 anastariq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-May 08

Login code

Post icon  Posted 19 May 2008 - 02:24 AM

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


namespace WindowsApplication6
{
	
	public partial class Login :Form
	{
		public string g_Login;
		int iCount;
		Form1 frmMain = new Form1();
		public Login()
		{
			InitializeComponent();
			
		}

		private void Login_Load(object sender, EventArgs e)
		{
		   
		}
		
		private void button1_Click(object sender, EventArgs e)
		{

			// string g_Login;
			string ConString;
			ConString = "Data Source=.;Initial Catalog=docworx;User ID=sa";
			SqlConnection DbCon = new SqlConnection(ConString);

			g_Login = this.txtUsername.Text;
			String StrPassword = this.txtPassword.Text;

			if (g_Login = null |= StrPassword = null)
			{
				MessageBox.Show("You are missing information. Please make sure that both the username and password fields are filled out.", "Missing Info");
				this.txtUsername.Focus();
				return;
			}


			String StrSql = "Select userid,password Form Login Where userid='" + g_Login + "'";
			SqlCommand cm = new SqlCommand(StrSql, DbCon);
			SqlDataReader dr;
			Boolean valid = false;
			Boolean HasRows = false;
			try
			{
				DbCon.Open();
				dr = cm.ExecuteReader();

				if (dr.HasRows)
				{
					while (dr.Read())

						if (StrPassword == dr.Item("password"))
						{
							valid = true;
						}
					HasRows = true;
				}
				dr.Close();
			}
			catch (Exception exo)
			{

				MessageBox.Show(exo.Message);
			}
			finally
			{
				if (DbCon.State == ConnectionState.Open)
				{
					DbCon.Close();
				}
				cm = null;
				dr = null;
				DbCon.Dispose();
				GC.Collect();
			}
			iCount = iCount + 1;
			if (valid == true)
			{
				this.Hide();
				frmMain.Show();
			}
			else
				if (iCount == 3)
				{
					MessageBox.Show("Contact Safreak!", "Invalid Info");
					this.Close();
				}
				else
					if (HasRows == false)
					{
						MessageBox.Show("Invalid user name, try again!", "Invalid Info");
						this.txtUsername.Focus();
						this.txtUsername.Text = " ";
						this.txtPassword.Text = " ";
					}
					else
					{
						MessageBox.Show("Invalid password, try again!", "Invalid Info");
						this.txtPassword.Focus();
						this.txtPassword.Text = " ";

					}




		}
	}
}



im having these errors in my code:

Error 1 The type 'System.Data.SqlClient.SqlConnection' exists in both 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll' and 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.SqlClient.dll'


ERROR 2 it is not recognizing the item property of SqlDataReader

Is This A Good Question/Topic? 0
  • +

Replies To: Login code

#2 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Login code

Posted 19 May 2008 - 12:01 PM

System.Data is the parent of System.Data.SqlClient, so you can just use System.Data in your using and remove System.Data.SqlClient
Was This Post Helpful? 0
  • +
  • -

#3 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

Re: Login code

Posted 19 May 2008 - 12:20 PM

your SQL statement is wrong also. you misspelled from it should be "SELECT userID, password FROM"
Was This Post Helpful? 0
  • +
  • -

#4 thor78  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 06-May 08

Re: Login code

Posted 19 May 2008 - 04:58 PM

You didn't get a syntax error here? Your OR should be || not |=
if (g_Login = null |= StrPassword = null)
{
     MessageBox.Show("You are missing information. Please make sure that              both the username and password fields are filled out.", "Missing Info");
 this.txtUsername.Focus();
return;
            }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1