using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace book
{
    public partial class login : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader reader;
        public login()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)//MY login button
        {
            string constr = @"Data Source=PRAVEEN\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True";
            conn = new SqlConnection(constr);
            conn.Open();
            cmd = new SqlCommand("select *from travelregi where name='" + username + "' and password='" + pwd + "'", conn);
            reader = cmd.ExecuteReader();
            if(reader.HasRows)
            {

                {
                    MessageBox.Show("sucessful");

                }

                reader.Close();
                conn.Close();
            }
        }

I am trying to check if username and password match from database. But when i hit login ion form nothing hapens :(

Without checking if your code is valid...

Logically... If you arent getting an error, something must be happening. The problem is that what you are expecting isnt what is happending.

You have a conditional statement on line 23. You are assuming that its returning true. It may be that its always returning false.

If you want to check your code, add include an else statement to capture the false condition.

yup tried an alse statement...and now output is always false

That would indicate that your database connection and query are fine, its just returning and empty result set i.e. that user name and password combination are found in the database.
Your code doesn't show where username and pwd are being defined. Are they the actual textbox controls themselves? Add a debug and check the string held in the command object to make sure the right values are being passed in.

yes 'username' and 'pwd'are names of textboxes and 'name' and 'password' are column names.do i need to store them in a variable or somethiing

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.SqlClient;
namespace book
{
    public partial class login : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader reader;
        public login()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string constr = @"Data Source=PRAVEEN\\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True";
            conn = new SqlConnection(constr);
            conn.Open();

            cmd = new SqlCommand("select *from travelregi where name='" + username + "' and password='" + pwd + "'", conn);

            reader = cmd.ExecuteReader();
            if(reader.HasRows)
            {
                if (this.username.Text == "name".ToString() && this.pwd.Text == "password".ToString())
                {
                    MessageBox.Show("login sucessful");
                }


                 else
                {
                    MessageBox.Show("failed login");
                }

                reader.Close();
                conn.Close();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {


        }

        private void button3_Click(object sender, EventArgs e)

        {
            reg n = new reg();
            n.Show();
        }
    }
}

Now i am getting invalid operation exception was unhandled. Some one please help me

okay i realised that exception was thrown because of "//" but now again i am back to square 1 there are no values being stored to cmd ....the condition always jumps to else....thank you in advance...plz help :(

my table has more than two columns....is that the problem do i need to create a separate table for username and password.....why isnt line 28 taking any effect...plz sm1 help me

private void button2_Click(object sender, EventArgs e)
        {
            string constr = @"Data Source=PRAVEEN\\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True";
            conn = new SqlConnection(constr);
            conn.Open();
            cmd = new SqlCommand("select *from travelregi where name='" + username + "' and password='" + pwd + "'", conn);
            reader = cmd.ExecuteReader();
            if(reader.HasRows)
            {
               reader.Read();
                if (this.username.Text == "name".ToString() && this.pwd.Text == "password".ToString())
                {
                    MessageBox.Show("login sucessful");
                }
                 else
                {
                    MessageBox.Show("failed login");
                }
                reader.Close();
                conn.Close();
            }
        }

The problem is the loop thing. You don't need it here but you NEED and you MUST read AT LEAST once through the database to get your data. The database won't read for you automatically you have to give it orders(code)xd.
-I just added a reader.Read();
-you may have to use the trim function to remove extra spaces but first just try the code above and then reply here...

hey thnx buddy :)... i changed line 6.....i changed it to username.Text and pwd.Text... and it worked like magic :P

commented: yeap it seems that the propety was only the problem, well done! Be sure that you avoid concatenation,use the code below instead for safe queries xd +4
 private void button1_Click(object sender, EventArgs e)
            {

            string constr = @"Data Source=PRAVEEN\\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True"; ;
            conn = new SqlConnection(constr);
            conn.Open();
            cmd = new SqlCommand(@"SELECT * FROM travelregi
                                   WHERE name=@im and password=@prezim", conn);

            cmd.Parameters.Add(new SqlParameter("im", username.Text));
            cmd.Parameters.Add(new SqlParameter("prezim", pwd.Text));
            reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                reader.Read();


                if (this.username.Text ==reader.GetString(0).Trim() && this.pwd.Text == reader.GetString(1).Trim())
                {
                    MessageBox.Show("login sucessful");
                }
                else
                {
                    MessageBox.Show("failed login");
                }
                reader.Close();
                conn.Close();
            }

        }

If the above does not work then thid definitely should!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.