This method read the Database and Returns the Username and Password. tblPassword has only one user name and a password.

            using (SqlConnection dataConnection = new SqlConnection("Data Source=isukoon-pc\\sqlexpress;Initial Catalog=AdsManager;Integrated Security=True"))
            using (SqlCommand dataCommand =
                    new SqlCommand("select username from tblPassword", dataConnection))
            using (SqlCommand data2Command =
                    new SqlCommand("select password from tblPassword", dataConnection))
            {

                dataConnection.Open();
                Uname = Convert.ToString(dataCommand.ExecuteScalar());
                Pword = Convert.ToString(data2Command.ExecuteScalar());
            }        

Then I compare those two values with textbox values.

if (textboxusername.Text == Uname && textboxpwd.Text == Pword)
            {

                var form = new MasterAds();
                form.Show();
                this.Hide();
            }

This is not worked for me. Even I had used

String.Compare();

and got the same result. I even used

MessageBox.Show(Uname);
MessageBox.Show(Pword);

It clearly shows the correct Uname and the Pword too.

Did you try the string Trim method?
Sometimes some whitespace is not removed.

try inserting a breakpoint on that line.

Breakpoint-> if (textboxusername.Text == Uname && textboxpwd.Text == PWord)

Then check the value of textboxusername.Text and Uname by right clicking and adding a quick watch. You can then copy the two values over to something like notepad and see if there are any significant differences.

@Fenrir(): In debugger you could also use text visualiser, so notepad is not really needed.

@ddanbe : Thankx mate. I fixed it using Trim() method. @Fenrir : I found it by using ur method.

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.