0

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?

3
Contributors
10
Replies
12
Views
5 Years
Discussion Span
Last Post by jateshs1007
0

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

Edited by jateshs1007

0

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

0

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".

0

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.

0
<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.

Edited by jateshs1007

0

<?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>
0

<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?

0

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?

Edited by jateshs1007

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.