5

I have my database connectivity which I define under <appSettings> in web.comfig:

<appSettings>
    <add key="ConnStr" 
         value="Data Source=dsk-159\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"/>
</appSettings>

But the problem is that I am unable to access it from my aspx page as I am trying like this

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:goldsConnectionString %>"
     SelectCommand="SELECT distinct TechnologyId , [TechnologyName], [ChildId] FROM [TreeTable] where childid is null AND technologyid in(@hid1)">
     <SelectParameters>
         <asp:ControlParameter ControlID="hid1" Name="hid1" DefaultValue="23" />
     </SelectParameters>

In place of <connectionStrings> I want to define it in <appSettings>

Plesae tell the correct syntax.

4 Answers 4

5

Your web.config should be like below:

<appSettings>
<add key="ConnStr" value="Server=yourservername;Database=yourdatabasename;UID=yourusername;Password=youruserpassword"/>
</appSettings>

And your .aspx file should like following:

<asp:GridView ID="grd" runat="server" DataSourceID="SqlDataSource2">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ appSettings:ConnStr %>" SelectCommand="SELECT * FROM ticketmaster"></asp:SqlDataSource>
Sign up to request clarification or add additional context in comments.

Comments

5

You know you can set the connection string in the code behind rather than inline, it's much cleaner.

SqlDataSource2.ConnectionString = 
System.Configuration.ConfigurationManager.AppSettings["ConnStr"];

Consider reading up on ConfigurationManger.AppSettings

Comments

1

Try to change the aspx part to this:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>" SelectCommand="SELECT distinct TechnologyId , [TechnologyName], [ChildId] FROM [TreeTable] where childid is null AND technologyid in(@hid1)">
<SelectParameters>
<asp:ControlParameter ControlID="hid1" Name="hid1" DefaultValue="23" />
</SelectParameters>

I have done 2 things:

  1. removed the tags.
  2. changed the connection string name to match the name in the web.config
    (goldsConnectionString-->ConnStr)

4 Comments

Thanks for Reply.i done by code side. Actually i have defined my connection string in AppSetting in webconfig. so that was trying to acces from aspx page as i am unable to find from aspx so i done it on code behind like SqlDataSource2.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
I believe that my solution should work without the changes on the code side but if you can make changes on the code side (like @DGibbs answer) it's better solution. anyway, you should mark the answer that worked for you :-)
Hi David I tried the way u told <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>" SelectCommand="SELECT distinct TechnologyId , [TechnologyName], [ChildId] FROM [TreeTable] where childid is null AND technologyid in(@hid1)"> <SelectParameters> <asp:ControlParameter ControlID="hid1" Name="hid1" DefaultValue="23" /> </SelectParameters></asp:SqlDataSource> but i am getting error that The connection name 'ConnStr' was not found in the applications configuration or the connection string is empty.
your web.config contains the <b> tags?
0

you can set the connection string in your code-behind page

SqlDataSource2.ConnectionString =
System.Configuration.ConfigurationManager.AppSttings["ConnStr"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.