Click here to Skip to main content
16,022,060 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
<connectionStrings>
  <add name="=ConStr" connectionString="Data Source=DESKTOP-HL3PK63; Database=ASP_Database; User Id=sa; Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>






.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)
            {
                BindRepeaterData();
            }

        }
 protected void BindRepeaterData()
        {
            strsqlCommand = "Select * from Comment Order By PostedDate Desc";
            da = new SqlDataAdapter(strsqlCommand, cn);
            ds = new DataSet();
            da.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
        }
Posted
Updated 28-Feb-17 22:03pm
Comments
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
 
Share this answer
 

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



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