Guys hi i need some help here though i fixed the connection with the sql server i tries to see if its correct and look first at the code and i explain m prob:

 private void lgnBtn_Click(object sender, EventArgs e)
    {
        SqlConnection lgnConnection  = new SqlConnection();
        lgnConnection.ConnectionString="Data    Source=.\\SQLEXPRESS;"+"AttachDbFilename=C:\\Users\\ryudo\\Desktop\\workspace\\sql_goldshop_project\\Project1\\Goldshop_Tax\\Goldshop_Tax\\gold_tax.mdf;"+"Integrated Security=True;"+"User Instance=True";
        lgnConnection.Open();
        SqlCommand cmd = new SqlCommand("SELECT username,password FROM users  WHERE username='" + textBox1.Text + "' and password='" + maskedTextBox1.Text + "'", lgnConnection);
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if (dr["username"].ToString() == textBox1.Text && dr["password"].ToString() == maskedTextBox1.Text)
            {
                [B]Close();[/B]

            }
            else
            {
               [B] Close();[/B]

            }
        }
        dr.Close();
        lgnConnection.Close();

i m tryin to make a login form but i tries when the username and password on my table users are the same with the one the user insert the form t close , but if i delete the close() under the else condition doesnt close it need it why havent i done sth correct ?? if u have any questions ask me i m new in c# and sql to thanks a lot!!

Doesn't close because the while statement runs out of reads, and drops into the dr & lgnconnection close...
It does not make sense to have both the true and false of a conditional result in the same statement "Close()". Why compare at all if the result is always going to be the same ?
Maybe you really mean to set the DialogResult to Ok or Cancel instead of just a close statemenet...

// Jerry

codereflex is a website BOT that is spamming all developer sites trying to drive traffic to their site.
The example this one points to is very lame.
Just use the System.Data.SqlClient.SqlConnectionBuilder class to generate your connection string. The rest is trivial.

Mr. JerryShaw

I'd like to divert your attention to the reality that I, Syed Asad Ahmed, a professional developer is running this website. If that was supposed to be a BOT, it would have written around 10-20 posts per day along with 100 posts around the forums and social media.

I'm sad that after writing 400+ posts on this forum, you're unable to understand why on earth do one has to create website. Of course, not for a charity but to encourage developer community (not freely). Nobody did that, not even daniweb. Otherwise the ad on the right side would've been a place for some anime junkies.

My appologies Mr. CodeReflex
So first, why did you respond to a post that is 3.5 years old? Don't you know that when you do that, everyone that has participated in that thread gets an email ?

Since you are a professional developer, and felt you should navigate the DaniWeb user to your site for the answer to this thread... why didn't you provide a quality code sample with comments to explain to the new or student developer what the code is doing, and why, and better yet, the construction of the SQL ConnectionString.

Sorry, but as a professional developer myself, I did not see any value in your post or your website sample code.

It is your perception because you stuck always to this forum. No offense to daniweb but there is always a chance to grow a community.

Mind your own business is a best policy. :)

Cheers!

Member Avatar for prathamesh3090
    public void LoGoBt_Click(object sender, EventArgs e)
    {
        if (LoUNameTxt.TextLength <= 0)
        {
            MessageBox.Show("Enter User Name");
            LoUNameTxt.Focus();
        }
        else if (LoPassTxt.TextLength <= 6)
        {
            MessageBox.Show("Enter Password greater than 6 char");
            LoPassTxt.Focus();
        }
        else if (LoAccTxt.TextLength < 8)
        {
            MessageBox.Show("enter Account Number 0f 11 character");
            LoAccTxt.Focus();
        }
        else if (LoMobTxt.TextLength < 10)
        {
            MessageBox.Show("enter 10 digit.");
            LoMobTxt.Focus();
        }
        else if (LoEmailTxt.TextLength <= 0)
        {
            MessageBox.Show("enter Email ID");
            LoEmailTxt.Focus();
        }
        else
        {
            SqlConnection sqlcon = new SqlConnection(@"Data Source=TRIMBAKKAR\SQLEXPRESS;Initial Catalog=OLB_DB;Integrated Security=True");
            sqlcon.Open(); MessageBox.Show("connection open");
            SqlCommand cmd = new SqlCommand("SELECT log_username, log_password FROM olb_login WHERE log_username='" + LoUNameTxt.Text + "' AND log_password='" + LoPassTxt.Text + "'", sqlcon);
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
           {
            //rdr.Read();
                if (rdr["log_username"].ToString() == LoUNameTxt.Text && rdr["log_password"].ToString() == LoPassTxt.Text)
                {
                    MessageBox.Show("login success");
                    MDI Form2 = new MDI();
                    Form2.Show();
                    Form2.MDIStartBt.Enabled = false;
                    Form2.MDIBackBt.Enabled = false;
                    Form2.MDIBalanceBt.Enabled = true;
                    Form2.MDIMoneyBt.Enabled = true;
                    Form2.MDILogutBt.Enabled = true;
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("access denied");
                }
            }
            rdr.Close();
            sqlcon.Close();MessageBox.Show("con close");
        }
    }
   i ude above code for login window but it not enterin in whil loop please help....

