Format of the initialization string does not conform to specification starting at index 0 visual web developer

StackOverflow https://stackoverflow.com/questions/18407358

  •  26-06-2022
  •  | 
  •  

I have read several of the responses to this error and have tried all of them without success.

Here is what I have in the web.config file:

<connectionStrings>
    <add name="SRM_MetricConnectionString" connectionString="Data Source=C3SRMSQL01T;Initial Catalog=SRM_Metric;Persist Security Info=True;User ID=**;Password=**"
        providerName="System.Data.SqlClient" />

</connectionStrings>

Here is the call in the aspx.cs:

    private void BindGridView()
    {

        // Get the connection string from Web.config. 

        // When we use Using statement, 

        // we don't need to explicitly dispose the object in the code, 

        // the using statement takes care of it.

        using (SqlConnection conn = new SqlConnection("SRM_MetricConnectionString"))

        {

            // Create a DataSet object.

            DataSet dsAnno = new DataSet();

In most of the responses SqlConnection(ConfigurationManager.ConnectionStrings["TestDataConnectionString"].ToString()); seems to be the format. When I do that the response is ConfigurationManager is not valid in this context.

Here are the using declarations I am using:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Data.Sql;

Any insight would be greatly appreciated. I am behind on getting this to work.

有帮助吗?

解决方案

You must use same name in web.config and code behind to ConnectionStrings name.

string ConStr = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ToString();
using (SqlConnection conn = new SqlConnection(ConStr))
{
  // ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top