12

I am learning to write into a database from a textbox with the click of a button. I have specified the connection string to my NorthWind database in my web.config file. However I am not able to access the connection string in my code behind.

This is what I have tried.

protected void buttontb_click(object sender, EventArgs e)
{
    System.Configuration.Configuration rootwebconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Mohtisham");
    System.Configuration.ConnectionStringSettings constring;
    constring = rootwebconfig.ConnectionStrings.ConnectionStrings["northwindconnect"];
    SqlConnection sql = new SqlConnection(constring);

    sql.Open();
    SqlCommand comm = new SqlCommand("Insert into categories (categoryName) values ('" + tb_database.Text + "')", sql);
    comm.ExecuteNonQuery();
    sql.Close();
}

I get a tooltip error for

SqlConnection sql = new SqlConnection(constring);

as

System.data.SqlClient.Sqlconnection.Sqlconnection(string) has some invalid arguments.

I want to load the connection string from the web.config in constring

3
  • 2
    It is not related to question but writing into DB from text box cause sql injection. Commented Mar 21, 2013 at 6:30
  • @Popeye Is it? What do you suggest? Which is a better way to store user input values in a database? Commented Mar 21, 2013 at 6:34
  • Just search on google also you can refer this mikesdotnetting.com/Article/113/… Commented Mar 21, 2013 at 6:36

4 Answers 4

12

You can simply give a Name to your ConnectionString in web.config file and do this:

web.config:

<add name="ConnectionStringName"  connectionString=YourServer"; Initial Catalog=YourDB; Integrated Security=True"/>

Code Behind:

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString());
Sign up to request clarification or add additional context in comments.

Comments

8

That's because the ConnectionStrings collection is a collection of ConnectionStringSettings objects, but the SqlConnection constructor expects a string parameter. So you can't just pass in constring by itself.

Try this instead.

SqlConnection sql = new SqlConnection(constring.ConnectionString);

Comments

4

try this

readonly SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwindconnect"].ToString());

Comments

0

I will suggests you to create a function rather than directly access the web.config file as follow

    public static string GetConfigurationValue(string pstrKey)
    {
        var configurationValue = ConfigurationManager.AppSettings[pstrKey];
        if (!string.IsNullOrWhiteSpace(configurationValue))
            return configurationValue;

        throw (new ApplicationException(
            "Configuration Tag is missing web.config. It should contain   <add key=\"" + pstrKey + "\" value=\"?\"/>"));

    }

And use this function in you application

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.