1

I want to retrieve all the registered users from my application using web service in ASP.NET. However, I have problem with connection string.

public static String getRegisteredUsers(string usernameCode)
    {
        string username = "";

        SqlConnection con = new SqlConnection("MyDatabaseConnectionString1");
        SqlCommand cmd = new SqlCommand("Select Username from Users where usernameCode = '" + usernameCode.ToUpper() + "'", con);
        con.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            username = dr["username"].ToString();
        }

        dr.Close();
        con.Close();

        return username;
    }

This MyDatabaseConnectionString1 is the connection string that I use for the table that I want to retrieve the information from. It works for that table, but not when it comes to web service.

<add name="MyDatabaseConnectionString1" connectionString= "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Korisnik\Desktop\Fitness\App_Data\MyDatabase.mdf;Integrated Security=True"/>

Should I use the same connection string for web service or it's done somehow differently ?

This is the error: enter image description here

2
  • The connection string is missing the initial catalog? See the MSDN page. However, unless you have multiple databases on that server, it shouldn't matter... possibly? Commented May 10, 2017 at 11:10
  • SqlConnection expects the connection string, not the name of the connection string. Commented May 10, 2017 at 11:12

2 Answers 2

2

You should not use MyDatabaseConnectionString1 (name of the connection string) directly as input for SqlConnection (which expects connection string as the input). Do this instead:

string connStr = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
... //continue as what you have done

You declare your MyDatabaseConnectionString1 in the .config file. The SqlConnection does not read your .config file but take the connStr input. Thus you get the error. You should read your connection string in the .config file by using ConfigurationManager available in the System.Configuration namespace. Then you use the connStr resulting from reading the .config file

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

Comments

1

Ensure System.Configuration assembly is referenced. Use this syntax:

 SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString);

2 Comments

Ye that works. Wondered that could've been the problem but didn't know how to display it with syntax.
Thanks to you all!

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.