Solved

How to make login form with connectionstring

Posted on 2008-10-02
6
4,150 Views
Last Modified: 2012-05-05
i am makiing a simple login form where i can login with txtusername.Text and password.Text into connectonstring and logon.

But i am getting error message: +            ex      {"The connection was not closed. The connection's current state is open."}      System.Exception {System.InvalidOperationException}

Can somone hlep me fiksing this bug...?

public void SqlServerLogin(string Sql, string connectionString)
        {
 
            
                SqlConnection sqlConnection2 = new SqlConnection();
                sqlConnection2.ConnectionString = "Data Source=" + sqlConnection2.DataSource + ";initial catalog=" + sqlConnection2.Database + ";Integrated Security=SSPI " + ";user id=" + txtUsername.Text + ";pwd=" + txtPassword.Text;
                sqlConnection2.Open();
                SqlCommand cmd = new SqlCommand(Sql, sqlConnection2);
                //SqlDataReader dr;
                //cmd.ExecuteNonQuery();
                try
                {
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Couldnt execute the query! " + ex.Message.ToString());
                }
                finally
                {
                    cmd.Connection.Close();
                }
}

Open in new window

0
Comment
Question by:Adnan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:johnaryan
ID: 22622677
I'd give this a go in your finally

 finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (sqlConnection2 != null)
                {
                    sqlConnection2.Close();
                    sqlConnection2 = null;
                }
            }

Open in new window

0
 

Author Comment

by:Adnan
ID: 22622699
thanks man, it helped, but now iam getting this error message: +            ex      {"Object reference not set to an instance of an object."}      System.Exception {System.NullReferenceException}

is my method correct for logon to sql server with sqlConnection2.ConnectionString = "Data Source="......!!!
0
 
LVL 5

Accepted Solution

by:
johnaryan earned 500 total points
ID: 22622770
When running through a debugger, when you hit the line
sqlConnection2.Open();
What is the state of the sqlConnection?

The connection string i use for SQL Server is of the form:
"Server=myServerName, port;Database=myDatabase;Uid=username;Pwd=password
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Adnan
ID: 22622784
when i hit the line sqlConnection2.Open(); the state i closed....????!!
0
 
LVL 5

Expert Comment

by:johnaryan
ID: 22623023
I just want to clarify, is the state closed after the statement runs ( by this i mean when the debugger hits the next line) ?

0
 

Author Comment

by:Adnan
ID: 22623403
i want to make a login form for sql server Authentication....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question