Try not using an SqlDataReader.... instead use a simple adapter and table. Most likely the original problem is that you did not set the SqlCommand CommandType property. No reason for the IF statement because the SQL will only return a row if it meets the criteria. Anyway, study the revised code, and ask questions for anything you do not understand.

-- Jerry

        private void LoGoBt_Click(object sender, EventArgs e)
        {
            if (LoUNameTxt.TextLength <= 0)
            {
                MessageBox.Show("Enter User Name");
                LoUNameTxt.Focus();
            }
            else if (LoPassTxt.TextLength <= 6)
            {
                MessageBox.Show("Enter Password greater than 6 char");
                LoPassTxt.Focus();
            }
            else if (LoAccTxt.TextLength < 8)
            {
                MessageBox.Show("enter Account Number 0f 11 character");
                LoAccTxt.Focus();
            }
            else if (LoMobTxt.TextLength < 10)
            {
                MessageBox.Show("enter 10 digit.");
                LoMobTxt.Focus();
            }
            else if (LoEmailTxt.TextLength <= 0)
            {
                MessageBox.Show("enter Email ID");
                LoEmailTxt.Focus();
            }
            else
            {
                DataTable table = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(
                        string.Format("SELECT TOP 1 * FROM olb_login WHERE log_username='{0}' AND log_password='{1}'", LoUNameTxt.Text, LoPassTxt.Text)
                        , @"Data Source=TRIMBAKKAR\SQLEXPRESS;Initial Catalog=OLB_DB;Integrated Security=True");
                try
                {
                    adapter.Fill(table);
                    if (table.Rows.Count > 0)
                    {
                        MessageBox.Show("login success");
                        MDI Form2 = new MDI();
                        Form2.Show();
                        Form2.MDIStartBt.Enabled = false;
                        Form2.MDIBackBt.Enabled = false;
                        Form2.MDIBalanceBt.Enabled = true;
                        Form2.MDIMoneyBt.Enabled = true;
                        Form2.MDILogutBt.Enabled = true;
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("access denied");
                    }

                }
                catch (SqlException sqlError)
                {
                    MessageBox.Show(sqlError.Message, "Error " + sqlError.Number.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    table.Dispose();
                    adapter.Dispose();
                }

                /* Old Way
                SqlConnection sqlcon = new SqlConnection(@"Data Source=TRIMBAKKAR\SQLEXPRESS;Initial Catalog=OLB_DB;Integrated Security=True");
                sqlcon.Open();
                MessageBox.Show("connection open");
                SqlCommand cmd = new SqlCommand("SELECT log_username, log_password FROM olb_login WHERE log_username='" + LoUNameTxt.Text + "' AND log_password='" + LoPassTxt.Text + "'", sqlcon);
                SqlDataReader rdr;
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    if (rdr["log_username"].ToString() == LoUNameTxt.Text && rdr["log_password"].ToString() == LoPassTxt.Text)
                    {
                        MessageBox.Show("login success");
                        MDI Form2 = new MDI();
                        Form2.Show();
                        Form2.MDIStartBt.Enabled = false;
                        Form2.MDIBackBt.Enabled = false;
                        Form2.MDIBalanceBt.Enabled = true;
                        Form2.MDIMoneyBt.Enabled = true;
                        Form2.MDILogutBt.Enabled = true;
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("access denied");
                    }
                }
                rdr.Close();
                sqlcon.Close();
                MessageBox.Show("con close");
                 */ 
            }
        }
Member Avatar for prathamesh3090

thank you very much jerry

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.