Click here to Skip to main content
15,305,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created connection string in configuration file

and then created connection object to access that but it is giving error as follows

Object reference not set to an instance of an object.

What I have tried:

Connecton String is as
  <add name="=ConStr" connectionString="Data Source=DESKTOP-HL3PK63; Database=ASP_Database; User Id=sa; Password=123" providerName="System.Data.SqlClient"/>

.cs file code

public partial class WebForm1 : System.Web.UI.Page
        SqlConnection cn = null;
        SqlCommand cmd = null;
        SqlDataAdapter da = null;
        DataSet ds = null;
        string strsqlCommand = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
            if (!Page.IsPostBack)

 protected void BindRepeaterData()
            strsqlCommand = "Select * from Comment Order By PostedDate Desc";
            da = new SqlDataAdapter(strsqlCommand, cn);
            ds = new DataSet();
            Repeater1.DataSource = ds;
Updated 28-Feb-17 22:03pm
Karthik_Mahalingam 1-Mar-17 3:58am
which line ?
paul_vin 1-Mar-17 4:00am
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);

1 solution

<add name="=ConStr"

remove the = symbol

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900