0

I did some coding on connecting data using Azure database on Windows Form and when I tried to retrieve the data, I received the following error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll.

Also in addition, I received a Login failed for user ''. When I tried to retrieve the data and located at myConnection.Open();

private void btnRetrieve_Click(object sender, EventArgs e)
{
    //Create a connection calling the App.config
    string conn = ConfigurationManager.ConnectionStrings["NYPConnection"].ConnectionString;
    //The query to use
    string query = "SELECT * FROM Users";
    SqlConnection myconnection = new SqlConnection(conn);
    //Create a Data Adapter
    SqlDataAdapter dadapter = new SqlDataAdapter(query, myconnection);
    //Create the dataset
    DataSet ds = new DataSet();

    //Open the connection
    ******myconnection.Open();******

    //Fill the Data Adapter
    dadapter.Fill(ds, "Users");
    myconnection.Close();
    //Bind the datagridview with the data set
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = "Users";
}          
2
  • 1
    It appears that the database user credentials in the connection string is not correct. Commented Feb 25, 2016 at 3:26
  • Perhaps the answer to this question will solve your problem? stackoverflow.com/questions/24510804/… Commented Feb 25, 2016 at 3:26

1 Answer 1

1

Print out the value of conn, using the following code:

string conn = ConfigurationManager.ConnectionStrings["NYPConnection"].ConnectionString;
Debug.WriteLine("conn= " + conn);

Have a look in the output window and you will probably find that conn is set to an empty string or maybe does not have the user name specified.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.