Results 1 to 21 of 21

Thread: web.config appsettings

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    web.config appsettings

    Hi, this book i am reading says there is an "appsettings" section in the web.config file. I have been thru the web.config file and there is no such section.


    Is the book wrong or is my web.config file missing something?
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Then put one in there
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Adding to what cander said. All the settings are not included in the web.config file when first created. Only the necessary parts, so you have to add the parts that you want to include.

  4. #4

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Yah i tried that...

    <appSettings>
    <add key="Northwind"
    value="Provider=SQLOLEDB; Data Source=localhost;
    Initial Catalog=Northwind; User ID=sa; Password=;"
    />
    </appSettings>

    but when i run the app i get an error that says something about unable to start debugging on the web server please make sure there are no syntax errors in the web.config...
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    probably put it in the wrong place. <appSettings> element goes direcly inside the <configuration> element
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Ahhh yep that did it. I had it inside the system.web element.

    Thanks!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Does anyone have a web resource that lists all the elements that are recognized in the web config file?

    It would be cool to be able to know all the different things we can put in it without having to hunt down each element on the web.

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I'm having a similar problem in a web service, but from what I can find, things look ok.

    This is part of my web.config:
    Code:
        <appSettings>
            <add key="database_connection_string" value="Persist Security Info=False;Integrated Security=SSPI;database=Patriot;server=MIKELAP"/>
        </appSettings>
    
    </configuration>
    And after this line executes, myConnectionString is still null
    Code:
    myConnectionString = ConfigurationSettings.AppSettings["database_connection_string"];
    What am I missing?

  10. #10
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366

  11. #11
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    MSDN has an overwhelming amount of info on this...
    Sure they do, where do you think I copied and pasted my code from ?

    Problem is, I can't get it to work. I must have read at least three different ways to do the same thing. But I still can't figure it out. Hence my post.

    Ft. Collins, eh? I went to school at CSU. My sister lives there. Help me out and I'll buy you a beer next time I visit

    Mike

  12. #12
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    hmmm, well I DO love beer...

    ok, since you've posted only a few pieces what's the big picture. Is this just a plain vanilla website, and your webservice is at the root level? Or is it buried in a virtual directory or something and there are multiple web.configs? how's your site set up.

    How are you testing that that variable is nulll? are you stepping through the code and trying to watch it or something else? exception being thrown, etc...

    The code you have should work so something else has to be happening somewhere else.

    Oh, and you say you cut and pasted your code from MSDN, was there a specific example you were trying out or was it just a code snippet and not really an example? Maybe post that or a link to it. And post your web.config file if you can(without any sensitive info if there is any).

  13. #13
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    LOL. I went and looked at your profile and saw that one of your interests were micro-brews. I was in Ft. Collins when New Belgium started up. I've drank hundreds of gallons of Fat Tire - still my favorite, but I'm getting off topic.

    I guess it's a plain vanilla web service - this is a web service and not a web site - if there's a difference. I'm just starting this and went with the defaults, so nothing too complex. Attachment is a zipped screen shot and my web.config.

    Yeah, it was a code snippet, you can find it at http://msdn.microsoft.com/library/de...ngselement.asp

    I just tried to mimic the example. I can tell because an exception is being thrown. Here's a couple of relevant code pieces:

    PHP Code:
        public class PatriotClientWebService System.Web.Services.WebService
        
    {
            
    string myConnectionString;

            public 
    PatriotClientWebService()
            {
                
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
                
    InitializeComponent();
                
                
    myConnectionString ConfigurationSettings.AppSettings["database_connection_string"]; 
    and then this:

    PHP Code:
            [WebMethod]
            public 
    DataSet GetUserTables()
            {
                
    DataSet ds = new DataSet("dsUserTables");
                
    SqlConnection myConnection = new SqlConnection();
                
    myConnection.ConnectionString myConnectionString;
                
    SqlDataAdapter myAdapter = new SqlDataAdapter();
                
    myAdapter.SelectCommand = new SqlCommand("SELECT * FROM SYSOBJECTS WHERE TYPE = 'U' AND OBJECTPROPERTY(id,'IsMSShipped') = 0 ORDER BY NAME"myConnection);
                
    int i myAdapter.Fill(ds"dtUserTables");
                return 
    ds;
            } 
    The InvalidOperationException happens on the line with myAdapter.Fill - saying the ConnectionString property has not been initialized. When I hover with the mouse over myConnectionString, the tool tip says it's null.

    Thanks,
    Mike
    Attached Files Attached Files

  14. #14
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Looking at this a little closer, my attempt at initializing myConnection string in the constructor does not work. Guess I'm doing something wrong there. Attachment is a zipped screen shot immediately after I try to set myConnectionString.
    Attached Files Attached Files

  15. #15
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    I couldn't get your example to not work... weird. So I've rewritten your method a little differently, and not for any other reason than a shot in the dark, it does the exact same thing your other one did.
    Here's the entire contents of the code behind for Service1.asmx:
    PHP Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;

    namespace 
    PatriotGames
    {
        
    /// <summary>
        /// Summary description for Service1.
        /// </summary>
        
    public class Service1 System.Web.Services.WebService
        
    {
            [
    WebMethod
            public 
    DataSet GetUserTables() 
            {
                
    string myConnectionString ConfigurationSettings.AppSettings"database_connection_string" ];
                if( 
    myConnectionString == null || myConnectionString.Length == )
                {
                    throw new 
    ApplicationException"Connection string was missing or empty." );
                }
                
    string cmdText "SELECT * FROM SYSOBJECTS WHERE TYPE = 'U' AND OBJECTPROPERTY(id,'IsMSShipped') = 0 ORDER BY NAME";
                
    DataSet ds = new DataSet("dsUserTables");
                
    usingSqlConnection myConnection = new SqlConnectionmyConnectionString ) )
                
    usingSqlDataAdapter myAdapter = new SqlDataAdaptercmdTextmyConnection )) 
                {
                    
    myAdapter.Fillds );
                }
                return 
    ds
            }
        }

    and here's the entire web config i'm using:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="database_connection_string" value="Persist Security Info=False;Integrated Security=SSPI;database=Pubs;server=DeathAngel"/>
        </appSettings>
    </configuration>
    ...but note, i was able to get your example to work as you posted, with no modifications. The site i deployed the webservice is a virtual directory under the localhost site. the localhost has a web.config as does the virtual directory where the web services lives. I am running Windows 2003 Server which may or may not make a difference, I run WindowsXP at work and have noticed some differences. I use a fileshare and not frontpage extensions to access the project from visual studio. Um...I can't think of anything else that could be wrong with your set up though. Can you read anything from your config files(i.e. not through a webservice but just a regular web page)?

  16. #16
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Wait, hold on a second. That's the ENTIRE web.config file you're using? I took the default one and added the appSettings element. Should I have a stand-alone web.config? Throw away the default stuff?

  17. #17
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Shouldn't matter. I did it first with the config file you posted, but changed up the connection string so it worked and that still worked. Figured I'd remove all of the other settings to remove as many variables from the equation as possible. It should work fine with those few lines, give it a try.

  18. #18
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Tom Clancy fan, eh? I tried your code in place of mine, no joy. I'm going to do a quick (?) rewrite to try this again. I really don't know if this is an issue, but I turned on Visual Source Safe for this project. I did the same for a Compact Framework project I have and VSS deploys the files as read-only. Screwed the app up.

    I'll try something clean - that is, no VSS and see if there is a difference.

    pvb, I appreciate your time. Will post what I find, might take me a couple days before I get to it - can't code all weekend, you know.

    Time for a beer.

    Thanks,
    Mike

  19. #19
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I finally figured out what was going on, although I'm not so sure how to fix it (nothing to do with VSS BTW).

    Problem is, I have a solution that right now, has two projects - a web service and a windows forms app. I thought it would be smart to group related projects in a solution, but maybe not.

    Apparently, the web service project is not looking at it's own web.config when the ConfigurationSettings.AppSettings line is executed. When I add a App.Config to the Windows form project and put the <appSettings> in there, things work fine.

    Is this by design? Is there a way to get the web service to look at it's own web.config and the GUI app to look at it's own App.config? Maybe I should only have one project in each solution? My original thought was to have several projects in this solutions, because they are all related.

    I'm not really sure how all this stuff should work, can anyone explain?

    Thanks,
    Mike

  20. #20
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    only thing i can think of is that in this one instance, is that since your UI is the windows startup project that only one config file supporting that process would be loaded(that is the main process running is the UI and not the web service); so maybe in that instance you'll want to run the web service on it's own. Really, both of those processes(the UI part and web services part) are main processes, the web service is(can be) a standalone process and the UI as well. Not sure if that's an ideal scenario for grouping them together in one solution where only one can be the start up project. This is different than having a data layer project, business layer project, etc supporting the main app, and having all of those gouped into one solution. I'm a little low on caffeine this morning and might not be making sense yet but maybe there was a nugget of help in the above.

  21. #21
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Putting the web service in it's own solution solves the problem. Doesn't seem to make a difference which one is the startup project.

    I don't quite understand all that, to me it makes sense to have in one solution, but whatever, at least it's working and I know why it wasn't working.

    Thanks for your time, pvb, looks like I owe

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width