I have a making a grid view from code behind but the error occurs

My code is:

    string str = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
            {


                if (!IsPostBack)
                {
                    GetData();
                }
            }

            public void GetData()
            {

                SqlDataSource ds = new SqlDataSource();
                ds.ConnectionString = str;
                ds.SelectCommand = "SELECT * FROM mytableinfo";

                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }

Error is for:
string str = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;

Object reference not set to an instance of an object.

Can anyone help me with this?

Recommended Answers

All 10 Replies

Have you validate that you have defined "SQLConn" in your web.config file?

Yes i have vaidate and using it for my login page too.
What i am trying to do is extract data from my MSsql Database and display in Gridview. But not happening. Do u have any solution for this

<add key="SQLConn" value="Data Source=(local);Initial Catalog=Database Name;Integrated Security=True" />

Member Avatar for michelleradu

Try this:
In web.config:

  <add name="SQLConn" connectionString="Data Source=(local);Initial Catalog=Database Name;Integrated Security=True" />

In C#:

   string connectionString = ConfigurationManager.ConnectionStrings["SQLConn"].Name;
   ....
   ds.ConnectionString = connectionString;

Note that you use "name" and "connectionString" in the web.config to define a connection string.
Also, to get the connection string in C#, you use "Name" instead of "ConnectionString".

Also, with regard to Data Source(local), make sure that instead of local, you have the correct FQDN of the computer that is running this DB. If you simply changed the value for the purpose of this posting, that's OK. But if you do have it defined as "local", that isnt going to resolve to anything unless you have an entry in your HOSTS file or other name resolution services. If SQL is running locally, it should be localhost instead of local.

<add name="SQLConn" connectionString="Data Source=(local);Initial Catalog=Database Name;Integrated Security=True" ></add>

I tried but didn't work. It says that "name" is not allowed.

Member Avatar for michelleradu

Can you show us the web.config?

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <appSettings>
     <add key="SQLConn" value="Data Source=(local);Initial Catalog=Database Name;Integrated Security=True" />


  </appSettings>



  <connectionStrings>

    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
    <add name="Database NameConnectionString" connectionString="Data Source=(local);Initial Catalog=Database Name;Integrated Security=True" providerName="System.Data.SqlClient" />
  <add name="Database NameEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(local);Initial Catalog=Database Name;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0"><assemblies><add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /></assemblies></compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/WebForm1.aspx"
             protection="All"
             timeout="1"
             name=".USERLOGINCONTROLAUTH"
             path="/"
             requireSSL="false"
             slidingExpiration="true"
             defaultUrl="~/Index.aspx"
             cookieless="UseDeviceProfile"
             enableCrossAppRedirects="false"/>
    </authentication>
 </system.web>
</configuration>
Member Avatar for michelleradu

<add name="SQLConn" connectionString="Data Source=(local);Initial Catalog=Database Name;Integrated Security=True" ></add>

That should be under <connectionStrings> in web.config . Where did you put it?

You can even put it under appsettings and even works for login and registration page and for other stuffs.
But somebody have a solution for this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.