Solved

How to make login form with connectionstring

Posted on 2008-10-02
6
4,142 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
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:johnaryan
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

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

Expert Comment

by:johnaryan
Comment Utility
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
Comment Utility
i want to make a login form for sql server Authentication....
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now