Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 135,925 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,612 people online right now. Registration is fast and FREE... Join Now!




Login code

 
Reply to this topicStart new topic

Login code

anastariq
19 May, 2008 - 01:24 AM
Post #1

New D.I.C Head
*

Joined: 19 May, 2008
Posts: 1

CODE
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
User is offlineProfile CardPM
+Quote Post

zakary
RE: Login Code
19 May, 2008 - 11:01 AM
Post #2

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
System.Data is the parent of System.Data.SqlClient, so you can just use System.Data in your using and remove System.Data.SqlClient
User is online!Profile CardPM
+Quote Post

marcells23
RE: Login Code
19 May, 2008 - 11:20 AM
Post #3

D.I.C Head
Group Icon

Joined: 22 Aug, 2007
Posts: 134



Thanked: 3 times
Dream Kudos: 125
My Contributions
your SQL statement is wrong also. you misspelled from it should be "SELECT userID, password FROM"
User is offlineProfile CardPM
+Quote Post

thor78
RE: Login Code
19 May, 2008 - 03:58 PM
Post #4

D.I.C Head
Group Icon

Joined: 6 May, 2008
Posts: 106


Dream Kudos: 50
My Contributions
You didn't get a syntax error here? Your OR should be || not |=
csharp

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;
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:15AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month