ASP.Net Error: Format of the initialization string does not conform to specification starting at index 0

Sumeet
 
on Jun 21, 2018 03:52 AM
Sample_153874.zip
6507 Views

When I am assign connectionString via code as: SqlConnection conn = MyConnection.ConnectionStr(); Then it's working perfectly. Or hardcore string in aspx page is also working.

But when I am assign string to SQlDataSource on Page_Load then it's giving me an error as

Format of the initialization string does not conform to specification starting at index 0.

But if connectionString is placed in separate class file then this method would give the following error.

I have a web project and made a MyConnection.cs in DAL folder under App_Folder. Class file is as below: 

namespace SLI.App_Code.DAL
{
    public class MyConnection
    {
        public static SqlConnection ConnectionStr()
        {
            SqlConnection conn = new SqlConnection();
            
            conn.ConnectionString = "Data Source=10.148.7.54;Initial Catalog=xxx;Persist Security Info=True;User ID=xxxx;Password=xxxxxx; Max Pool size = 500; Pooling = True;";
            

            return conn;
        }
    }
}

 

I need to assign MyConnection class string to SQlDataSource. How should I do this ?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
pandeyism
 
on Jun 26, 2018 05:14 AM

Hi Sumeet,

Please take reference the below code and correct your code.

HTML

<asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Customer Name" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data.SqlClient;

Code

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        SqlConnection con = MyConnection.ConnectionStr();
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        SqlDataSource1.ID = "SqlDataSource1";
        this.Page.Controls.Add(SqlDataSource1);
        SqlDataSource1.ConnectionString = con.ConnectionString;
        SqlDataSource1.SelectCommand = "SELECT top 3 ContactName, City, Country, PostalCode from Customers";
        this.GridView1.DataSource = SqlDataSource1; ;
        this.GridView1.DataBind();
    }
}

MyConnection.cs

using System.Data.SqlClient;

/// <summary>
/// Summary description for MyConnection
/// </summary>
public class MyConnection
{
    public static SqlConnection ConnectionStr()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=Northwind; UID=xxx;Password=xxxxxxx;";

        return conn;
    }
}

Screenshot